Skip to content

Commit 0918d42

Browse files
AdamPDottypujagani
authored andcommitted
implement functions in py; adjust tests
1 parent f998a5f commit 0918d42

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

py/selenium/webdriver/support/relative_locator.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def locate_with(by: ByType, using: str) -> "RelativeBy":
6161

6262
class RelativeBy:
6363
"""Gives the opportunity to find elements based on their relative location
64-
on the page from a root elelemt. It is recommended that you use the helper
64+
on the page from a root element. It is recommended that you use the helper
6565
function to create it.
6666
6767
Example:
@@ -160,6 +160,78 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict, None] = None)
160160
self.filters.append({"kind": "right", "args": [element_or_locator]})
161161
return self
162162

163+
@overload
164+
def straight_above(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy": ...
165+
166+
@overload
167+
def straight_above(self, element_or_locator: None = None) -> "NoReturn": ...
168+
169+
def straight_above(self, element_or_locator: Union[WebElement, LocatorType, None] = None) -> "RelativeBy":
170+
"""Add a filter to look for elements above.
171+
172+
:Args:
173+
- element_or_locator: Element to look above
174+
"""
175+
if not element_or_locator:
176+
raise WebDriverException("Element or locator must be given when calling above method")
177+
178+
self.filters.append({"kind": "straightAbove", "args": [element_or_locator]})
179+
return self
180+
181+
@overload
182+
def straight_below(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy": ...
183+
184+
@overload
185+
def straight_below(self, element_or_locator: None = None) -> "NoReturn": ...
186+
187+
def straight_below(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
188+
"""Add a filter to look for elements below.
189+
190+
:Args:
191+
- element_or_locator: Element to look below
192+
"""
193+
if not element_or_locator:
194+
raise WebDriverException("Element or locator must be given when calling below method")
195+
196+
self.filters.append({"kind": "straightBelow", "args": [element_or_locator]})
197+
return self
198+
199+
@overload
200+
def straight_left_of(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy": ...
201+
202+
@overload
203+
def straight_left_of(self, element_or_locator: None = None) -> "NoReturn": ...
204+
205+
def straight_left_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
206+
"""Add a filter to look for elements to the left of.
207+
208+
:Args:
209+
- element_or_locator: Element to look to the left of
210+
"""
211+
if not element_or_locator:
212+
raise WebDriverException("Element or locator must be given when calling to_left_of method")
213+
214+
self.filters.append({"kind": "straightLeft", "args": [element_or_locator]})
215+
return self
216+
217+
@overload
218+
def straight_right_of(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy": ...
219+
220+
@overload
221+
def straight_right_of(self, element_or_locator: None = None) -> "NoReturn": ...
222+
223+
def straight_right_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
224+
"""Add a filter to look for elements right of.
225+
226+
:Args:
227+
- element_or_locator: Element to look right of
228+
"""
229+
if not element_or_locator:
230+
raise WebDriverException("Element or locator must be given when calling to_right_of method")
231+
232+
self.filters.append({"kind": "straightRight", "args": [element_or_locator]})
233+
return self
234+
163235
@overload
164236
def near(self, element_or_locator: Union[WebElement, LocatorType], distance: int = 50) -> "RelativeBy": ...
165237

py/test/selenium/webdriver/support/relative_by_tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_should_be_able_to_combine_straight_filters(driver, pages):
133133

134134
elements = driver.find_elements(
135135
with_tag_name("td")
136-
.straightBelow(driver.find_element(By.ID, "topRight"))
136+
.straight_below(driver.find_element(By.ID, "topRight"))
137137
.straight_to_right_of(driver.find_element(By.ID, "bottomLeft"))
138138
)
139139

@@ -261,7 +261,7 @@ def test_should_find_elements_right_of_another(driver, pages):
261261
def test_should_find_elements_straight_above_another(driver, pages):
262262
pages.load("relative_locators.html")
263263

264-
elements = driver.find_elements(with_tag_name("td").above({By.ID: "bottom"}))
264+
elements = driver.find_elements(with_tag_name("td").straight_above({By.ID: "bottom"}))
265265

266266
ids = [el.get_attribute("id") for el in elements]
267267
assert len(ids) == 2
@@ -272,7 +272,7 @@ def test_should_find_elements_straight_above_another(driver, pages):
272272
def test_should_find_elements_straight_below_another(driver, pages):
273273
pages.load("relative_locators.html")
274274

275-
elements = driver.find_elements(with_tag_name("td").below({By.ID: "top"}))
275+
elements = driver.find_elements(with_tag_name("td").straight_below({By.ID: "top"}))
276276

277277
ids = [el.get_attribute("id") for el in elements]
278278
assert len(ids) == 2
@@ -283,7 +283,7 @@ def test_should_find_elements_straight_below_another(driver, pages):
283283
def test_should_find_elements_straight_left_of_another(driver, pages):
284284
pages.load("relative_locators.html")
285285

286-
elements = driver.find_elements(with_tag_name("td").to_left_of({By.ID: "right"}))
286+
elements = driver.find_elements(with_tag_name("td").straight_left_of({By.ID: "right"}))
287287

288288
ids = [el.get_attribute("id") for el in elements]
289289
assert len(ids) == 2
@@ -294,7 +294,7 @@ def test_should_find_elements_straight_left_of_another(driver, pages):
294294
def test_should_find_elements_straight_right_of_another(driver, pages):
295295
pages.load("relative_locators.html")
296296

297-
elements = driver.find_elements(with_tag_name("td").to_right_of({By.ID: "left"}))
297+
elements = driver.find_elements(with_tag_name("td").straight_right_of({By.ID: "left"}))
298298

299299
ids = [el.get_attribute("id") for el in elements]
300300
assert len(ids) == 2

0 commit comments

Comments
 (0)