Wednesday 8 February 2023

Playwright: Hello World application

In this post, I am going to explain, how to open an url in a browser and print the title using Playwright library.

 

Step 1: Get an instance of Playwright.

Playwright playwright = Playwright.create()

 

‘Playwright.create()’ method launches new Playwright driver process and connects to it. Make sure that you called ‘Playwright.close()’ method, when this instance is no longer needed. As ‘Playwright’ implements AutoCloseable interface, you can use it in try-with resources statement to auto close.

 

Step 2: Get an instance of the browser.

Playwright provided below methods to get an instance of the Browser.

a.   chromium() : Returned object can be used to launch or connect to Chromium. It is used for Google Chrome, Microsoft Edge and other Chromium-based browsers.

b.   firefox() : Returned object can be used to launch or connect to Firefox

c.    webkit() : Returned object can be used to launch or connect to WebKit


Browser browser = playwright.chromium().launch();

Step 3: Create a new page in the browser context.

Page page = browser.newPage();

Step 4: Get the page response by navigating to an url.

page.navigate("https://self-learning-java-tutorial.blogspot.com/");

Step 5: Print page title.

System.out.println(page.title());

Find the below working application.

 


HelloWorld.java

package com.sample.app;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class HelloWorld {

	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium().launch();
			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");
			System.out.println(page.title());
		}
	}

}

Output

Programming for beginners

Playwright runs in headless mode by default, you can disable it by setting headless property to false.

Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));

DisableHeadless.java

package com.sample.app;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class DisableHeadless {

	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");
			System.out.println(page.title());
		}
	}

}

Using 'setSlowMo' method, we can slow down Playwright operations by the specified amount of milliseconds. It is useful, if you want to see what is going on.


Previous                                                 Next                                                 Home

No comments:

Post a Comment