Create and Execute First Playwright-Java Test

Tutorial 2 – Create and Execute First Playwright-Java Test

What you will Learn:

  • Create first​​ playwright java test

  • Execute test

  • Resolve exception

  • Optimising the code

  • Code snippets

Create first playwright java test​​ 

Create a package under src/test/java and create a simple java class

Create a simple @Test and import the required packages.​​ Create an object of Playwright and make sure there are no compile time errors

If there is no error, we can proceed further.​​ 

Below code is self-explanatory wherein we are creating the chromium browser type object (line#13) and then launching the chromium browser (line#14).​​ 

We then create a new page on this launched browser (line#15) and then navigate to a webpage (line#16). Finally we print the title of the page (line#17) ​​ 

Execute Test

Save and run the junit test. The below exception might be thrown

java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineExecutionListener

Resolve exception

To resolve this exception,​​ we need to add the ‘junit-jupiter-engine’ dependency in xml

https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine/5.9.0​​ 

Copy this and​​ add this dependency

Save xml.

Execute the junit test once more.​​ 

Notice that, there is no exception this time. The page title gets printed​​ 

The page title (printed in the console) matches with the actual page title, see below

https://www.way2automation.com/lifetime-membership-club/​​ 

The junit test passes

Optimising the code

We can optimise the code by commenting lines#13 and 14. Append their respective methods in line#15 itself

Save and execute. The page title gets printed

Code snippet (without optimization)

package​​ com.w2a.pwjava;

 

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​​ PWTest {

@Test

public​​ void​​ PWFirstTest() {

 Playwright​​ pw​​ = Playwright.create();

 BrowserType​​ browsertype​​ =​​ pw.chromium();

 Browser​​ browser​​ =​​ browsertype.launch();

 Page​​ page​​ =​​ browser.newPage();

page.navigate("https://www.way2automation.com/lifetime-membership-club/");

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

 }

}

 

Code snippet (with optimization)

package​​ com.w2a.pwjava;

 

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​​ PWTest {

@Test

public​​ void​​ PWFirstTest() {

 Playwright​​ pw​​ = Playwright.create();

//BrowserType​​ browsertype​​ = pw.chromium();

//Browser browser = browsertype.launch();

 Page​​ page​​ =​​ pw.chromium().launch().newPage();

page.navigate("https://www.way2automation.com/lifetime-membership-club/");

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

 }

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024