Skip to content

Commit

Permalink
more strict typing on s.w.support.relative_locator
Browse files Browse the repository at this point in the history
  • Loading branch information
pinterior committed Nov 2, 2023
1 parent ddc94fb commit fd834b7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
5 changes: 5 additions & 0 deletions py/selenium/webdriver/common/by.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# under the License.
"""The By implementation."""

from typing import Literal


class By:
"""Set of supported locator strategies."""
Expand All @@ -28,3 +30,6 @@ class By:
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"


ByType = Literal["id", "xpath", "link text", "partial link text", "name", "tag name", "class name", "css selector"]
61 changes: 53 additions & 8 deletions py/selenium/webdriver/support/relative_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
# under the License.
from typing import Dict
from typing import List
from typing import NoReturn
from typing import Optional
from typing import Union
from typing import overload

from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.by import ByType
from selenium.webdriver.remote.webelement import WebElement


Expand All @@ -37,10 +40,10 @@ def with_tag_name(tag_name: str) -> "RelativeBy":
"""
if not tag_name:
raise WebDriverException("tag_name can not be null")
return RelativeBy({"css selector": tag_name})
return RelativeBy({By.CSS_SELECTOR: tag_name})


def locate_with(by: By, using: str) -> "RelativeBy":
def locate_with(by: ByType, using: str) -> "RelativeBy":
"""Start searching for relative objects your search criteria with By.
:Args:
Expand Down Expand Up @@ -70,7 +73,9 @@ class RelativeBy:
assert "mid" in ids
"""

def __init__(self, root: Optional[Dict[Union[By, str], str]] = None, filters: Optional[List] = None):
LocatorType = Dict[ByType, str]

def __init__(self, root: Optional[Dict[ByType, str]] = None, filters: Optional[List] = None):
"""Creates a new RelativeBy object. It is preferred if you use the
`locate_with` method as this signature could change.
Expand All @@ -82,7 +87,15 @@ def __init__(self, root: Optional[Dict[Union[By, str], str]] = None, filters: Op
self.root = root
self.filters = filters or []

def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
@overload
def above(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy":
...

@overload
def above(self, element_or_locator: None = None) -> "NoReturn":
...

def above(self, element_or_locator: Union[WebElement, LocatorType, None] = None) -> "RelativeBy":
"""Add a filter to look for elements above.
:Args:
Expand All @@ -94,7 +107,15 @@ def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
self.filters.append({"kind": "above", "args": [element_or_locator]})
return self

def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
@overload
def below(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy":
...

@overload
def below(self, element_or_locator: None = None) -> "NoReturn":
...

def below(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
"""Add a filter to look for elements below.
:Args:
Expand All @@ -106,7 +127,15 @@ def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
self.filters.append({"kind": "below", "args": [element_or_locator]})
return self

def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
@overload
def to_left_of(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy":
...

@overload
def to_left_of(self, element_or_locator: None = None) -> "NoReturn":
...

def to_left_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
"""Add a filter to look for elements to the left of.
:Args:
Expand All @@ -118,7 +147,15 @@ def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Rel
self.filters.append({"kind": "left", "args": [element_or_locator]})
return self

def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "RelativeBy":
@overload
def to_right_of(self, element_or_locator: Union[WebElement, LocatorType]) -> "RelativeBy":
...

@overload
def to_right_of(self, element_or_locator: None = None) -> "NoReturn":
...

def to_right_of(self, element_or_locator: Union[WebElement, Dict, None] = None) -> "RelativeBy":
"""Add a filter to look for elements right of.
:Args:
Expand All @@ -130,7 +167,15 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Re
self.filters.append({"kind": "right", "args": [element_or_locator]})
return self

def near(self, element_or_locator: Union[WebElement, Dict, None] = None, distance: int = 50) -> "RelativeBy":
@overload
def near(self, element_or_locator: Union[WebElement, LocatorType], distance: int = 50) -> "RelativeBy":
...

@overload
def near(self, element_or_locator: None = None, distance: int = 50) -> "NoReturn":
...

def near(self, element_or_locator: Union[WebElement, LocatorType, None] = None, distance: int = 50) -> "RelativeBy":
"""Add a filter to look for elements near.
:Args:
Expand Down

0 comments on commit fd834b7

Please sign in to comment.