How to Setup Playwright with Java

Profile picture for user arilio666

In this article, we will be setting up a playwright with java, and this article will demonstrate eclipse ide.

1.) Maven

  • Create a maven project by clicking on new -> project.
  • Click on the maven project and hit next.
  • Select the quickstart archetype and name the project, and hit finish.

2.) POM

  <dependency>
     <groupId>com.microsoft.playwright</groupId>
     <artifactId>playwright</artifactId>
     <version>1.29.0</version>
   </dependency>
  
  •   In the POM.xml, add this dependency and save the XML file.

3.) Example

package week4.day2;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Play {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();
            page.navigate("http://www.programsbuzz.com/user/login");
            System.out.println(page.title());
        }
    }
}
  • The browser instance is created first out of chromium.
  • Then new page in that instance is created called page.
  • With the page, we navigated to the site.
  • We can see that playwright has been successfully installed and working as expected.