-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy path177 4-selenium-driver.py
146 lines (133 loc) · 5.65 KB
/
177 4-selenium-driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
Created on May 30, 2018
@author: venkateshwara.d
'''
from selenium.webdriver.common.by import By
from traceback import print_stack
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
import utilities.custom_logger as cl
import logging
import time
import os
class SeleniumDriver():
log = cl.customLogger(logging.DEBUG)
def __init__(self, driver):
self.driver = driver
def screenShot(self, resultMessage):
"""
Takes screenshot of the current open web page
"""
fileName = resultMessage + "." + str(round(time.time() * 1000)) + ".png"
screenshotDirectory = "../screenshots/"
relativeFileName = screenshotDirectory + fileName
currentDirectory = os.path.dirname(__file__)
destinationFile = os.path.join(currentDirectory, relativeFileName)
destinationDirectory = os.path.join(currentDirectory, screenshotDirectory)
try:
if not os.path.exists(destinationDirectory):
os.makedirs(destinationDirectory)
self.driver.save_screenshot(destinationFile)
self.log.info("Screenshot save to directory: " + destinationFile)
except:
self.log.error("### Exception Occurred when taking screenshot")
print_stack()
def getTitle(self):
return self.driver.title
def getByType(self, locatorType):
locatorType = locatorType.lower()
if locatorType == "id":
return By.ID
elif locatorType == "name":
return By.NAME
elif locatorType == "xpath":
return By.XPATH
elif locatorType == "css":
return By.CSS_SELECTOR
elif locatorType == "class":
return By.CLASS_NAME
elif locatorType == "link":
return By.LINK_TEXT
else:
self.log.info("Locator type " + locatorType +
" not correct/supported")
return False
def getElement(self, locator, locatorType="id"):
element = None
try:
locatorType = locatorType.lower()
byType = self.getByType(locatorType)
element = self.driver.find_element(byType, locator)
self.log.info("Element found with locator: " + locator +
" and locatorType: " + locatorType)
except:
self.log.info("Element not found with locator: " + locator +
" and locatorType: " + locatorType)
return element
def elementClick(self, locator, locatorType="id"):
try:
element = self.getElement(locator, locatorType)
element.click()
self.log.info("Clicked on element with locator: " + locator +
" locatorType: " + locatorType)
except:
self.log.info("Cannot click on the element with locator: " + locator +
" locatorType: " + locatorType)
print_stack()
def sendKeys(self, data, locator, locatorType="id"):
try:
element = self.getElement(locator, locatorType)
element.send_keys(data)
self.log.info("Sent data on element with locator: " + locator +
" locatorType: " + locatorType)
except:
self.log.info("Cannot send data on the element with locator: " + locator +
" locatorType: " + locatorType)
print_stack()
def isElementPresent(self, locator, locatorType="id"):
try:
element = self.getElement(locator, locatorType)
if element is not None:
self.log.info("Element present with locator: " + locator +
" locatorType: " + locatorType)
return True
else:
self.log.info("Element not present with locator: " + locator +
" locatorType: " + locatorType)
return False
except:
print("Element not found")
return False
def elementPresenceCheck(self, locator, byType):
try:
elementList = self.driver.find_elements(byType, locator)
if len(elementList) > 0:
self.log.info("Element present with locator: " + locator +
" locatorType: " + str(byType))
return True
else:
self.log.info("Element not present with locator: " + locator +
" locatorType: " + str(byType))
return False
except:
self.log.info("Element not found")
return False
def waitForElement(self, locator, locatorType="id",
timeout=10, pollFrequency=0.5):
element = None
try:
byType = self.getByType(locatorType)
self.log.info("Waiting for maximum :: " + str(timeout) +
" :: seconds for element to be clickable")
wait = WebDriverWait(self.driver, 10, poll_frequency=1,
ignored_exceptions=[NoSuchElementException,
ElementNotVisibleException,
ElementNotSelectableException])
element = wait.until(EC.element_to_be_clickable((byType,
"stopFilter_stops-0")))
self.log.info("Element appeared on the web page")
except:
self.log.info("Element not appeared on the web page")
print_stack()
return element