In this blog post, you will learn how to create the first playwright-Java test, execute the test and optimize the code.

Topics that we will cover:

  • Create playwright java program
  • Execute the first Playwright Test
  • Refactor (refine) the code
  • Source code (without refactoring)
  • Source code (with refactoring)

Create playwright java program 

Create a simple java class under src/test/java

 Create @Test and import required packages.
We than create an object of Playwright. Ensure there are no compile errors

If there are no errors, we can continue.
The code below is self-explanatory where we are creating the Chromium Browser type object (line #13) and then launching the Chromium Browser (line #14).

Next, we create a new page (line #15) on this launched browser and navigate to the webpage (line #16). 

Finally, we print the page title (line #17)  

 Execute the first Playwright Test

Run the test. 

When we execute the playwright test for the first time, the browsers get downloaded as can be seen below. 

We can see chromium, webkit, firefox etc browsers getting downloaded to our local machine 

 The title of the page is printed as can be seen above. 

This page title (that gets printed) matches with the actual page title, as can be seen below 

 Also, the junit test passes.

 Refactor (refine) the code

To refactor the code, comment lines#13 and 14. 

We now append the respective methods (method to launch chromium) in line#15

 Run the code.
The title of the page gets printed

 Source code (without refactoring)

package com.rsa.playwrightjava;

import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

public class PlaywrightJavaTest {

        @Test

        public void PlaywrightJTest() {

                Playwright pt = Playwright.create();

                BrowserType btype = pt.chromium();

                Browser b = btype.launch();

                Page pg = b.newPage();

        pg.navigate("https://rahulshettyacademy.com/");

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

        }

}

Source code (with refactoring)

package com.rsa.playwrightjava;

import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;

public class PlaywrightJavaTest {

        @Test

        public void PlaywrightJTest() {

                Playwright pt = Playwright.create();

                //BrowserType btype = pt.chromium();
                //Browser b = btype.launch();

                Page pg = pt.chromium().launch().newPage();
    pg.navigate("https://rahulshettyacademy.com/mentorship");

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

        }

}

So this is how we execute the playwright code.

Thanks!


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}