@@ -34,6 +34,7 @@ def title_is(title):
34
34
"""An expectation for checking the title of a page.
35
35
title is the expected title, which must be an exact match
36
36
returns True if the title matches, false otherwise."""
37
+
37
38
def _predicate (driver ):
38
39
return driver .title == title
39
40
@@ -45,6 +46,7 @@ def title_contains(title):
45
46
substring. title is the fragment of title expected
46
47
returns True when the title matches, False otherwise
47
48
"""
49
+
48
50
def _predicate (driver ):
49
51
return title in driver .title
50
52
@@ -57,6 +59,7 @@ def presence_of_element_located(locator):
57
59
locator - used to find the element
58
60
returns the WebElement once it is located
59
61
"""
62
+
60
63
def _predicate (driver ):
61
64
return driver .find_element (* locator )
62
65
@@ -69,6 +72,7 @@ def url_contains(url):
69
72
url is the fragment of url expected,
70
73
returns True when the url matches, False otherwise
71
74
"""
75
+
72
76
def _predicate (driver ):
73
77
return url in driver .current_url
74
78
@@ -79,6 +83,7 @@ def url_matches(pattern):
79
83
"""An expectation for checking the current url.
80
84
pattern is the expected pattern, which must be an exact match
81
85
returns True if the url matches, false otherwise."""
86
+
82
87
def _predicate (driver ):
83
88
return bool (re .search (pattern , driver .current_url ))
84
89
@@ -89,6 +94,7 @@ def url_to_be(url):
89
94
"""An expectation for checking the current url.
90
95
url is the expected url, which must be an exact match
91
96
returns True if the url matches, false otherwise."""
97
+
92
98
def _predicate (driver ):
93
99
return url == driver .current_url
94
100
@@ -99,6 +105,7 @@ def url_changes(url):
99
105
"""An expectation for checking the current url.
100
106
url is the expected url, which must not be an exact match
101
107
returns True if the url is different, false otherwise."""
108
+
102
109
def _predicate (driver ):
103
110
return url != driver .current_url
104
111
@@ -112,6 +119,7 @@ def visibility_of_element_located(locator):
112
119
locator - used to find the element
113
120
returns the WebElement once it is located and visible
114
121
"""
122
+
115
123
def _predicate (driver ):
116
124
try :
117
125
return _element_if_visible (driver .find_element (* locator ))
@@ -128,6 +136,7 @@ def visibility_of(element):
128
136
element is the WebElement
129
137
returns the (same) WebElement once it is visible
130
138
"""
139
+
131
140
def _predicate (_ ):
132
141
return _element_if_visible (element )
133
142
@@ -144,6 +153,7 @@ def presence_of_all_elements_located(locator):
144
153
locator is used to find the element
145
154
returns the list of WebElements once they are located
146
155
"""
156
+
147
157
def _predicate (driver ):
148
158
return driver .find_elements (* locator )
149
159
@@ -156,6 +166,7 @@ def visibility_of_any_elements_located(locator):
156
166
locator is used to find the element
157
167
returns the list of WebElements once they are located
158
168
"""
169
+
159
170
def _predicate (driver ):
160
171
return [element for element in driver .find_elements (* locator ) if _element_if_visible (element )]
161
172
@@ -169,6 +180,7 @@ def visibility_of_all_elements_located(locator):
169
180
locator - used to find the elements
170
181
returns the list of WebElements once they are located and visible
171
182
"""
183
+
172
184
def _predicate (driver ):
173
185
try :
174
186
elements = driver .find_elements (* locator )
@@ -187,6 +199,7 @@ def text_to_be_present_in_element(locator, text_):
187
199
specified element.
188
200
locator, text
189
201
"""
202
+
190
203
def _predicate (driver ):
191
204
try :
192
205
element_text = driver .find_element (* locator ).text
@@ -202,6 +215,7 @@ def text_to_be_present_in_element_value(locator, text_):
202
215
An expectation for checking if the given text is present in the element's
203
216
locator, text
204
217
"""
218
+
205
219
def _predicate (driver ):
206
220
try :
207
221
element_text = driver .find_element (* locator ).get_attribute ("value" )
@@ -217,6 +231,7 @@ def frame_to_be_available_and_switch_to_it(locator):
217
231
switch to. If the frame is available it switches the given driver to the
218
232
specified frame.
219
233
"""
234
+
220
235
def _predicate (driver ):
221
236
try :
222
237
if hasattr (locator , '__iter__' ):
@@ -236,6 +251,7 @@ def invisibility_of_element_located(locator):
236
251
237
252
locator used to find the element
238
253
"""
254
+
239
255
def _predicate (driver ):
240
256
try :
241
257
target = locator
@@ -262,13 +278,23 @@ def invisibility_of_element(element):
262
278
return invisibility_of_element_located (element )
263
279
264
280
265
- def element_to_be_clickable (locator ):
266
- """ An Expectation for checking an element is visible and enabled such that
267
- you can click it."""
281
+ def element_to_be_clickable (mark ):
282
+ """
283
+ An Expectation for checking an element is visible and enabled such that
284
+ you can click it.
285
+
286
+ element is either a locator (text) or an WebElement
287
+ """
288
+
289
+ # renamed argument to 'mark', to indicate that both locator
290
+ # and WebElement args are valid
268
291
def _predicate (driver ):
269
- element = visibility_of_element_located (locator )(driver )
270
- if element and element .is_enabled ():
271
- return element
292
+ target = mark
293
+ if not isinstance (target , WebElement ): # if given locator instead of WebElement
294
+ target = driver .find_element (* target ) # grab element at locator
295
+ target = visibility_of (target )(driver )
296
+ if target and target .is_enabled ():
297
+ return target
272
298
else :
273
299
return False
274
300
@@ -280,6 +306,7 @@ def staleness_of(element):
280
306
element is the element to wait for.
281
307
returns False if the element is still attached to the DOM, true otherwise.
282
308
"""
309
+
283
310
def _predicate (_ ):
284
311
try :
285
312
# Calling any method forces a staleness check
@@ -295,6 +322,7 @@ def element_to_be_selected(element):
295
322
""" An expectation for checking the selection is selected.
296
323
element is WebElement object
297
324
"""
325
+
298
326
def _predicate (_ ):
299
327
return element .is_selected ()
300
328
@@ -304,6 +332,7 @@ def _predicate(_):
304
332
def element_located_to_be_selected (locator ):
305
333
"""An expectation for the element to be located is selected.
306
334
locator is a tuple of (by, path)"""
335
+
307
336
def _predicate (driver ):
308
337
return driver .find_element (* locator ).is_selected ()
309
338
@@ -315,6 +344,7 @@ def element_selection_state_to_be(element, is_selected):
315
344
element is WebElement object
316
345
is_selected is a Boolean."
317
346
"""
347
+
318
348
def _predicate (_ ):
319
349
return element .is_selected () == is_selected
320
350
@@ -327,6 +357,7 @@ def element_located_selection_state_to_be(locator, is_selected):
327
357
locator is a tuple of (by, path)
328
358
is_selected is a boolean
329
359
"""
360
+
330
361
def _predicate (driver ):
331
362
try :
332
363
element = driver .find_element (* locator )
@@ -339,6 +370,7 @@ def _predicate(driver):
339
370
340
371
def number_of_windows_to_be (num_windows ):
341
372
""" An expectation for the number of windows to be a certain value."""
373
+
342
374
def _predicate (driver ):
343
375
return len (driver .window_handles ) == num_windows
344
376
@@ -348,6 +380,7 @@ def _predicate(driver):
348
380
def new_window_is_opened (current_handles ):
349
381
""" An expectation that a new window will be opened and have the number of
350
382
windows handles increase"""
383
+
351
384
def _predicate (driver ):
352
385
return len (driver .window_handles ) > len (current_handles )
353
386
@@ -369,6 +402,7 @@ def element_attribute_to_include(locator, attribute_):
369
402
specified element.
370
403
locator, attribute
371
404
"""
405
+
372
406
def _predicate (driver ):
373
407
try :
374
408
element_attribute = driver .find_element (* locator ).get_attribute (attribute_ )
@@ -383,6 +417,7 @@ def any_of(*expected_conditions):
383
417
""" An expectation that any of multiple expected conditions is true.
384
418
Equivalent to a logical 'OR'.
385
419
Returns results of the first matching condition, or False if none do. """
420
+
386
421
def any_of_condition (driver ):
387
422
for expected_condition in expected_conditions :
388
423
try :
@@ -392,6 +427,7 @@ def any_of_condition(driver):
392
427
except WebDriverException :
393
428
pass
394
429
return False
430
+
395
431
return any_of_condition
396
432
397
433
@@ -400,6 +436,7 @@ def all_of(*expected_conditions):
400
436
Equivalent to a logical 'AND'.
401
437
Returns: When any ExpectedCondition is not met: False.
402
438
When all ExpectedConditions are met: A List with each ExpectedCondition's return value. """
439
+
403
440
def all_of_condition (driver ):
404
441
results = []
405
442
for expected_condition in expected_conditions :
@@ -411,13 +448,15 @@ def all_of_condition(driver):
411
448
except WebDriverException :
412
449
return False
413
450
return results
451
+
414
452
return all_of_condition
415
453
416
454
417
455
def none_of (* expected_conditions ):
418
456
""" An expectation that none of 1 or multiple expected conditions is true.
419
457
Equivalent to a logical 'NOT-OR'.
420
458
Returns a Boolean """
459
+
421
460
def none_of_condition (driver ):
422
461
for expected_condition in expected_conditions :
423
462
try :
@@ -427,4 +466,5 @@ def none_of_condition(driver):
427
466
except WebDriverException :
428
467
pass
429
468
return True
469
+
430
470
return none_of_condition
0 commit comments