Esempio n. 1
0
    async def connect(
        self,
        ws_endpoint: str,
        timeout: float = None,
        slow_mo: float = None,
        headers: Dict[str, str] = None,
    ) -> Browser:
        if timeout is None:
            timeout = 30000

        transport = WebSocketTransport(self._connection._loop, ws_endpoint,
                                       headers)
        connection = Connection(
            self._connection._dispatcher_fiber,
            self._connection._object_factory,
            transport,
        )
        connection._is_sync = self._connection._is_sync
        connection._loop = self._connection._loop
        connection._loop.create_task(connection.run())
        future = connection._loop.create_task(
            connection.wait_for_object_with_known_name("Playwright"))
        timeout_future = throw_on_timeout(timeout,
                                          Error("Connection timed out"))
        done, pending = await asyncio.wait(
            {transport.on_error_future, future, timeout_future},
            return_when=asyncio.FIRST_COMPLETED,
        )
        if not future.done():
            future.cancel()
        if not timeout_future.done():
            timeout_future.cancel()
        playwright = next(iter(done)).result()
        self._connection._child_ws_connections.append(connection)
        pre_launched_browser = playwright._initializer.get(
            "preLaunchedBrowser")
        assert pre_launched_browser
        browser = cast(Browser, from_channel(pre_launched_browser))
        browser._is_remote = True
        browser._is_connected_over_websocket = True

        transport.once("close", browser._on_close)

        return browser
Esempio n. 2
0
    async def connect(
        self, ws_endpoint: str, timeout: float = None, slow_mo: float = None
    ) -> Browser:
        transport = WebSocketTransport(ws_endpoint, timeout)

        connection = Connection(
            self._connection._dispatcher_fiber,
            self._connection._object_factory,
            transport,
        )
        connection._is_sync = self._connection._is_sync
        connection._loop = self._connection._loop
        connection._loop.create_task(connection.run())
        self._connection._child_ws_connections.append(connection)
        playwright = await connection.wait_for_object_with_known_name("Playwright")
        pre_launched_browser = playwright._initializer.get("preLaunchedBrowser")
        assert pre_launched_browser
        browser = cast(Browser, from_channel(pre_launched_browser))
        browser._is_remote = True
        browser._is_connected_over_websocket = True

        transport.once("close", browser._on_close)

        return browser