Skip to content

Commit

Permalink
Read marionette socket
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpitt committed Aug 8, 2024
1 parent e9f4fcf commit edcec61
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions test/common/bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,42 @@ async def start_bidi_session(self) -> None:
for _ in range(1, 30):
try:
# we must keep this socket open throughout the lifetime of that session
reader, self.writer_marionette = await asyncio.open_connection("127.0.0.1", marionette_port)
self.reader_marionette, self.writer_marionette = await asyncio.open_connection("127.0.0.1", marionette_port)
break
except ConnectionRefusedError as e:
log_proto.debug("waiting for firefox marionette: %s", e)
await asyncio.sleep(1)
else:
raise WebdriverError("could not connect to firefox marionette")

reply = await reader.read(1024)
reply = await self.reader_marionette.read(1024)
if b'"marionetteProtocol":3' not in reply:
raise WebdriverError(f"unexpected marionette reply: {reply.decode()}")
cmd = '[0,1,"WebDriver:NewSession",{"webSocketUrl":true,"acceptInsecureCerts":true}]'
self.writer_marionette.write(f"{len(cmd)}:{cmd}".encode())
await self.writer_marionette.drain()
reply = await reader.read(1024)
reply = await self.reader_marionette.read(1024)
# cut off length prefix
reply = json.loads(reply[reply.index(b":") + 1:].decode())
if not isinstance(reply, list) or len(reply) != 4 or not isinstance(reply[3], dict):
raise WebdriverError(f"unexpected marionette session request reply: {reply!r}")
log_proto.debug("marionette session request reply: %s", reply)

# continue to read from Marionette, in case there are any messages; we must drain the pipe
self.marionette_logger: asyncio.Task = asyncio.create_task(self.log_marionette(), name="marionette_logger")

url = reply[3]["capabilities"]["webSocketUrl"]
self.bidi_session = BidiSession(session_url=url, ws_url=url, process=driver)
log_proto.debug("Established firefox session %r", self.bidi_session)

async def log_marionette(self) -> None:
# we don't expect messages here, so log them as warnings
while True:
msg = await self.reader_marionette.read()
log_proto.warning("marionette: %s", msg)

async def close_bidi_session(self) -> None:
self.marionette_logger.cancel()
del self.marionette_logger
self.writer_marionette.close()
await self.writer_marionette.wait_closed()

0 comments on commit edcec61

Please sign in to comment.