@@ -31,6 +31,14 @@ def test_should_be_able_to_find_first_one(driver, pages):
3131 assert el .get_attribute ("id" ) == "mid"
3232
3333
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+
3442def test_should_be_able_to_find_elements_above_another (driver , pages ):
3543 pages .load ("relative_locators.html" )
3644 lowest = driver .find_element (By .ID , "below" )
@@ -42,6 +50,16 @@ def test_should_be_able_to_find_elements_above_another(driver, pages):
4250 assert "mid" in ids
4351
4452
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+
4563def test_should_be_able_to_combine_filters (driver , pages ):
4664 pages .load ("relative_locators.html" )
4765
@@ -55,6 +73,15 @@ def test_should_be_able_to_combine_filters(driver, pages):
5573 assert "third" in ids
5674
5775
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+
5885def test_should_be_able_to_use_css_selectors (driver , pages ):
5986 pages .load ("relative_locators.html" )
6087
@@ -68,6 +95,17 @@ def test_should_be_able_to_use_css_selectors(driver, pages):
6895 assert "third" in ids
6996
7097
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+
71109def test_should_be_able_to_use_xpath (driver , pages ):
72110 pages .load ("relative_locators.html" )
73111
@@ -81,6 +119,15 @@ def test_should_be_able_to_use_xpath(driver, pages):
81119 assert "fourth" in ids
82120
83121
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+
84131def test_no_such_element_is_raised_rather_than_index_error (driver , pages ):
85132 pages .load ("relative_locators.html" )
86133 with pytest .raises (NoSuchElementException ) as exc :
@@ -89,6 +136,13 @@ def test_no_such_element_is_raised_rather_than_index_error(driver, pages):
89136 assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc .value .msg
90137
91138
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+
92146def test_near_locator_should_find_near_elements (driver , pages ):
93147 pages .load ("relative_locators.html" )
94148 rect1 = driver .find_element (By .ID , "rect1" )
@@ -98,6 +152,14 @@ def test_near_locator_should_find_near_elements(driver, pages):
98152 assert el .get_attribute ("id" ) == "rect2"
99153
100154
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+
101163def test_near_locator_should_not_find_far_elements (driver , pages ):
102164 pages .load ("relative_locators.html" )
103165 rect3 = driver .find_element (By .ID , "rect3" )
@@ -106,3 +168,29 @@ def test_near_locator_should_not_find_far_elements(driver, pages):
106168 driver .find_element (locate_with (By .ID , "rect4" ).near (rect3 ))
107169
108170 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