3
3
import sys
4
4
import time
5
5
import logging
6
- from typing import cast
6
+ from typing import Union , Optional
7
7
import warnings
8
8
import percy
9
9
import requests
10
10
11
11
from selenium import webdriver
12
+ from selenium .webdriver .remote .webdriver import BaseWebDriver
12
13
from selenium .webdriver .support import expected_conditions as EC
13
14
from selenium .webdriver .common .by import By
14
15
from selenium .webdriver .support .wait import WebDriverWait
38
39
39
40
40
41
class Browser (DashPageMixin ):
42
+ _url : str
43
+
41
44
# pylint: disable=too-many-arguments
42
45
def __init__ (
43
46
self ,
44
- browser ,
45
- remote = False ,
46
- remote_url = None ,
47
- headless = False ,
48
- options = None ,
49
- download_path = "" ,
50
- percy_run = True ,
51
- percy_finalize = True ,
52
- percy_assets_root = "" ,
53
- wait_timeout = 10 ,
54
- pause = False ,
47
+ browser : str ,
48
+ remote : bool = False ,
49
+ remote_url : Optional [ str ] = None ,
50
+ headless : bool = False ,
51
+ options : Optional [ Union [ dict , list ]] = None ,
52
+ download_path : str = "" ,
53
+ percy_run : bool = True ,
54
+ percy_finalize : bool = True ,
55
+ percy_assets_root : str = "" ,
56
+ wait_timeout : int = 10 ,
57
+ pause : bool = False ,
55
58
):
56
59
self ._browser = browser .lower ()
57
60
self ._remote_url = remote_url
@@ -71,7 +74,7 @@ def __init__(
71
74
72
75
self ._wd_wait = WebDriverWait (self .driver , wait_timeout )
73
76
self ._last_ts = 0
74
- self ._url = None
77
+ self ._url = ""
75
78
76
79
self ._window_idx = 0 # switch browser tabs
77
80
@@ -108,8 +111,8 @@ def __exit__(self, exc_type, exc_val, traceback):
108
111
109
112
def visit_and_snapshot (
110
113
self ,
111
- resource_path ,
112
- hook_id ,
114
+ resource_path : str ,
115
+ hook_id : str ,
113
116
wait_for_callbacks = True ,
114
117
convert_canvases = False ,
115
118
assert_check = True ,
@@ -120,7 +123,7 @@ def visit_and_snapshot(
120
123
try :
121
124
if path != resource_path :
122
125
logger .warning ("we stripped the left '/' in resource_path" )
123
- self .server_url = cast ( str , self .server_url ) # to satisfy type checking
126
+ self .server_url = self .server_url
124
127
self .driver .get (f"{ self .server_url .rstrip ('/' )} /{ path } " )
125
128
126
129
# wait for the hook_id to present and all callbacks get fired
@@ -219,7 +222,7 @@ def percy_snapshot(
219
222
"""
220
223
)
221
224
222
- def take_snapshot (self , name ):
225
+ def take_snapshot (self , name : str ):
223
226
"""Hook method to take snapshot when a selenium test fails. The
224
227
snapshot is placed under.
225
228
@@ -228,8 +231,10 @@ def take_snapshot(self, name):
228
231
with a filename combining test case name and the
229
232
running selenium session id
230
233
"""
231
- target = "/tmp/dash_artifacts" if not self ._is_windows () else os .getenv ("TEMP" )
232
- target = cast (str , target ) # to satisfy type checking
234
+ target = (
235
+ "/tmp/dash_artifacts" if not self ._is_windows () else os .getenv ("TEMP" , "" )
236
+ )
237
+
233
238
if not os .path .exists (target ):
234
239
try :
235
240
os .mkdir (target )
@@ -286,7 +291,7 @@ def _wait_for(self, method, timeout, msg):
286
291
message = msg (self .driver )
287
292
else :
288
293
message = msg
289
- raise TimeoutException (message ) from err
294
+ raise TimeoutException (str ( message ) ) from err
290
295
291
296
def wait_for_element (self , selector , timeout = None ):
292
297
"""wait_for_element is shortcut to `wait_for_element_by_css_selector`
@@ -534,10 +539,12 @@ def _get_firefox(self):
534
539
"browser.helperApps.neverAsk.saveToDisk" ,
535
540
"application/octet-stream" , # this MIME is generic for binary
536
541
)
537
- self ._remote_url = cast (str , self ._remote_url ) # to satisfy type checking
542
+ if not self ._remote_url and self ._remote :
543
+ raise TypeError ("remote_url was not provided but required for Firefox" )
544
+
538
545
return (
539
546
webdriver .Remote (
540
- command_executor = self ._remote_url ,
547
+ command_executor = self ._remote_url , # type: ignore[reportTypeArgument]
541
548
options = options ,
542
549
)
543
550
if self ._remote
@@ -633,7 +640,7 @@ def session_id(self):
633
640
return self .driver .session_id
634
641
635
642
@property
636
- def server_url (self ):
643
+ def server_url (self ) -> str :
637
644
return self ._url
638
645
639
646
@server_url .setter
0 commit comments