Verify URL in Playwright Java

Profile picture for user arilio666

In this article, we will see how we can verify URLs in playwright java. From the page object, after we create the browser context, we can get the current URL using url() method.

{
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
            Page page = browser.newPage();
            page.navigate("http://www.programsbuzz.com/user/login");
            page.locator("#edit-name").type("Naruto");
            page.locator("#edit-pass").type("uzumaki");
            String currentUrl = page.url();
            String expectedUrl = "https://www.programsbuzzz.com/user/login";
            if (currentUrl.equals(expectedUrl)) {
                System.out.println("URL is correct: " + currentUrl);
            } else {
                System.out.println("URL is incorrect. Expected: " + expectedUrl + ", but got: " + currentUrl);
            }
            // System.out.println(currentUrl);
            browser.close();
            playwright.close();
        }
    }
}


  • We have stored the expected and currentUrl in a variable, and with the help of a simple if statement, we used equals to validate both the variables which have stored URLs.
  • As you can see from the console, we have purposefully given the wrong URL and made it fail.