Search code examples
pythonplaywrightplaywright-python

How to wait for URL to contain text using Playwright


I'm clicking on an element on my page and being redirected to another page.

How to test using Playwright and Expect that the url was updated?

def is_url_contains(self, text_in_url):
    try:
        expect(self.page).to_have_url(text_in_url, timeout=10 * 1000)
        return True
    except AssertionError:
        return False

Solution

  • Docs

    import re
        
        import pytest
        import unittest
        
        from playwright.sync_api import Page, expect
        
        
        class MyTest(unittest.TestCase):
            @pytest.fixture(autouse=True)
            def setup(self, page: Page):
                self.page = page
        
            def test_foobar(self):
                self.page.goto("https://www.wikipedia.org/")
        
                # Create a locator
                en_wikipedia = self.page.get_by_title("English — Wikipedia — The Free Encyclopedia")
        
                # Click link to start navigation
                en_wikipedia.click()
        
                # Expects the URL to contain Main_Page.
                expect(self.page).to_have_url(re.compile(".*/Main_Page"))