Playwright Java Tutorial: Web Automation Testing | How to perform browser navigation?

Mohammad Faisal Khatri
8 min readMar 8, 2024

--

In the previous tutorial, we learnt about writing and running the tests on Chrome, Firefox, and Edge browsers using Playwright Java.

In this blog, we will learn how to perform browser navigations in Playwright framework using Java.

The following topics will be covered as part of this blog using Playwright Java:

  1. How to perform the back navigation in the browser?
  2. How to perform the forward navigation in the browser?
  3. How to refresh the web page?
  4. How to get the page Url?

Let’s first get a brief insight on the Playwright’s browser navigations method in Java that can help us in performing the required browser actions.

Playwright’s browser navigations methods in Java

goBack()

The goBack() method in Playwright helps to perform the browser’s back navigation action. It allows to navigate to the previous page in the browser history.

Syntax

goBack() method supports the following options which are optional in the method parameter.

  • setTimeout()

The maximum operation time in milliseconds can be supplied using this optional parameter. The default operation time is 30 seconds. To disable the timeout “0” should be passed.

  • setWaitUntil()

This optional parameter can be used to check if the operation succeeded. Enums — LOAD, DOMCONTENTLOADED, NETWORKIDLE, COMMIT can be used in this optional parameter. By default the LOAD state is used.

Details of the enums used to check the goBack operation succeeded:

  • 'domcontentloaded' - This option considers the operation to be finished when the DOMContentLoaded event is fired.
  • 'load' - This option considers the operation to be to be finished when the load event is fired.
  • 'networkidle' - This option considers operation to be finished when there are no network connections for at least 500 ms. Using this option is DISCOURAGED by Playwright and it is recommended to use web assertions to assess page readiness instead.
  • 'commit' - Using this option, operation is considered to be finished when network response is received and the document has started loading.

goForward()

The goForward() method in Playwright allows to perform the browser’s forward navigation action. In case if the forward navigation action cannot be performed, it will return null. It allows to navigate to the next page in the browser history.

Syntax

goForward() method supports the following options which are optional in the method parameter.

  • setTimeout()

The maximum operation time in milliseconds can be supplied using this optional parameter. The default operation time is 30 seconds. To disable the timeout “0” should be passed.

  • setWaitUntil()

This optional parameter can be used to check if the operation succeeded. Enums — LOAD, DOMCONTENTLOADED, NETWORKIDLE, COMMIT can be used in this optional parameter. By default the LOAD state is used.

Details of the enums used to check the goBack operation succeeded:

  • 'domcontentloaded' - This option considers the operation to be finished when the DOMContentLoaded event is fired.
  • 'load' - This option considers the operation to be to be finished when the load event is fired.
  • 'networkidle' - This option considers operation to be finished when there are no network connections for at least 500 ms. Using this option is DISCOURAGED by Playwright and it is recommended to use web assertions to assess page readiness instead.
  • 'commit' - Using this option, operation is considered to be finished when network response is received and the document has started loading.

reload()

The reload() method in Playwright is used for refreshing the web page. This method performs the same action like the user would refresh the current web page.

Syntax

reload() method supports the following options which are optional in the method parameter.

  • setTimeout()

The maximum operation time in milliseconds can be supplied using this optional parameter. The default operation time is 30 seconds. To disable the timeout “0” should be passed.

  • setWaitUntil()

This optional parameter can be used to check if the operation succeeded. Enums — LOAD, DOMCONTENTLOADED, NETWORKIDLE, COMMIT can be used in this optional parameter. By default the LOAD state is used.

Details of the enums used to check the goBack operation succeeded:

  • 'domcontentloaded' - This option considers the operation to be finished when the DOMContentLoaded event is fired.
  • 'load' - This option considers the operation to be to be finished when the load event is fired.
  • 'networkidle' - This option considers operation to be finished when there are no network connections for at least 500 ms. Using this option is DISCOURAGED by Playwright and it is recommended to use web assertions to assess page readiness instead.
  • 'commit' - Using this option, operation is considered to be finished when network response is received and the document has started loading.

Getting the Page URL

url()

The url() method in Playwright can be used to get the URL of the current page. It returns the current page’s URL in String format.

Syntax

Demo

Let’s now discuss the test scenario we will be using in the demo to showcase the browser navigation commands and getting the page url.

If you are a beginner in Playwright with Java, checkout the following tutorial blog for installation and setting up of the project

Test Scenario

  1. Navigate to the Internet Herokuapp demo website

2. Click on the Challenging DOM link to navigate to its respective page

3. Check that the page header of Challenging DOM page is displayed as “Challenging DOM”

4. Perform the back navigation on the browser, it should take user back to the home page of the website. On the home page, check that the page header is displayed as “Available Examples”.

5. Perform the forward navigation on the browser, it should again take the user to the “Challenging DOM” where the page header should be displayed as “Challenging DOM”.

6. Get the current page’s Url.

Implementation of the Test Scenarios

The following test method named testBrowserNavigation() is created inside the PlaywrightDemoTests class. This method will be used to demo the working of the browser navigations methods using the test scenario we discussed.

FileName: PlaywrightDemoTests.java

    @Test
public void testBrowserNavigation() {
final Playwright playwright = Playwright.create();
final Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
final Page page = browser.newPage();
page.navigate("https://the-internet.herokuapp.com/");

final String homePageHeader = page.locator("h2").textContent();
assertEquals(homePageHeader, "Available Examples");

page.navigate("https://the-internet.herokuapp.com/challenging_dom");
final String challengingDomPageHeader = page.locator("h3").textContent();
assertEquals(challengingDomPageHeader, "Challenging DOM");

page.goBack();
assertEquals(homePageHeader, "Available Examples");

page.goForward();
assertEquals(challengingDomPageHeader, "Challenging DOM");

page.reload();
assertEquals(challengingDomPageHeader, "Challenging DOM");

final String currentPageUrl = page.url();
final String websiteLink = "https://the-internet.herokuapp.com/challenging_dom";
assertEquals(currentPageUrl, websiteLink);

browser.close();

}

The following first four lines of the testBrowserNavigation() method will create an instance of Playwright, launch the Chrome browser in headed mode and create the instance of the Page interface in Playwright that could further be used for browser navigation. Next, it will navigate to the internet herokuapp website.

We will be getting the text of the page header next, as shown in the screenshot below using the tagname “h2”.

This home page header will be checked using the assertEquals() method to confirm that we have landed on the home page. Next, we will be navigating to the Challenging DOM page and again will be verifying the page header to check that we land on the correct page.

This navigation is performed so we have two different pages links stored in browser history as it will help us in running the browser navigations back and forward.

The following code will allow us perform the navigations to the respective pages and perform assertions.

After landing on the Challenging DOM page, the goBack() method will be called which should take us to the home page. We will be again running the assertion by checking that the page header equals “Available Examples” to confirm that the browser’s back navigation was performed successfully.

Once we are on the home page, the goForward() method will be called which should take us to the Challenging DOM page. Here, an assertion will be run again to check that we correctly landed on the desired page.

The browser refresh action will be performed next using the reload() method. As this method will just refresh the page, we will be performing an an assertion that the page header displays the same heading “Challenging DOM” after the page is refreshed successfully.

This brings us to the end of the browser navigation methods in Playwright. We will now check the current Url of the web page by calling the url() method and make an assertion to verify that the current Url points to the Challenging DOM web page.

Finally, calling the browser.close() method which will close the Playwright session marking an end to the automated test case.

Test Execution

The following is the screenshot of the tests executed on the local machine using IntelliJ IDE:

Conclusion

In this blog, we learnt how to run the different browser navigation methods using Playwright. While using Playwright, I experienced it to be very intuitive framework that anyone can learn, the only condition is that we need to keep the documentation handy in case we get stuck.

I would recommend to try and execute these web automation tests at your end as well.

Happy Testing!!

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.