55import subprocess
66from time import sleep
77from urllib .request import urlopen
8+ from urllib .error import URLError
89
910from selenium import webdriver
10- from selenium .common .exceptions import TimeoutException , WebDriverException
11+ from selenium .common .exceptions import TimeoutException
1112from selenium .webdriver .support .wait import WebDriverWait
1213from selenium .webdriver .support import expected_conditions as EC
1314from selenium .webdriver .common .by import By
@@ -87,6 +88,16 @@ def start_jupyter(self, jupyterdir, app):
8788 }
8889 self .jupyter_proc = subprocess .Popen (command , cwd = str (jupyterdir ), env = env )
8990
91+ # Wait for Jupyter to start
92+ for n in range (10 ):
93+ sleep (1 )
94+ try :
95+ with urlopen (f"http://localhost:{ PORT } " , timeout = 2 ) as _ :
96+ pass
97+ except URLError :
98+ continue
99+ print (f"jupyter-{ app } started" )
100+
90101 def initialise_firefox (self , downloaddir , url ):
91102 profile = FirefoxProfile ()
92103 profile .set_preference ("browser.download.folderList" , 2 )
@@ -107,15 +118,7 @@ def initialise_firefox(self, downloaddir, url):
107118 self .driver = webdriver .Firefox (options = options )
108119 self .wait = WebDriverWait (self .driver , TIMEOUT )
109120
110- # Jupyter may be slow to start
111- for n in range (5 ):
112- sleep (1 )
113- try :
114- self .driver .get (url )
115- except WebDriverException as exc :
116- if "about:neterror" not in exc .msg :
117- raise
118-
121+ self .driver .get (url )
119122 print ("Firefox Initialized" )
120123
121124 def initialise (self , tmpdir , app , url ):
@@ -350,8 +353,6 @@ def test_offline_lab(self, tmpdir):
350353class TestServer (FirefoxTestBase ):
351354 def test_server_config (self , tmpdir ):
352355 self .start_jupyter (tmpdir , "server" )
353- # Wait for server to start
354- sleep (2 )
355356
356357 with urlopen (CONFIG_URL ) as r :
357358 assert json .load (r ) == {
0 commit comments