Skip to content

Commit 2d3fd9c

Browse files
committed
Add more typing for browser.py
1 parent 6924a0d commit 2d3fd9c

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

dash/testing/browser.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import sys
44
import time
55
import logging
6-
from typing import cast
6+
from typing import Union, Optional
77
import warnings
88
import percy
99
import requests
1010

1111
from selenium import webdriver
12+
from selenium.webdriver.remote.webdriver import BaseWebDriver
1213
from selenium.webdriver.support import expected_conditions as EC
1314
from selenium.webdriver.common.by import By
1415
from selenium.webdriver.support.wait import WebDriverWait
@@ -38,20 +39,22 @@
3839

3940

4041
class Browser(DashPageMixin):
42+
_url: str
43+
4144
# pylint: disable=too-many-arguments
4245
def __init__(
4346
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,
5558
):
5659
self._browser = browser.lower()
5760
self._remote_url = remote_url
@@ -71,7 +74,7 @@ def __init__(
7174

7275
self._wd_wait = WebDriverWait(self.driver, wait_timeout)
7376
self._last_ts = 0
74-
self._url = None
77+
self._url = ""
7578

7679
self._window_idx = 0 # switch browser tabs
7780

@@ -108,8 +111,8 @@ def __exit__(self, exc_type, exc_val, traceback):
108111

109112
def visit_and_snapshot(
110113
self,
111-
resource_path,
112-
hook_id,
114+
resource_path: str,
115+
hook_id: str,
113116
wait_for_callbacks=True,
114117
convert_canvases=False,
115118
assert_check=True,
@@ -120,7 +123,7 @@ def visit_and_snapshot(
120123
try:
121124
if path != resource_path:
122125
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
124127
self.driver.get(f"{self.server_url.rstrip('/')}/{path}")
125128

126129
# wait for the hook_id to present and all callbacks get fired
@@ -219,7 +222,7 @@ def percy_snapshot(
219222
"""
220223
)
221224

222-
def take_snapshot(self, name):
225+
def take_snapshot(self, name: str):
223226
"""Hook method to take snapshot when a selenium test fails. The
224227
snapshot is placed under.
225228
@@ -228,8 +231,10 @@ def take_snapshot(self, name):
228231
with a filename combining test case name and the
229232
running selenium session id
230233
"""
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+
233238
if not os.path.exists(target):
234239
try:
235240
os.mkdir(target)
@@ -286,7 +291,7 @@ def _wait_for(self, method, timeout, msg):
286291
message = msg(self.driver)
287292
else:
288293
message = msg
289-
raise TimeoutException(message) from err
294+
raise TimeoutException(str(message)) from err
290295

291296
def wait_for_element(self, selector, timeout=None):
292297
"""wait_for_element is shortcut to `wait_for_element_by_css_selector`
@@ -534,10 +539,12 @@ def _get_firefox(self):
534539
"browser.helperApps.neverAsk.saveToDisk",
535540
"application/octet-stream", # this MIME is generic for binary
536541
)
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+
538545
return (
539546
webdriver.Remote(
540-
command_executor=self._remote_url,
547+
command_executor=self._remote_url, # type: ignore[reportTypeArgument]
541548
options=options,
542549
)
543550
if self._remote
@@ -633,7 +640,7 @@ def session_id(self):
633640
return self.driver.session_id
634641

635642
@property
636-
def server_url(self):
643+
def server_url(self) -> str:
637644
return self._url
638645

639646
@server_url.setter

0 commit comments

Comments
 (0)