Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/support/relative_by_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
import pytest
18
19
from selenium.common.exceptions import NoSuchElementException
20
from selenium.webdriver.common.by import By
21
from selenium.webdriver.support.relative_locator import locate_with, with_tag_name
22
23
24
def test_should_be_able_to_find_first_one(driver, pages):
25
pages.load("relative_locators.html")
26
lowest = driver.find_element(By.ID, "below")
27
28
el = driver.find_element(with_tag_name("p").above(lowest))
29
30
assert el.get_attribute("id") == "mid"
31
32
33
def test_should_be_able_to_find_first_one_by_locator(driver, pages):
34
pages.load("relative_locators.html")
35
36
el = driver.find_element(with_tag_name("p").above({By.ID: "below"}))
37
38
assert el.get_attribute("id") == "mid"
39
40
41
def test_should_be_able_to_find_elements_above_another(driver, pages):
42
pages.load("relative_locators.html")
43
lowest = driver.find_element(By.ID, "below")
44
45
elements = driver.find_elements(with_tag_name("p").above(lowest))
46
47
ids = [el.get_attribute("id") for el in elements]
48
assert "above" in ids
49
assert "mid" in ids
50
51
52
def test_should_be_able_to_find_elements_above_another_by_locator(driver, pages):
53
pages.load("relative_locators.html")
54
55
elements = driver.find_elements(with_tag_name("p").above({By.ID: "below"}))
56
57
ids = [el.get_attribute("id") for el in elements]
58
assert "above" in ids
59
assert "mid" in ids
60
61
62
def test_should_be_able_to_combine_filters(driver, pages):
63
pages.load("relative_locators.html")
64
65
elements = driver.find_elements(
66
with_tag_name("td").above(driver.find_element(By.ID, "center")).to_right_of(driver.find_element(By.ID, "top"))
67
)
68
69
ids = [el.get_attribute("id") for el in elements]
70
assert "topRight" in ids
71
72
73
def test_should_be_able_to_combine_filters_by_locator(driver, pages):
74
pages.load("relative_locators.html")
75
76
elements = driver.find_elements(with_tag_name("td").above({By.ID: "center"}).to_right_of({By.ID: "top"}))
77
78
ids = [el.get_attribute("id") for el in elements]
79
assert "topRight" in ids
80
81
82
def test_should_be_able_to_use_css_selectors(driver, pages):
83
pages.load("relative_locators.html")
84
85
elements = driver.find_elements(
86
locate_with(By.CSS_SELECTOR, "td")
87
.above(driver.find_element(By.ID, "center"))
88
.to_right_of(driver.find_element(By.ID, "top"))
89
)
90
91
ids = [el.get_attribute("id") for el in elements]
92
assert "topRight" in ids
93
94
95
def test_should_be_able_to_use_css_selectors_by_locator(driver, pages):
96
pages.load("relative_locators.html")
97
98
elements = driver.find_elements(
99
locate_with(By.CSS_SELECTOR, "td").above({By.ID: "center"}).to_right_of({By.ID: "top"})
100
)
101
102
ids = [el.get_attribute("id") for el in elements]
103
assert "topRight" in ids
104
105
106
def test_should_be_able_to_use_xpath(driver, pages):
107
pages.load("relative_locators.html")
108
109
elements = driver.find_elements(
110
locate_with(By.XPATH, "//td[1]")
111
.below(driver.find_element(By.ID, "top"))
112
.above(driver.find_element(By.ID, "bottomLeft"))
113
)
114
115
ids = [el.get_attribute("id") for el in elements]
116
assert "left" in ids
117
118
119
def test_should_be_able_to_use_xpath_by_locator(driver, pages):
120
pages.load("relative_locators.html")
121
122
elements = driver.find_elements(locate_with(By.XPATH, "//td[1]").below({By.ID: "top"}).above({By.ID: "bottomLeft"}))
123
124
ids = [el.get_attribute("id") for el in elements]
125
assert "left" in ids
126
127
128
def test_should_be_able_to_combine_straight_filters(driver, pages):
129
pages.load("relative_locators.html")
130
131
elements = driver.find_elements(
132
with_tag_name("td")
133
.straight_below(driver.find_element(By.ID, "topRight"))
134
.straight_right_of(driver.find_element(By.ID, "bottomLeft"))
135
)
136
137
ids = [el.get_attribute("id") for el in elements]
138
assert len(ids) == 1
139
assert "bottomRight" in ids
140
141
142
def test_no_such_element_is_raised_rather_than_index_error(driver, pages):
143
pages.load("relative_locators.html")
144
with pytest.raises(NoSuchElementException) as exc:
145
anchor = driver.find_element(By.ID, "top")
146
driver.find_element(locate_with(By.ID, "nonexistentid").above(anchor))
147
assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc.value.msg
148
149
150
def test_no_such_element_is_raised_rather_than_index_error_by_locator(driver, pages):
151
pages.load("relative_locators.html")
152
with pytest.raises(NoSuchElementException) as exc:
153
driver.find_element(locate_with(By.ID, "nonexistentid").above({By.ID: "top"}))
154
assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc.value.msg
155
156
157
def test_near_locator_should_find_near_elements(driver, pages):
158
pages.load("relative_locators.html")
159
rect = driver.find_element(By.ID, "rect1")
160
161
el = driver.find_element(locate_with(By.ID, "rect2").near(rect))
162
163
assert el.get_attribute("id") == "rect2"
164
165
166
def test_near_locator_should_find_near_elements_by_locator(driver, pages):
167
pages.load("relative_locators.html")
168
169
el = driver.find_element(locate_with(By.ID, "rect2").near({By.ID: "rect1"}))
170
171
assert el.get_attribute("id") == "rect2"
172
173
174
def test_near_locator_should_not_find_far_elements(driver, pages):
175
pages.load("relative_locators.html")
176
rect = driver.find_element(By.ID, "rect2")
177
178
with pytest.raises(NoSuchElementException) as exc:
179
driver.find_element(locate_with(By.ID, "rect4").near(rect))
180
181
assert "Cannot locate relative element with: {'id': 'rect4'}" in exc.value.msg
182
183
184
def test_near_locator_should_not_find_far_elements_by_locator(driver, pages):
185
pages.load("relative_locators.html")
186
187
with pytest.raises(NoSuchElementException) as exc:
188
driver.find_element(locate_with(By.ID, "rect4").near({By.ID: "rect2"}))
189
190
assert "Cannot locate relative element with: {'id': 'rect4'}" in exc.value.msg
191
192
193
def test_near_locator_should_find_far_elements(driver, pages):
194
pages.load("relative_locators.html")
195
rect = driver.find_element(By.ID, "rect2")
196
197
el = driver.find_element(locate_with(By.ID, "rect4").near(rect, 100))
198
199
assert el.get_attribute("id") == "rect4"
200
201
202
def test_near_locator_should_find_far_elements_by_locator(driver, pages):
203
pages.load("relative_locators.html")
204
205
el = driver.find_element(locate_with(By.ID, "rect4").near({By.ID: "rect2"}, 100))
206
207
assert el.get_attribute("id") == "rect4"
208
209
210
def test_should_find_elements_above_another(driver, pages):
211
pages.load("relative_locators.html")
212
213
elements = driver.find_elements(with_tag_name("td").above({By.ID: "center"}))
214
215
ids = [el.get_attribute("id") for el in elements]
216
assert len(ids) == 3
217
assert "top" in ids
218
assert "topLeft" in ids
219
assert "topRight" in ids
220
221
222
def test_should_find_elements_below_another(driver, pages):
223
pages.load("relative_locators.html")
224
225
elements = driver.find_elements(with_tag_name("td").below({By.ID: "center"}))
226
227
ids = [el.get_attribute("id") for el in elements]
228
assert len(ids) == 3
229
assert "bottom" in ids
230
assert "bottomLeft" in ids
231
assert "bottomRight" in ids
232
233
234
def test_should_find_elements_left_of_another(driver, pages):
235
pages.load("relative_locators.html")
236
237
elements = driver.find_elements(with_tag_name("td").to_left_of({By.ID: "center"}))
238
239
ids = [el.get_attribute("id") for el in elements]
240
assert len(ids) == 3
241
assert "left" in ids
242
assert "topLeft" in ids
243
assert "bottomLeft" in ids
244
245
246
def test_should_find_elements_right_of_another(driver, pages):
247
pages.load("relative_locators.html")
248
249
elements = driver.find_elements(with_tag_name("td").to_right_of({By.ID: "center"}))
250
251
ids = [el.get_attribute("id") for el in elements]
252
assert len(ids) == 3
253
assert "right" in ids
254
assert "topRight" in ids
255
assert "bottomRight" in ids
256
257
258
def test_should_find_elements_straight_above_another(driver, pages):
259
pages.load("relative_locators.html")
260
261
elements = driver.find_elements(with_tag_name("td").straight_above({By.ID: "bottom"}))
262
263
ids = [el.get_attribute("id") for el in elements]
264
assert len(ids) == 2
265
assert "top" in ids
266
assert "center" in ids
267
268
269
def test_should_find_elements_straight_below_another(driver, pages):
270
pages.load("relative_locators.html")
271
272
elements = driver.find_elements(with_tag_name("td").straight_below({By.ID: "top"}))
273
274
ids = [el.get_attribute("id") for el in elements]
275
assert len(ids) == 2
276
assert "bottom" in ids
277
assert "center" in ids
278
279
280
def test_should_find_elements_straight_left_of_another(driver, pages):
281
pages.load("relative_locators.html")
282
283
elements = driver.find_elements(with_tag_name("td").straight_left_of({By.ID: "right"}))
284
285
ids = [el.get_attribute("id") for el in elements]
286
assert len(ids) == 2
287
assert "left" in ids
288
assert "center" in ids
289
290
291
def test_should_find_elements_straight_right_of_another(driver, pages):
292
pages.load("relative_locators.html")
293
294
elements = driver.find_elements(with_tag_name("td").straight_right_of({By.ID: "left"}))
295
296
ids = [el.get_attribute("id") for el in elements]
297
assert len(ids) == 2
298
assert "right" in ids
299
assert "center" in ids
300
301