@@ -31,6 +31,14 @@ def test_should_be_able_to_find_first_one(driver, pages):
31
31
assert el .get_attribute ("id" ) == "mid"
32
32
33
33
34
+ def test_should_be_able_to_find_first_one_by_locator (driver , pages ):
35
+ pages .load ("relative_locators.html" )
36
+
37
+ el = driver .find_element (with_tag_name ("p" ).above ({By .ID : "below" }))
38
+
39
+ assert el .get_attribute ("id" ) == "mid"
40
+
41
+
34
42
def test_should_be_able_to_find_elements_above_another (driver , pages ):
35
43
pages .load ("relative_locators.html" )
36
44
lowest = driver .find_element (By .ID , "below" )
@@ -42,6 +50,16 @@ def test_should_be_able_to_find_elements_above_another(driver, pages):
42
50
assert "mid" in ids
43
51
44
52
53
+ def test_should_be_able_to_find_elements_above_another_by_locator (driver , pages ):
54
+ pages .load ("relative_locators.html" )
55
+
56
+ elements = driver .find_elements (with_tag_name ("p" ).above ({By .ID : "below" }))
57
+
58
+ ids = [el .get_attribute ("id" ) for el in elements ]
59
+ assert "above" in ids
60
+ assert "mid" in ids
61
+
62
+
45
63
def test_should_be_able_to_combine_filters (driver , pages ):
46
64
pages .load ("relative_locators.html" )
47
65
@@ -55,6 +73,15 @@ def test_should_be_able_to_combine_filters(driver, pages):
55
73
assert "third" in ids
56
74
57
75
76
+ def test_should_be_able_to_combine_filters_by_locator (driver , pages ):
77
+ pages .load ("relative_locators.html" )
78
+
79
+ elements = driver .find_elements (with_tag_name ("td" ).above ({By .ID : "center" }).to_right_of ({By .ID : "second" }))
80
+
81
+ ids = [el .get_attribute ("id" ) for el in elements ]
82
+ assert "third" in ids
83
+
84
+
58
85
def test_should_be_able_to_use_css_selectors (driver , pages ):
59
86
pages .load ("relative_locators.html" )
60
87
@@ -68,6 +95,17 @@ def test_should_be_able_to_use_css_selectors(driver, pages):
68
95
assert "third" in ids
69
96
70
97
98
+ def test_should_be_able_to_use_css_selectors_by_locator (driver , pages ):
99
+ pages .load ("relative_locators.html" )
100
+
101
+ elements = driver .find_elements (
102
+ locate_with (By .CSS_SELECTOR , "td" ).above ({By .ID : "center" }).to_right_of ({By .ID : "second" })
103
+ )
104
+
105
+ ids = [el .get_attribute ("id" ) for el in elements ]
106
+ assert "third" in ids
107
+
108
+
71
109
def test_should_be_able_to_use_xpath (driver , pages ):
72
110
pages .load ("relative_locators.html" )
73
111
@@ -81,6 +119,15 @@ def test_should_be_able_to_use_xpath(driver, pages):
81
119
assert "fourth" in ids
82
120
83
121
122
+ def test_should_be_able_to_use_xpath_by_locator (driver , pages ):
123
+ pages .load ("relative_locators.html" )
124
+
125
+ elements = driver .find_elements (locate_with (By .XPATH , "//td[1]" ).below ({By .ID : "second" }).above ({By .ID : "seventh" }))
126
+
127
+ ids = [el .get_attribute ("id" ) for el in elements ]
128
+ assert "fourth" in ids
129
+
130
+
84
131
def test_no_such_element_is_raised_rather_than_index_error (driver , pages ):
85
132
pages .load ("relative_locators.html" )
86
133
with pytest .raises (NoSuchElementException ) as exc :
@@ -89,6 +136,13 @@ def test_no_such_element_is_raised_rather_than_index_error(driver, pages):
89
136
assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc .value .msg
90
137
91
138
139
+ def test_no_such_element_is_raised_rather_than_index_error_by_locator (driver , pages ):
140
+ pages .load ("relative_locators.html" )
141
+ with pytest .raises (NoSuchElementException ) as exc :
142
+ driver .find_element (locate_with (By .ID , "nonexistentid" ).above ({By .ID : "second" }))
143
+ assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc .value .msg
144
+
145
+
92
146
def test_near_locator_should_find_near_elements (driver , pages ):
93
147
pages .load ("relative_locators.html" )
94
148
rect1 = driver .find_element (By .ID , "rect1" )
@@ -98,6 +152,14 @@ def test_near_locator_should_find_near_elements(driver, pages):
98
152
assert el .get_attribute ("id" ) == "rect2"
99
153
100
154
155
+ def test_near_locator_should_find_near_elements_by_locator (driver , pages ):
156
+ pages .load ("relative_locators.html" )
157
+
158
+ el = driver .find_element (locate_with (By .ID , "rect2" ).near ({By .ID : "rect1" }))
159
+
160
+ assert el .get_attribute ("id" ) == "rect2"
161
+
162
+
101
163
def test_near_locator_should_not_find_far_elements (driver , pages ):
102
164
pages .load ("relative_locators.html" )
103
165
rect3 = driver .find_element (By .ID , "rect3" )
@@ -106,3 +168,29 @@ def test_near_locator_should_not_find_far_elements(driver, pages):
106
168
driver .find_element (locate_with (By .ID , "rect4" ).near (rect3 ))
107
169
108
170
assert "Cannot locate relative element with: {'id': 'rect4'}" in exc .value .msg
171
+
172
+
173
+ def test_near_locator_should_not_find_far_elements_by_locator (driver , pages ):
174
+ pages .load ("relative_locators.html" )
175
+
176
+ with pytest .raises (NoSuchElementException ) as exc :
177
+ driver .find_element (locate_with (By .ID , "rect4" ).near ({By .ID : "rect3" }))
178
+
179
+ assert "Cannot locate relative element with: {'id': 'rect4'}" in exc .value .msg
180
+
181
+
182
+ def test_near_locator_should_find_far_elements (driver , pages ):
183
+ pages .load ("relative_locators.html" )
184
+ rect3 = driver .find_element (By .ID , "rect3" )
185
+
186
+ el = driver .find_element (locate_with (By .ID , "rect4" ).near (rect3 , 100 ))
187
+
188
+ assert el .get_attribute ("id" ) == "rect4"
189
+
190
+
191
+ def test_near_locator_should_find_far_elements_by_locator (driver , pages ):
192
+ pages .load ("relative_locators.html" )
193
+
194
+ el = driver .find_element (locate_with (By .ID , "rect4" ).near ({By .ID : "rect3" }, 100 ))
195
+
196
+ assert el .get_attribute ("id" ) == "rect4"
0 commit comments