The navigate() command launches a new browser and opens the specified URL. The command takes a single string type parameter that is usually a URL of an application under test.
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
System.out.println("hello");
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.webkit().launch(
new BrowserType.LaunchOptions().setHeadless(false)
);
Page page = browser.newPage();
page.navigate("http://playwright.dev");
System.out.println(page.title());
}
}
}
The url() command is used to retrieve the URL of the webpage the user is currently accessing. The command doesn’t require any parameter and returns a string value.
public class Example {
public static void main(String[] args) {
System.out.println("hello");
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(page.url());
}
}
}
The output of url() command
http://playwright.dev
The title() command retrieves the title of the webpage the user is currently working on.
A null string is returned if the webpage has no title. The command doesn’t require any parameter and returns a trimmed string value.
public class Example {
public static void main(String[] args) {
System.out.println("hello");
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(page.title());
}
}
}
isClosed() method checks whether the current page(tab) is closed or not, if closed returns true otherwise false. You cannot check whether the browser is closed or not.
System.out.println(page.isClosed());
You can close the page using the close() method in the playwright. You can create a new page by accessing the browser object even if the page is closed
page.close();
Closing the browser is similar to Crossing a page, but we have to call the close method on the browser object.
Once the browser is closed you cannot access the page.
browser.close();
The complete Playwright java page-level commands program
public class Example {
public static void main(String[] args) {
System.out.println("hello");
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("Url of page: "+page.url());
System.out.println("Title of a page: " + page.title());
System.out.println("before closeing page: " +page.isClosed());
page.close();
System.out.println("after closeing page: " +page.isClosed());
browser.close();
}
}
}
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.