1212import threading
1313import time
1414from urllib .error import URLError
15+ from urllib .parse import quote
1516from urllib .request import Request , urlopen
1617from zipfile import ZipFile
1718
@@ -95,6 +96,26 @@ def wait_for_bridge(daemon, browser):
9596 raise RuntimeError ('The extension did not connect to the isolated daemon' )
9697
9798
99+ def prepare_bound_tab (profile , url ):
100+ # Windows Chrome can open a startup tab before its URL is set. Provision the
101+ # user tab via the independent DevTools endpoint and wait for navigation.
102+ deadline = time .monotonic () + 15
103+ port_file = profile / 'DevToolsActivePort'
104+ while not port_file .exists () and time .monotonic () < deadline :
105+ time .sleep (0.1 )
106+ port = int (port_file .read_text (encoding = 'utf-8' ).splitlines ()[0 ])
107+ endpoint = f'http://127.0.0.1:{ port } /json'
108+ with urlopen (Request (endpoint + '/new?' + quote (url , safe = ':/' ), method = 'PUT' ), timeout = 5 ) as response :
109+ target = json .load (response )['id' ]
110+ while time .monotonic () < deadline :
111+ with urlopen (endpoint + '/list' , timeout = 2 ) as response :
112+ if any (page ['id' ] == target and page .get ('url' ) == url for page in json .load (response )):
113+ with urlopen (endpoint + '/activate/' + target , timeout = 2 ):
114+ return
115+ time .sleep (0.1 )
116+ raise RuntimeError ('The original Chrome fixture tab did not finish navigation' )
117+
118+
98119def stop (process ):
99120 if process is None or process .poll () is not None :
100121 return
@@ -148,12 +169,13 @@ def execute(args, root, evidence):
148169 with (evidence / 'daemon.log' ).open ('w' ) as log :
149170 daemon = subprocess .Popen ([node , str (args .cli_entry .with_name ('daemon.js' ))],
150171 cwd = root , env = env , stdout = log , stderr = subprocess .STDOUT )
172+ profile = root / 'profile'
151173 chrome_args = [
152174 '--headless=new' , '--no-first-run' , '--no-default-browser-check' ,
153175 '--disable-background-networking' , '--disable-component-update' ,
154176 '--disable-sync' , '--remote-debugging-port=0' , '--no-proxy-server' ,
155177 '--host-resolver-rules=MAP localhost 127.0.0.1' ,
156- '--user-data-dir=' + str (root / ' profile' ),
178+ '--user-data-dir=' + str (profile ),
157179 '--disable-extensions-except=' + str (extension ), '--load-extension=' + str (extension ),
158180 ]
159181 if sys .platform == 'linux' and os .environ .get ('GITHUB_ACTIONS' ) == 'true' :
@@ -162,9 +184,10 @@ def execute(args, root, evidence):
162184 if sys .platform == 'darwin' :
163185 chrome_args .append ('--use-mock-keychain' )
164186 with (evidence / 'chrome.log' ).open ('w' ) as log :
165- browser = subprocess .Popen ([str (chrome ), * chrome_args , url . replace ( '/fixture' , '/bound-fixture' ) ],
187+ browser = subprocess .Popen ([str (chrome ), * chrome_args , 'about:blank' ],
166188 cwd = root , env = env , stdout = log , stderr = subprocess .STDOUT )
167189 wait_for_bridge (daemon , browser )
190+ prepare_bound_tab (profile , url .replace ('/fixture' , '/bound-fixture' ))
168191 with (evidence / 'maven.log' ).open ('w' ) as log :
169192 result = subprocess .run ([* maven_command , '-B' , '--no-transfer-progress' , '-Dtest=OpenCliBrowserBridgeIT' , 'test' ],
170193 env = env , stdout = log , stderr = subprocess .STDOUT , timeout = 180 )
0 commit comments