Best Python code snippet using playwright-python
_network.py
Source:_network.py  
...159    @property160    def request(self) -> Request:161        return from_channel(self._initializer["request"])162    async def abort(self, errorCode: str = None) -> None:163        await self._race_with_page_close(164            self._channel.send("abort", locals_to_params(locals()))165        )166    async def fulfill(167        self,168        status: int = None,169        headers: Dict[str, str] = None,170        body: Union[str, bytes] = None,171        path: Union[str, Path] = None,172        contentType: str = None,173        response: "APIResponse" = None,174    ) -> None:175        params = locals_to_params(locals())176        if response:177            del params["response"]178            params["status"] = (179                params["status"] if params.get("status") else response.status180            )181            params["headers"] = (182                params["headers"] if params.get("headers") else response.headers183            )184            from playwright._impl._fetch import APIResponse185            if body is None and path is None and isinstance(response, APIResponse):186                if response._request._connection is self._connection:187                    params["fetchResponseUid"] = response._fetch_uid188                else:189                    body = await response.body()190        length = 0191        if isinstance(body, str):192            params["body"] = body193            params["isBase64"] = False194            length = len(body.encode())195        elif isinstance(body, bytes):196            params["body"] = base64.b64encode(body).decode()197            params["isBase64"] = True198            length = len(body)199        elif path:200            del params["path"]201            file_content = Path(path).read_bytes()202            params["body"] = base64.b64encode(file_content).decode()203            params["isBase64"] = True204            length = len(file_content)205        headers = {k.lower(): str(v) for k, v in params.get("headers", {}).items()}206        if params.get("contentType"):207            headers["content-type"] = params["contentType"]208        elif path:209            headers["content-type"] = (210                mimetypes.guess_type(str(Path(path)))[0] or "application/octet-stream"211            )212        if length and "content-length" not in headers:213            headers["content-length"] = str(length)214        params["headers"] = serialize_headers(headers)215        await self._race_with_page_close(self._channel.send("fulfill", params))216    async def continue_(217        self,218        url: str = None,219        method: str = None,220        headers: Dict[str, str] = None,221        postData: Union[str, bytes] = None,222    ) -> None:223        overrides: ContinueParameters = {}224        if url:225            overrides["url"] = url226        if method:227            overrides["method"] = method228        if headers:229            overrides["headers"] = serialize_headers(headers)230        if isinstance(postData, str):231            overrides["postData"] = base64.b64encode(postData.encode()).decode()232        elif isinstance(postData, bytes):233            overrides["postData"] = base64.b64encode(postData).decode()234        await self._race_with_page_close(235            self._channel.send("continue", cast(Any, overrides))236        )237    def _internal_continue(self) -> None:238        async def continue_route() -> None:239            try:240                await self.continue_()241            except Exception:242                pass243        asyncio.create_task(continue_route())244    async def _race_with_page_close(self, future: Coroutine) -> None:245        if hasattr(self.request.frame, "_page"):246            page = self.request.frame._page247            # When page closes or crashes, we catch any potential rejects from this Route.248            # Note that page could be missing when routing popup's initial request that249            # does not have a Page initialized just yet.250            fut = asyncio.create_task(future)251            await asyncio.wait(252                [fut, page._closed_or_crashed_future],253                return_when=asyncio.FIRST_COMPLETED,254            )255            if page._closed_or_crashed_future.done():256                await asyncio.gather(fut, return_exceptions=True)257        else:258            await future...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
