Friday, April 11, 2025

Start Playwirght with java

Begin your journey with playwright+java. 

Below is the basic setup of playwright with java and sample basic code.


 Do the practice playwright with java. 

1. Open VS Code. 

2. Press control+shift+P

3. Create maven project. (select archtype).

4. Once project is created, open pom.xml and add below depeandacy and plugin:         

    <!-- additional added dep -->

    <dependency>

  <groupId>com.microsoft.playwright</groupId>

  <artifactId>playwright</artifactId>

  <version>1.41.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>7.11.0</version>

    <scope>test</scope>

</dependency>


below is for plugin: 

<plugin>

    <groupId>org.codehaus.mojo</groupId>

    <artifactId>exec-maven-plugin</artifactId>

    <version>3.1.0</version>

  </plugin>

5. Write below code and run it

package com.playwright.java.demo;


import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

/**

 * Unit test for simple App.

 */

public class AppTest 

{

    /**

     * Rigorous Test :-)

     */

    @Test

    public void shouldAnswerWithTrue()

    {  try(Playwright playwright = Playwright.create()) {

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

             Page page = browser.newPage();

             page.navigate("https://example.com");

             System.out.println("Page Title: " + page.title());

             browser.close();

            System.out.println("Hello World!");

    }

}

}


                              

No comments:

Post a Comment