1414from selenium .webdriver .common .by import By
1515
1616from selenium .webdriver .firefox .firefox_profile import FirefoxProfile
17- from selenium .webdriver .firefox .options import Options
17+ from selenium .webdriver .firefox .options import Options as FirefoxOptions
18+ from selenium .webdriver .chrome .options import Options as ChromeOptions
1819
1920
2021PORT = 18888
@@ -111,7 +112,7 @@ def initialise_firefox(self, downloaddir, url):
111112 "browser.helperApps.neverAsk.saveToDisk" , "application/x-ipynb+json"
112113 )
113114
114- options = Options ()
115+ options = FirefoxOptions ()
115116 if HEADLESS :
116117 options .add_argument ("-headless" )
117118
@@ -124,7 +125,28 @@ def initialise_firefox(self, downloaddir, url):
124125 self .driver .get (url )
125126 print ("Firefox Initialized" )
126127
127- def initialise (self , tmpdir , app , url ):
128+ def initialise_chrome (self , downloaddir , url ):
129+ """Initialises a Chrome webdriver with download preferences."""
130+ options = ChromeOptions ()
131+ if HEADLESS :
132+ options .add_argument ("--headless=new" )
133+
134+ prefs = {
135+ "download.default_directory" : downloaddir ,
136+ "download.prompt_for_download" : False ,
137+ "safebrowsing.enabled" : True ,
138+ }
139+ options .add_experimental_option ("prefs" , prefs )
140+
141+ self .driver = webdriver .Chrome (options = options )
142+ self .wait = WebDriverWait (self .driver , TIMEOUT )
143+
144+ self .driver .get (url )
145+ # Default size is too narrow, so toolbar is partially hidden
146+ self .driver .set_window_size (1024 , 768 )
147+ print ("Chrome Initialized" )
148+
149+ def initialise (self , tmpdir , app , url , browser ):
128150 jupyterdir = (tmpdir / "jupyter" ).mkdir ()
129151 downloaddir = (tmpdir / "download" ).mkdir ()
130152
@@ -136,7 +158,12 @@ def initialise(self, tmpdir, app, url):
136158 self .expected_download = str (downloaddir / "example.ipynb" )
137159
138160 self .start_jupyter (jupyterdir , app )
139- self .initialise_firefox (str (downloaddir ), url )
161+ if browser == "firefox" :
162+ self .initialise_firefox (str (downloaddir ), url )
163+ elif browser == "chrome" :
164+ self .initialise_chrome (str (downloaddir ), url )
165+ else :
166+ pytest .fail (f"Unsupported browser: { browser } " )
140167
141168
142169class TestOfflineLab (FirefoxTestBase ):
@@ -201,11 +228,11 @@ def restore_from_browser_storage(self):
201228 @pytest .mark .flaky (max_runs = 3 )
202229 # Notebook 7 is based on JupyterLab
203230 @pytest .mark .parametrize ("app" , ["lab" , "notebook" ])
204- def test_offline_lab (self , tmpdir , app ):
231+ def test_offline_lab (self , tmpdir , app , browser ):
205232 # Selenium can't access IndexedDB so instead check save/load by
206233 # downloading the updated notebook
207234
208- self .initialise (tmpdir , app , JUPYTER_URL [app ])
235+ self .initialise (tmpdir , app , JUPYTER_URL [app ], browser )
209236 self .toolbar_button = "jp-button"
210237
211238 # Wait for the loading logo to appear, then disappear
0 commit comments