We often see scenarios where we might want to navigate from the landing URL and then go back or forward. In such cases, we can use navigation methods based on the browser history. We can use goBack() and goForward(), reload() methods without specifying the URLs.

  • goBack() - navigates back when we have at least opened an URL
  • goForward() - navigates forward when we have at least used the goBack() method once.
  • reload() - Refreshes the current page.
import com.microsoft.playwright.*;

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(
    		  new BrowserType.LaunchOptions().setHeadless(false)
    		  );
      Page page = browser.newPage();
      page.navigate("http://playwright.dev");
      System.out.println("Title of a page: " + page.title());

      page.navigate("http://bing.com");
      System.out.println("Title of a page: " + page.title());
      
      page.goBack();
      System.out.println("Title of a page: " + page.title());
      
      page.goForward();
      System.out.println("Title of a page: " + page.title());
      
      page.reload();
      System.out.println("Title of a page: " + page.title());
      
      page.close();
      browser.close();
    }
  }
}

The output of navigation commands

Title of a page: Fast and reliable end-to-end testing for modern web apps | Playwright
Title of a page: Bing
Title of a page: Fast and reliable end-to-end testing for modern web apps | Playwright
Title of a page: Bing
Title of a page: Bing

GobackOptions and GoForwardOptions in Playwright Java

setTimeout:

Amount of time we can give the playwright to load the page, if the timeout is reached then the playwright throws an exception.

setWaitUntil:

Wait till the page reaches a state.

  • LOAD - Give up the control when the webpage starts loading.
  • DOMCONTENTLOADED - Give up the control when the DOM content webpage loaded
  • NETWORKIDLE - Give up the control when the NO more requests/responses are active.
page.goBack(new GoBackOptions().setTimeout(100));
page.goBack(new GoBackOptions().setWaitUntil(WaitUntilState.NETWORKIDLE));

page.goForward(new GoForwardOptions().setTimeout(100));
page.goForward(new GoForwardOptions().setWaitUntil(WaitUntilState.NETWORKIDLE));
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions