How to use test_wait_for_url_should_work_with_url_match_for_same_document_navigations 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_wait_for_url.py

Source: test_wait_for_url.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 re
16
17import pytest
18
19from playwright.async_api import Error, Page
20
21
22async def test_wait_for_url_should_work(page: Page, server):
23    await page.goto(server.EMPTY_PAGE)
24    await page.evaluate(
25        "url => window.location.href = url", server.PREFIX + "/grid.html"
26    )
27    await page.wait_for_url("**/grid.html")
28    assert "grid.html" in page.url
29
30
31async def test_wait_for_url_should_respect_timeout(page: Page, server):
32    await page.goto(server.EMPTY_PAGE)
33    with pytest.raises(Error) as exc_info:
34        await page.wait_for_url("**/frame.html", timeout=2500)
35    assert "Timeout 2500ms exceeded" in exc_info.value.message
36
37
38async def test_wait_for_url_should_work_with_both_domcontentloaded_and_load(
39    page: Page, server
40):
41    await page.goto(server.EMPTY_PAGE)
42    await page.wait_for_url("**/*", wait_until="domcontentloaded")
43    await page.wait_for_url("**/*", wait_until="load")
44
45
46async def test_wait_for_url_should_work_with_clicking_on_anchor_links(
47    page: Page, server
48):
49    await page.goto(server.EMPTY_PAGE)
50    await page.set_content('<a href="#foobar">foobar</a>')
51    await page.click("a")
52    await page.wait_for_url("**/*#foobar")
53    assert page.url == server.EMPTY_PAGE + "#foobar"
54
55
56async def test_wait_for_url_should_work_with_history_push_state(page: Page, server):
57    await page.goto(server.EMPTY_PAGE)
58    await page.set_content(
59        """
60        <a onclick='javascript:pushState()'>SPA</a>
61        <script>
62            function pushState() { history.pushState({}, '', 'wow.html') }
63        </script>
64    """
65    )
66    await page.click("a")
67    await page.wait_for_url("**/wow.html")
68    assert page.url == server.PREFIX + "/wow.html"
69
70
71async def test_wait_for_url_should_work_with_history_replace_state(page: Page, server):
72    await page.goto(server.EMPTY_PAGE)
73    await page.set_content(
74        """
75        <a onclick='javascript:replaceState()'>SPA</a>
76        <script>
77            function replaceState() { history.replaceState({}, '', '/replaced.html') }
78        </script>
79    """
80    )
81    await page.click("a")
82    await page.wait_for_url("**/replaced.html")
83    assert page.url == server.PREFIX + "/replaced.html"
84
85
86async def test_wait_for_url_should_work_with_dom_history_back_forward(
87    page: Page, server
88):
89    await page.goto(server.EMPTY_PAGE)
90    await page.set_content(
91        """
92      <a id=back onclick='javascript:go_back()'>back</a>
93      <a id=forward onclick='javascript:go_forward()'>forward</a>
94      <script>
95        function go_back() { history.back(); }
96        function go_forward() { history.forward(); }
97        history.pushState({}, '', '/first.html')
98        history.pushState({}, '', '/second.html')
99      </script>
100    """
101    )
102
103    assert page.url == server.PREFIX + "/second.html"
104
105    await page.click("a#back"),
106    await page.wait_for_url("**/first.html")
107    assert page.url == server.PREFIX + "/first.html"
108
109    await page.click("a#forward"),
110    await page.wait_for_url("**/second.html")
111    assert page.url == server.PREFIX + "/second.html"
112
113
114async def test_wait_for_url_should_work_with_url_match_for_same_document_navigations(
115    page: Page, server
116):
117    await page.goto(server.EMPTY_PAGE)
118    await page.evaluate("history.pushState({}, '', '/first.html')")
119    await page.evaluate("history.pushState({}, '', '/second.html')")
120    await page.evaluate("history.pushState({}, '', '/third.html')")
121    await page.wait_for_url(re.compile(r"third\.html"))
122    assert "/third.html" in page.url
123
124
125async def test_wait_for_url_should_work_with_commit(page: Page, server):
126    await page.goto(server.EMPTY_PAGE)
127    await page.evaluate(
128        "url => window.location.href = url", server.PREFIX + "/grid.html"
129    )
130    await page.wait_for_url("**/grid.html", wait_until="commit")
131    assert "grid.html" in page.url
132
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)