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.
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
Amount of time we can give the playwright to load the page, if the timeout is reached then the playwright throws an exception.
Wait till the page reaches a state.
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));
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.