How to use test_should_override_request_url method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_request_continue.py

Source: test_request_continue.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import asyncio
16
17
18async def test_request_continue_should_work(page, server):
19    await page.route("**/*", lambda route: asyncio.create_task(route.continue_()))
20    await page.goto(server.EMPTY_PAGE)
21
22
23async def test_request_continue_should_amend_http_headers(page, server):
24    await page.route(
25        "**/*",
26        lambda route: asyncio.create_task(
27            route.continue_(headers={**route.request.headers, "FOO": "bar"})
28        ),
29    )
30
31    await page.goto(server.EMPTY_PAGE)
32    [request, _] = await asyncio.gather(
33        server.wait_for_request("/sleep.zzz"),
34        page.evaluate('() => fetch("/sleep.zzz")'),
35    )
36    assert request.getHeader("foo") == "bar"
37
38
39async def test_request_continue_should_amend_method(page, server):
40    server_request = asyncio.create_task(server.wait_for_request("/sleep.zzz"))
41    await page.goto(server.EMPTY_PAGE)
42    await page.route(
43        "**/*", lambda route: asyncio.create_task(route.continue_(method="POST"))
44    )
45    [request, _] = await asyncio.gather(
46        server.wait_for_request("/sleep.zzz"),
47        page.evaluate('() => fetch("/sleep.zzz")'),
48    )
49    assert request.method.decode() == "POST"
50    assert (await server_request).method.decode() == "POST"
51
52
53async def test_request_continue_should_amend_method_on_main_request(page, server):
54    request = asyncio.create_task(server.wait_for_request("/empty.html"))
55    await page.route(
56        "**/*", lambda route: asyncio.create_task(route.continue_(method="POST"))
57    )
58    await page.goto(server.EMPTY_PAGE)
59    assert (await request).method.decode() == "POST"
60
61
62async def test_request_continue_should_amend_post_data(page, server):
63    await page.goto(server.EMPTY_PAGE)
64    await page.route(
65        "**/*",
66        lambda route: asyncio.create_task(route.continue_(post_data=b"doggo")),
67    )
68
69    [server_request, _] = await asyncio.gather(
70        server.wait_for_request("/sleep.zzz"),
71        page.evaluate(
72            """
73            () => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })
74            """
75        ),
76    )
77    assert server_request.post_body.decode() == "doggo"
78
79
80async def test_should_override_request_url(page, server):
81    request = asyncio.create_task(server.wait_for_request("/empty.html"))
82    await page.route(
83        "**/foo",
84        lambda route: asyncio.create_task(route.continue_(url=server.EMPTY_PAGE)),
85    )
86
87    await page.goto(server.PREFIX + "/foo")
88    assert (await request).method == b"GET"
89
90
91async def test_should_amend_utf8_post_data(page, server):
92    await page.goto(server.EMPTY_PAGE)
93    await page.route(
94        "**/*",
95        lambda route: asyncio.create_task(route.continue_(post_data="пушкин")),
96    )
97
98    [server_request, result] = await asyncio.gather(
99        server.wait_for_request("/sleep.zzz"),
100        page.evaluate("fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })"),
101    )
102    assert server_request.method == b"POST"
103    assert server_request.post_body.decode("utf8") == "пушкин"
104
105
106async def test_should_amend_binary_post_data(page, server):
107    await page.goto(server.EMPTY_PAGE)
108    await page.route(
109        "**/*",
110        lambda route: asyncio.create_task(
111            route.continue_(post_data=b"\x00\x01\x02\x03\x04")
112        ),
113    )
114
115    [server_request, result] = await asyncio.gather(
116        server.wait_for_request("/sleep.zzz"),
117        page.evaluate("fetch('/sleep.zzz', { method: 'POST', body: 'birdy' })"),
118    )
119    assert server_request.method == b"POST"
120    assert server_request.post_body == b"\x00\x01\x02\x03\x04"
121
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)