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
|
# 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 urllib
from marionette_driver import expected
from marionette_driver.by import By
from marionette_harness import marionette_test
def inline(doc):
return "data:text/html;charset=utf-8,{}".format(urllib.quote(doc))
static_element = inline("""<p>foo</p>""")
static_elements = static_element + static_element
remove_element_by_tag_name = \
"""var el = document.getElementsByTagName('{}')[0];
document.getElementsByTagName("body")[0].remove(el);"""
hidden_element = inline("<p style='display: none'>hidden</p>")
selected_element = inline("<option selected>selected</option>")
unselected_element = inline("<option>unselected</option>")
enabled_element = inline("<input>")
disabled_element = inline("<input disabled>")
def no_such_element(marionette):
return marionette.find_element(By.ID, "nosuchelement")
def no_such_elements(marionette):
return marionette.find_elements(By.ID, "nosuchelement")
def p(marionette):
return marionette.find_element(By.TAG_NAME, "p")
def ps(marionette):
return marionette.find_elements(By.TAG_NAME, "p")
class TestExpected(marionette_test.MarionetteTestCase):
def test_element_present_func(self):
self.marionette.navigate(static_element)
el = expected.element_present(p)(self.marionette)
self.assertIsNotNone(el)
def test_element_present_locator(self):
self.marionette.navigate(static_element)
el = expected.element_present(By.TAG_NAME, "p")(self.marionette)
self.assertIsNotNone(el)
def test_element_present_not_present(self):
r = expected.element_present(no_such_element)(self.marionette)
self.assertIsInstance(r, bool)
self.assertFalse(r)
def test_element_not_present_func(self):
r = expected.element_not_present(no_such_element)(self.marionette)
self.assertIsInstance(r, bool)
self.assertTrue(r)
def test_element_not_present_locator(self):
r = expected.element_not_present(By.ID, "nosuchelement")(self.marionette)
self.assertIsInstance(r, bool)
self.assertTrue(r)
def test_element_not_present_is_present(self):
self.marionette.navigate(static_element)
r = expected.element_not_present(p)(self.marionette)
self.assertIsInstance(r, bool)
self.assertFalse(r)
def test_element_stale(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
self.assertIsNotNone(el)
self.marionette.execute_script(remove_element_by_tag_name.format("p"))
r = expected.element_stale(el)(self.marionette)
self.assertTrue(r)
def test_element_stale_is_not_stale(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
r = expected.element_stale(el)(self.marionette)
self.assertFalse(r)
def test_elements_present_func(self):
self.marionette.navigate(static_elements)
els = expected.elements_present(ps)(self.marionette)
self.assertEqual(len(els), 2)
def test_elements_present_locator(self):
self.marionette.navigate(static_elements)
els = expected.elements_present(By.TAG_NAME, "p")(self.marionette)
self.assertEqual(len(els), 2)
def test_elements_present_not_present(self):
r = expected.elements_present(no_such_elements)(self.marionette)
self.assertEqual(r, [])
def test_elements_not_present_func(self):
r = expected.element_not_present(no_such_elements)(self.marionette)
self.assertIsInstance(r, bool)
self.assertTrue(r)
def test_elements_not_present_locator(self):
r = expected.element_not_present(By.ID, "nosuchelement")(self.marionette)
self.assertIsInstance(r, bool)
self.assertTrue(r)
def test_elements_not_present_is_present(self):
self.marionette.navigate(static_elements)
r = expected.elements_not_present(ps)(self.marionette)
self.assertIsInstance(r, bool)
self.assertFalse(r)
def test_element_displayed(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
visible = expected.element_displayed(el)(self.marionette)
self.assertTrue(visible)
def test_element_displayed_locator(self):
self.marionette.navigate(static_element)
visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
self.assertTrue(visible)
def test_element_displayed_when_hidden(self):
self.marionette.navigate(hidden_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
visible = expected.element_displayed(el)(self.marionette)
self.assertFalse(visible)
def test_element_displayed_when_hidden_locator(self):
self.marionette.navigate(hidden_element)
visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
self.assertFalse(visible)
def test_element_displayed_when_not_present(self):
self.marionette.navigate("about:blank")
visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
self.assertFalse(visible)
def test_element_displayed_when_stale_element(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
self.marionette.navigate("about:blank")
missing = expected.element_displayed(el)(self.marionette)
self.assertFalse(missing)
def test_element_not_displayed(self):
self.marionette.navigate(hidden_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
hidden = expected.element_not_displayed(el)(self.marionette)
self.assertTrue(hidden)
def test_element_not_displayed_locator(self):
self.marionette.navigate(hidden_element)
hidden = expected.element_not_displayed(By.TAG_NAME, "p")(self.marionette)
self.assertTrue(hidden)
def test_element_not_displayed_when_visible(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
hidden = expected.element_not_displayed(el)(self.marionette)
self.assertFalse(hidden)
def test_element_not_displayed_when_visible_locator(self):
self.marionette.navigate(static_element)
hidden = expected.element_not_displayed(By.TAG_NAME, "p")(self.marionette)
self.assertFalse(hidden)
def test_element_not_displayed_when_stale_element(self):
self.marionette.navigate(static_element)
el = self.marionette.find_element(By.TAG_NAME, "p")
self.marionette.navigate("about:blank")
missing = expected.element_not_displayed(el)(self.marionette)
self.assertTrue(missing)
def test_element_selected(self):
self.marionette.navigate(selected_element)
el = self.marionette.find_element(By.TAG_NAME, "option")
selected = expected.element_selected(el)(self.marionette)
self.assertTrue(selected)
def test_element_selected_when_not_selected(self):
self.marionette.navigate(unselected_element)
el = self.marionette.find_element(By.TAG_NAME, "option")
unselected = expected.element_selected(el)(self.marionette)
self.assertFalse(unselected)
def test_element_not_selected(self):
self.marionette.navigate(unselected_element)
el = self.marionette.find_element(By.TAG_NAME, "option")
unselected = expected.element_not_selected(el)(self.marionette)
self.assertTrue(unselected)
def test_element_not_selected_when_selected(self):
self.marionette.navigate(selected_element)
el = self.marionette.find_element(By.TAG_NAME, "option")
selected = expected.element_not_selected(el)(self.marionette)
self.assertFalse(selected)
def test_element_enabled(self):
self.marionette.navigate(enabled_element)
el = self.marionette.find_element(By.TAG_NAME, "input")
enabled = expected.element_enabled(el)(self.marionette)
self.assertTrue(enabled)
def test_element_enabled_when_disabled(self):
self.marionette.navigate(disabled_element)
el = self.marionette.find_element(By.TAG_NAME, "input")
disabled = expected.element_enabled(el)(self.marionette)
self.assertFalse(disabled)
def test_element_not_enabled(self):
self.marionette.navigate(disabled_element)
el = self.marionette.find_element(By.TAG_NAME, "input")
disabled = expected.element_not_enabled(el)(self.marionette)
self.assertTrue(disabled)
def test_element_not_enabled_when_enabled(self):
self.marionette.navigate(enabled_element)
el = self.marionette.find_element(By.TAG_NAME, "input")
enabled = expected.element_not_enabled(el)(self.marionette)
self.assertFalse(enabled)
|