16
16
# under the License.
17
17
from typing import Dict
18
18
from typing import List
19
+ from typing import NoReturn
19
20
from typing import Optional
20
21
from typing import Union
22
+ from typing import overload
21
23
22
24
from selenium .common .exceptions import WebDriverException
23
25
from selenium .webdriver .common .by import By
26
+ from selenium .webdriver .common .by import ByType
24
27
from selenium .webdriver .remote .webelement import WebElement
25
28
26
29
@@ -37,10 +40,10 @@ def with_tag_name(tag_name: str) -> "RelativeBy":
37
40
"""
38
41
if not tag_name :
39
42
raise WebDriverException ("tag_name can not be null" )
40
- return RelativeBy ({"css selector" : tag_name })
43
+ return RelativeBy ({By . CSS_SELECTOR : tag_name })
41
44
42
45
43
- def locate_with (by : By , using : str ) -> "RelativeBy" :
46
+ def locate_with (by : ByType , using : str ) -> "RelativeBy" :
44
47
"""Start searching for relative objects your search criteria with By.
45
48
46
49
:Args:
@@ -70,7 +73,9 @@ class RelativeBy:
70
73
assert "mid" in ids
71
74
"""
72
75
73
- def __init__ (self , root : Optional [Dict [Union [By , str ], str ]] = None , filters : Optional [List ] = None ):
76
+ LocatorType = Dict [ByType , str ]
77
+
78
+ def __init__ (self , root : Optional [Dict [ByType , str ]] = None , filters : Optional [List ] = None ):
74
79
"""Creates a new RelativeBy object. It is preferred if you use the
75
80
`locate_with` method as this signature could change.
76
81
@@ -82,7 +87,13 @@ def __init__(self, root: Optional[Dict[Union[By, str], str]] = None, filters: Op
82
87
self .root = root
83
88
self .filters = filters or []
84
89
85
- def above (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
90
+ @overload
91
+ def above (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
92
+
93
+ @overload
94
+ def above (self , element_or_locator : None = None ) -> "NoReturn" : ...
95
+
96
+ def above (self , element_or_locator : Union [WebElement , LocatorType , None ] = None ) -> "RelativeBy" :
86
97
"""Add a filter to look for elements above.
87
98
88
99
:Args:
@@ -94,7 +105,13 @@ def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
94
105
self .filters .append ({"kind" : "above" , "args" : [element_or_locator ]})
95
106
return self
96
107
97
- def below (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
108
+ @overload
109
+ def below (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
110
+
111
+ @overload
112
+ def below (self , element_or_locator : None = None ) -> "NoReturn" : ...
113
+
114
+ def below (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
98
115
"""Add a filter to look for elements below.
99
116
100
117
:Args:
@@ -106,7 +123,13 @@ def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
106
123
self .filters .append ({"kind" : "below" , "args" : [element_or_locator ]})
107
124
return self
108
125
109
- def to_left_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
126
+ @overload
127
+ def to_left_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
128
+
129
+ @overload
130
+ def to_left_of (self , element_or_locator : None = None ) -> "NoReturn" : ...
131
+
132
+ def to_left_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
110
133
"""Add a filter to look for elements to the left of.
111
134
112
135
:Args:
@@ -118,7 +141,13 @@ def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Rel
118
141
self .filters .append ({"kind" : "left" , "args" : [element_or_locator ]})
119
142
return self
120
143
121
- def to_right_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
144
+ @overload
145
+ def to_right_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" : ...
146
+
147
+ @overload
148
+ def to_right_of (self , element_or_locator : None = None ) -> "NoReturn" : ...
149
+
150
+ def to_right_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
122
151
"""Add a filter to look for elements right of.
123
152
124
153
:Args:
@@ -130,16 +159,25 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Re
130
159
self .filters .append ({"kind" : "right" , "args" : [element_or_locator ]})
131
160
return self
132
161
133
- def near (self , element_or_locator_distance : Union [WebElement , Dict , int ] = None ) -> "RelativeBy" :
162
+ @overload
163
+ def near (self , element_or_locator : Union [WebElement , LocatorType ], distance : int = 50 ) -> "RelativeBy" : ...
164
+
165
+ @overload
166
+ def near (self , element_or_locator : None = None , distance : int = 50 ) -> "NoReturn" : ...
167
+
168
+ def near (self , element_or_locator : Union [WebElement , LocatorType , None ] = None , distance : int = 50 ) -> "RelativeBy" :
134
169
"""Add a filter to look for elements near.
135
170
136
171
:Args:
137
- - element_or_locator_distance: Element to look near by the element or within a distance
172
+ - element_or_locator: Element to look near by the element or within a distance
173
+ - distance: distance in pixel
138
174
"""
139
- if not element_or_locator_distance :
140
- raise WebDriverException ("Element or locator or distance must be given when calling near method" )
175
+ if not element_or_locator :
176
+ raise WebDriverException ("Element or locator must be given when calling near method" )
177
+ if distance <= 0 :
178
+ raise WebDriverException ("Distance must be positive" )
141
179
142
- self .filters .append ({"kind" : "near" , "args" : [element_or_locator_distance ]})
180
+ self .filters .append ({"kind" : "near" , "args" : [element_or_locator , distance ]})
143
181
return self
144
182
145
183
def to_dict (self ) -> Dict :
0 commit comments