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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import errors
import types
from marionette import HTMLElement
"""This file provides a set of expected conditions for common use
cases when writing Marionette tests.
The conditions rely on explicit waits that retries conditions a number
of times until they are either successfully met, or they time out.
"""
class element_present(object):
"""Checks that a web element is present in the DOM of the current
context. This does not necessarily mean that the element is
visible.
You can select which element to be checked for presence by
supplying a locator::
el = Wait(marionette).until(expected.element_present(By.ID, "foo"))
Or by using a function/lambda returning an element::
el = Wait(marionette).\
until(expected.element_present(lambda m: m.find_element(By.ID, "foo")))
:param args: locator or function returning web element
:returns: the web element once it is located, or False
"""
def __init__(self, *args):
if len(args) == 1 and isinstance(args[0], types.FunctionType):
self.locator = args[0]
else:
self.locator = lambda m: m.find_element(*args)
def __call__(self, marionette):
return _find(marionette, self.locator)
class element_not_present(element_present):
"""Checks that a web element is not present in the DOM of the current
context.
You can select which element to be checked for lack of presence by
supplying a locator::
r = Wait(marionette).until(expected.element_not_present(By.ID, "foo"))
Or by using a function/lambda returning an element::
r = Wait(marionette).\
until(expected.element_present(lambda m: m.find_element(By.ID, "foo")))
:param args: locator or function returning web element
:returns: True if element is not present, or False if it is present
"""
def __init__(self, *args):
super(element_not_present, self).__init__(*args)
def __call__(self, marionette):
return not super(element_not_present, self).__call__(marionette)
class element_stale(object):
"""Check that the given element is no longer attached to DOM of the
current context.
This can be useful for waiting until an element is no longer
present.
Sample usage::
el = marionette.find_element(By.ID, "foo")
# ...
Wait(marionette).until(expected.element_stale(el))
:param element: the element to wait for
:returns: False if the element is still attached to the DOM, True
otherwise
"""
def __init__(self, element):
self.el = element
def __call__(self, marionette):
try:
# Calling any method forces a staleness check
self.el.is_enabled()
return False
except errors.StaleElementException:
return True
class elements_present(object):
"""Checks that web elements are present in the DOM of the current
context. This does not necessarily mean that the elements are
visible.
You can select which elements to be checked for presence by
supplying a locator::
els = Wait(marionette).until(expected.elements_present(By.TAG_NAME, "a"))
Or by using a function/lambda returning a list of elements::
els = Wait(marionette).\
until(expected.elements_present(lambda m: m.find_elements(By.TAG_NAME, "a")))
:param args: locator or function returning a list of web elements
:returns: list of web elements once they are located, or False
"""
def __init__(self, *args):
if len(args) == 1 and isinstance(args[0], types.FunctionType):
self.locator = args[0]
else:
self.locator = lambda m: m.find_elements(*args)
def __call__(self, marionette):
return _find(marionette, self.locator)
class elements_not_present(elements_present):
"""Checks that web elements are not present in the DOM of the
current context.
You can select which elements to be checked for not being present
by supplying a locator::
r = Wait(marionette).until(expected.elements_not_present(By.TAG_NAME, "a"))
Or by using a function/lambda returning a list of elements::
r = Wait(marionette).\
until(expected.elements_not_present(lambda m: m.find_elements(By.TAG_NAME, "a")))
:param args: locator or function returning a list of web elements
:returns: True if elements are missing, False if one or more are
present
"""
def __init__(self, *args):
super(elements_not_present, self).__init__(*args)
def __call__(self, marionette):
return not super(elements_not_present, self).__call__(marionette)
class element_displayed(object):
"""An expectation for checking that an element is visible.
Visibility means that the element is not only displayed, but also
has a height and width that is greater than 0 pixels.
Stale elements, meaning elements that have been detached from the
DOM of the current context are treated as not being displayed,
meaning this expectation is not analogous to the behaviour of
calling `is_displayed()` on an `HTMLElement`.
You can select which element to be checked for visibility by
supplying a locator::
displayed = Wait(marionette).until(expected.element_displayed(By.ID, "foo"))
Or by supplying an element::
el = marionette.find_element(By.ID, "foo")
displayed = Wait(marionette).until(expected.element_displayed(el))
:param args: locator or web element
:returns: True if element is displayed, False if hidden
"""
def __init__(self, *args):
self.el = None
if len(args) == 1 and isinstance(args[0], HTMLElement):
self.el = args[0]
else:
self.locator = lambda m: m.find_element(*args)
def __call__(self, marionette):
if self.el is None:
self.el = _find(marionette, self.locator)
if not self.el:
return False
try:
return self.el.is_displayed()
except errors.StaleElementException:
return False
class element_not_displayed(element_displayed):
"""An expectation for checking that an element is not visible.
Visibility means that the element is not only displayed, but also
has a height and width that is greater than 0 pixels.
Stale elements, meaning elements that have been detached fom the
DOM of the current context are treated as not being displayed,
meaning this expectation is not analogous to the behaviour of
calling `is_displayed()` on an `HTMLElement`.
You can select which element to be checked for visibility by
supplying a locator::
hidden = Wait(marionette).until(expected.element_not_displayed(By.ID, "foo"))
Or by supplying an element::
el = marionette.find_element(By.ID, "foo")
hidden = Wait(marionette).until(expected.element_not_displayed(el))
:param args: locator or web element
:returns: True if element is hidden, False if displayed
"""
def __init__(self, *args):
super(element_not_displayed, self).__init__(*args)
def __call__(self, marionette):
return not super(element_not_displayed, self).__call__(marionette)
class element_selected(object):
"""An expectation for checking that the given element is selected.
:param element: the element to be selected
:returns: True if element is selected, False otherwise
"""
def __init__(self, element):
self.el = element
def __call__(self, marionette):
return self.el.is_selected()
class element_not_selected(element_selected):
"""An expectation for checking that the given element is not
selected.
:param element: the element to not be selected
:returns: True if element is not selected, False if selected
"""
def __init__(self, element):
super(element_not_selected, self).__init__(element)
def __call__(self, marionette):
return not super(element_not_selected, self).__call__(marionette)
class element_enabled(object):
"""An expectation for checking that the given element is enabled.
:param element: the element to check if enabled
:returns: True if element is enabled, False otherwise
"""
def __init__(self, element):
self.el = element
def __call__(self, marionette):
return self.el.is_enabled()
class element_not_enabled(element_enabled):
"""An expectation for checking that the given element is disabled.
:param element: the element to check if disabled
:returns: True if element is disabled, False if enabled
"""
def __init__(self, element):
super(element_not_enabled, self).__init__(element)
def __call__(self, marionette):
return not super(element_not_enabled, self).__call__(marionette)
def _find(marionette, func):
el = None
try:
el = func(marionette)
except errors.NoSuchElementException:
pass
if el is None:
return False
return el
|