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,15 @@ 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
+
94
+ @overload
95
+ def above (self , element_or_locator : None = None ) -> "NoReturn" :
96
+ ...
97
+
98
+ def above (self , element_or_locator : Union [WebElement , LocatorType , None ] = None ) -> "RelativeBy" :
86
99
"""Add a filter to look for elements above.
87
100
88
101
:Args:
@@ -94,7 +107,15 @@ def above(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
94
107
self .filters .append ({"kind" : "above" , "args" : [element_or_locator ]})
95
108
return self
96
109
97
- def below (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
110
+ @overload
111
+ def below (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
112
+ ...
113
+
114
+ @overload
115
+ def below (self , element_or_locator : None = None ) -> "NoReturn" :
116
+ ...
117
+
118
+ def below (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
98
119
"""Add a filter to look for elements below.
99
120
100
121
:Args:
@@ -106,7 +127,15 @@ def below(self, element_or_locator: Union[WebElement, Dict] = None) -> "Relative
106
127
self .filters .append ({"kind" : "below" , "args" : [element_or_locator ]})
107
128
return self
108
129
109
- def to_left_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
130
+ @overload
131
+ def to_left_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
132
+ ...
133
+
134
+ @overload
135
+ def to_left_of (self , element_or_locator : None = None ) -> "NoReturn" :
136
+ ...
137
+
138
+ def to_left_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
110
139
"""Add a filter to look for elements to the left of.
111
140
112
141
:Args:
@@ -118,7 +147,15 @@ def to_left_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Rel
118
147
self .filters .append ({"kind" : "left" , "args" : [element_or_locator ]})
119
148
return self
120
149
121
- def to_right_of (self , element_or_locator : Union [WebElement , Dict ] = None ) -> "RelativeBy" :
150
+ @overload
151
+ def to_right_of (self , element_or_locator : Union [WebElement , LocatorType ]) -> "RelativeBy" :
152
+ ...
153
+
154
+ @overload
155
+ def to_right_of (self , element_or_locator : None = None ) -> "NoReturn" :
156
+ ...
157
+
158
+ def to_right_of (self , element_or_locator : Union [WebElement , Dict , None ] = None ) -> "RelativeBy" :
122
159
"""Add a filter to look for elements right of.
123
160
124
161
:Args:
@@ -130,16 +167,27 @@ def to_right_of(self, element_or_locator: Union[WebElement, Dict] = None) -> "Re
130
167
self .filters .append ({"kind" : "right" , "args" : [element_or_locator ]})
131
168
return self
132
169
133
- def near (self , element_or_locator_distance : Union [WebElement , Dict , int ] = None ) -> "RelativeBy" :
170
+ @overload
171
+ def near (self , element_or_locator : Union [WebElement , LocatorType ], distance : int = 50 ) -> "RelativeBy" :
172
+ ...
173
+
174
+ @overload
175
+ def near (self , element_or_locator : None = None , distance : int = 50 ) -> "NoReturn" :
176
+ ...
177
+
178
+ def near (self , element_or_locator : Union [WebElement , LocatorType , None ] = None , distance : int = 50 ) -> "RelativeBy" :
134
179
"""Add a filter to look for elements near.
135
180
136
181
:Args:
137
- - element_or_locator_distance: Element to look near by the element or within a distance
182
+ - element_or_locator: Element to look near by the element or within a distance
183
+ - distance: distance in pixel
138
184
"""
139
- if not element_or_locator_distance :
140
- raise WebDriverException ("Element or locator or distance must be given when calling near method" )
185
+ if not element_or_locator :
186
+ raise WebDriverException ("Element or locator must be given when calling near method" )
187
+ if distance <= 0 :
188
+ raise WebDriverException ("Distance must be positive" )
141
189
142
- self .filters .append ({"kind" : "near" , "args" : [element_or_locator_distance ]})
190
+ self .filters .append ({"kind" : "near" , "args" : [element_or_locator , distance ]})
143
191
return self
144
192
145
193
def to_dict (self ) -> Dict :
0 commit comments