-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresources.py
236 lines (188 loc) · 7.17 KB
/
resources.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from time import sleep
class Resources:
WAITTIME = 2
LONGWAITTIME = 5
SHORTWAITTIME = 1
VERYSHORTWAITTIME = 0.5
def __init__(self,TargetUrl):
"""
Controls a browser by sending commands to a remote server. A common Class which can se used by any Selenium work
:Attributes:
- TargetUrl : base url of the webdriver
"""
self.BASEURL= TargetUrl
chrome_options = webdriver.ChromeOptions()
self.driver = webdriver.Chrome('chromedriver', options=chrome_options)
self.openUrl()
self.driver.delete_all_cookies()
def get_curr_url(self):
"""
Gets the URL of the current page.
:Usage:
driver.current_url
"""
return self.driver.current_url
def check_url(self,text, Partial= False):
"""
Check Element exists or not
:Args:
- text - url which you want to compare.
- Partial (Defailt value 'false')- check certian part of url if trur .
:Returns:
- true if element found, else false
:Usage:
element = driverObject.check_url('https://www.linkedin.com/')
element = driverObject.check_url('https://www.linkedin.com/', True)
"""
if Partial == True:
return True if text in self.get_curr_url() else False
return True if text == self.get_curr_url() else False
def is_element_exist(self,text,typeofelement):
"""
Check Element exists or not
:Args:
- text - The path of the element.
- typeofelement - Type of element to search target .
:Returns:
- true if element found, else false
:Raises:
- NoSuchElementException - if the element wasn't found
:Usage:
element = driverObject.is_element_exist('foo', 'class')
element = driverObject.is_element_exist('alert', 'class')
"""
return True if len(self.get_elements_data(text,typeofelement)) > 0 else False
def get_elements_data(self,text,typeofelement):
"""
Finds and return an elements
:Args:
- text - The path of the element.
- typeofelement - Type of element to search target .
:Returns:
- list of WebElement - a list with elements if any was found. An
empty list if not
:Raises:
- NoSuchElementException - if the element wasn't found
:Usage:
element = driverObject.get_elements_data('foo', 'class')
element = driverObject.get_elements_data('alert', 'class')
"""
if str(typeofelement).lower() == "partial_link_text":
elements = self.driver.find_elements_by_partial_link_text(text)
elif str(typeofelement).lower() == 'class':
elements = self.driver.find_elements_by_class_name(text)
elif str(typeofelement).lower() == 'id':
elements = self.driver.find_elements_by_id(text)
elif str(typeofelement).lower() == 'xpath':
elements = self.driver.find_elements_by_xpath(text)
else:
raise Exception('INVALID ARRUGMENT')
return elements
def get_element_data(self,text,typeofelement):
"""Finds and return an elements.
:Args:
- text - The path of the element.
- typeofelement - Type of element to search target .
:Returns:
- WebElement - elements if found.
:Raises:
- NoSuchElementException - if the element wasn't found
:Usage:
element = driverObject.get_element_data('foo', 'class')
element = driverObject.get_element_data('alert', 'class')
"""
if str(typeofelement).lower() == "partial_link_text":
element = self.driver.find_element_by_partial_link_text(text)
elif str(typeofelement).lower() == 'class':
element = self.driver.find_element_by_class_name(text)
elif str(typeofelement).lower() == 'id':
element = self.driver.find_element_by_id(text)
elif str(typeofelement).lower() == 'xpath':
element = self.driver.find_element_by_xpath(text)
else:
raise Exception('INVALID ARRUGMENT')
return element
#
def press_button(self,role,data_testid):
"""Finds and Press the buttonz.
:Args:
- Role - Role value of Button.
- data_testid - data_testid value of Button.
:Returns:
- No Return Type
:Raises:
- NoSuchElementException - if the element wasn't found
:Usage:
driverObject.press_button('button', 'login')
"""
text= "//div[@role='{}' and @data-testid='{}']".format(role,data_testid)
if self.is_element_exist(text,'xpath'):
self.get_element_data(text,'xpath').click()
else:
self.press_button(role,data_testid)
def clearField(self,text,typeofelement):
"""
clear the field in the web element
Example:
webdriverObject.clearField(text, 'xpath')
webdriverObject.clearField('//input[@id="username"]', 'xpath')
webdriverObject.clearField('username', 'class')
webdriverObject.clearField('username', 'id')
Parameters:
- > Accept Web element path as string type
- > Type of element to search target
Return:
No return type
"""
self.get_element_data(text,typeofelement).send_keys(Keys.CONTROL + 'a' + Keys.BACKSPACE)
def openUrl(self,url=''):
"""
Open the url in Selenium Driver Browser
Example:
-> webdriverObject.openUrl()
-> webdriverObject.openUrl('https://www.linkedin.com/')
Parameters:
Accept url as string type
Return:
No return type
"""
self.driver.get(self.BASEURL+url)
def driver_execute_script(self,scriptText):
"""
Excute the Java script in the browser
Example:
webdriverObject.time_sleep(5)
Parameters:
Accept javascipt command as string type
Return:
only if function receive any value from javascript execution
"""
data = self.driver.execute_script(scriptText)
if data is not None:
return data
def driver_quit(self):
"""
Close the Selenium Driver Browser
Example:
webdriverObject.driver_quit()
Parameters:
Accept No Parameters
Return:
No return type
"""
self.driver.quit()
def time_sleep(self,time):
"""
Pause the driver for certian seconds
Example:
webdriverObject.time_sleep(5)
Parameters:
accept time as integer avlue to stop driver as Parameters
Return:
No return type
"""
sleep(time)