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();
            String currentUrl = page.url();
            String expectedUrl = "";
            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);

  • 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.