Skip to content

Commit 92cc811

Browse files
committed
implement functions in py; adjust tests
1 parent f6b1978 commit 92cc811

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
@@ -60,7 +60,7 @@ def locate_with(by: ByType, using: str) -> "RelativeBy":
6060

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

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

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)