1
- import settings
1
+ from selenium .common .exceptions import (
2
+ NoSuchElementException ,
3
+ StaleElementReferenceException ,
4
+ TimeoutException ,
5
+ )
6
+ from selenium .webdriver .support import expected_conditions as EC
7
+ from selenium .webdriver .support .ui import WebDriverWait
2
8
9
+ import settings
3
10
from base import expected_conditions as ec
4
- from selenium .webdriver .support .ui import WebDriverWait
5
- from selenium .webdriver .support import expected_conditions as EC
6
- from selenium .common .exceptions import StaleElementReferenceException , TimeoutException , NoSuchElementException
11
+
7
12
8
13
class WebElementWrapper :
9
14
"""A wrapper for selenium's WebElement. Supports all WebElement attributes
@@ -13,6 +18,7 @@ class WebElementWrapper:
13
18
:param str attribute_name: The attribute name of the locator in its containing class.
14
19
:param locator: An object of the type Locator.
15
20
"""
21
+
16
22
def __init__ (self , driver , attribute_name , locator ):
17
23
self .driver = driver
18
24
self .locator = locator
@@ -90,8 +96,7 @@ def click_expecting_popup(self, timeout=settings.TIMEOUT):
90
96
self .click ()
91
97
92
98
try :
93
- WebDriverWait (self .driver , timeout ).until (
94
- EC .number_of_windows_to_be (2 ))
99
+ WebDriverWait (self .driver , timeout ).until (EC .number_of_windows_to_be (2 ))
95
100
except TimeoutException :
96
101
raise ValueError ('No new window was opened.' )
97
102
self .driver .close ()
@@ -106,13 +111,15 @@ def send_keys_deliberately(self, keys):
106
111
for k in keys :
107
112
self .element .send_keys (k )
108
113
114
+
109
115
class BaseLocator :
110
116
"""Abstract base class from which all Locator classes inherit.
111
117
112
118
Includes the method of how to locate the element (a subclass of selenium By),
113
119
a string that actually identifies the element, and a timeout for how long to wait
114
120
when searching for the element.
115
121
"""
122
+
116
123
def __init__ (self , selector , path , timeout = settings .TIMEOUT ):
117
124
self .selector = selector
118
125
self .path = path
@@ -135,6 +142,7 @@ class Locator(BaseLocator):
135
142
most notably `get_web_element`. You may end up waiting longer than your timeout because some
136
143
methods use more than one Wait.
137
144
"""
145
+
138
146
def get_web_element (self , driver , attribute_name ):
139
147
"""
140
148
Check if element is on page and visible before returning the selenium
@@ -150,39 +158,54 @@ def get_web_element(self, driver, attribute_name):
150
158
WebDriverWait (driver , self .timeout ).until (
151
159
EC .presence_of_element_located (self .location )
152
160
)
153
- except (TimeoutException , StaleElementReferenceException ):
154
- raise ValueError ('Element {} not present on page. {}' .format (
155
- attribute_name , driver .current_url )) from None
161
+ except (TimeoutException , StaleElementReferenceException ):
162
+ raise ValueError (
163
+ 'Element {} not present on page. {}' .format (
164
+ attribute_name , driver .current_url
165
+ )
166
+ ) from None
156
167
157
168
try :
158
169
WebDriverWait (driver , self .timeout ).until (
159
170
EC .visibility_of_element_located (self .location )
160
171
)
161
- except (TimeoutException , StaleElementReferenceException ):
162
- raise ValueError ('Element {} not visible before timeout. {}' .format (
163
- attribute_name , driver .current_url )) from None
172
+ except (TimeoutException , StaleElementReferenceException ):
173
+ raise ValueError (
174
+ 'Element {} not visible before timeout. {}' .format (
175
+ attribute_name , driver .current_url
176
+ )
177
+ ) from None
164
178
165
179
try :
166
180
WebDriverWait (driver , self .timeout ).until (
167
181
EC .element_to_be_clickable (self .location )
168
182
)
169
- except (TimeoutException , StaleElementReferenceException ):
170
- raise ValueError ('Element {} not clickable before timeout. {}' .format (
171
- attribute_name , driver .current_url )) from None
183
+ except (TimeoutException , StaleElementReferenceException ):
184
+ raise ValueError (
185
+ 'Element {} not clickable before timeout. {}' .format (
186
+ attribute_name , driver .current_url
187
+ )
188
+ ) from None
172
189
173
190
if 'href' in attribute_name :
174
191
try :
175
192
WebDriverWait (driver , self .timeout ).until (
176
193
ec .link_has_href (self .location )
177
194
)
178
- except (TimeoutException , StaleElementReferenceException ):
179
- raise ValueError ('Element {} on page but does not have a href. {}' .format (
180
- attribute_name , driver .current_url )) from None
195
+ except (TimeoutException , StaleElementReferenceException ):
196
+ raise ValueError (
197
+ 'Element {} on page but does not have a href. {}' .format (
198
+ attribute_name , driver .current_url
199
+ )
200
+ ) from None
181
201
try :
182
202
return driver .find_element (self .selector , self .path )
183
203
except NoSuchElementException :
184
- raise ValueError ('Element {} was present, but now is gone. {}' .format (
185
- attribute_name , driver .current_url )) from None
204
+ raise ValueError (
205
+ 'Element {} was present, but now is gone. {}' .format (
206
+ attribute_name , driver .current_url
207
+ )
208
+ ) from None
186
209
187
210
def get_element (self , driver , attribute_name ):
188
211
return WebElementWrapper (driver , attribute_name , self )
@@ -195,6 +218,7 @@ class GroupLocator(BaseLocator):
195
218
:param str path: Identifying string that is shared between the elements
196
219
you are attempting to locate.
197
220
"""
221
+
198
222
def get_web_elements (self , driver ):
199
223
return driver .find_elements (self .selector , self .path )
200
224
@@ -214,7 +238,10 @@ class ComponentLocator(Locator):
214
238
:param component_class: A subclass of BaseElment.
215
239
Note: Currently the parameters selector, path, and timeout don't do anything.
216
240
"""
217
- def __init__ (self , component_class , selector = None , path = None , timeout = settings .TIMEOUT ):
241
+
242
+ def __init__ (
243
+ self , component_class , selector = None , path = None , timeout = settings .TIMEOUT
244
+ ):
218
245
super ().__init__ (selector , path , timeout )
219
246
self .component_class = component_class
220
247
@@ -227,6 +254,7 @@ class BaseElement:
227
254
Handles waffled pages, storage of the WebDriver, and returning WebElements when Locators are
228
255
accessed.
229
256
"""
257
+
230
258
default_timeout = settings .TIMEOUT
231
259
232
260
def __new__ (cls , * args , ** kwargs ):
0 commit comments