Example #1
0
    async def connect(
        self,
        ws_endpoint: str,
        timeout: float = None,
        slow_mo: float = None,
        headers: Dict[str, str] = None,
    ) -> Browser:
        transport = WebSocketTransport(ws_endpoint, timeout, 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())
        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
Example #2
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

        headers = {
            **(headers if headers else {}), "x-playwright-browser": self.name
        }

        transport = WebSocketTransport(self._connection._loop, ws_endpoint,
                                       headers, slow_mo)
        connection = Connection(
            self._connection._dispatcher_fiber,
            self._connection._object_factory,
            transport,
            self._connection._loop,
        )
        connection.mark_as_remote()
        connection._is_sync = self._connection._is_sync
        connection._loop.create_task(connection.run())
        playwright_future = connection.playwright_future

        timeout_future = throw_on_timeout(timeout,
                                          Error("Connection timed out"))
        done, pending = await asyncio.wait(
            {transport.on_error_future, playwright_future, timeout_future},
            return_when=asyncio.FIRST_COMPLETED,
        )
        if not playwright_future.done():
            playwright_future.cancel()
        if not timeout_future.done():
            timeout_future.cancel()
        playwright: "Playwright" = next(iter(done)).result()
        playwright._set_selectors(self._playwright.selectors)
        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._should_close_connection_on_close = True
        browser._local_utils = self._playwright._utils

        def handle_transport_close() -> None:
            for context in browser.contexts:
                for page in context.pages:
                    page._on_close()
                context._on_close()
            browser._on_close()
            connection.cleanup()

        transport.once("close", handle_transport_close)

        return browser
Example #3
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, slow_mo)
        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

        def handle_transport_close() -> None:
            for context in browser.contexts:
                for page in context.pages:
                    page._on_close()
                context._on_close()
            browser._on_close()

        transport.once("close", handle_transport_close)

        return browser
Example #4
0
    async def connect(
        self,
        ws_endpoint: str,
        timeout: float = None,
        slow_mo: float = None,
        headers: Dict[str, str] = None,
    ) -> Browser:
        transport = WebSocketTransport(ws_endpoint, timeout, headers)

        connection = Connection(
            self._connection._dispatcher_fiber,
            self._connection._object_factory,
            transport,
        )
        await connection._transport.start()
        connection._is_sync = self._connection._is_sync
        connection._loop = self._connection._loop
        connection._loop.create_task(connection.run())
        obj = asyncio.create_task(
            connection.wait_for_object_with_known_name("Playwright"))
        done, pending = await asyncio.wait(
            {
                obj,
                connection._transport.on_error_future,  # type: ignore
            },
            return_when=asyncio.FIRST_COMPLETED,
        )
        if not obj.done():
            obj.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