Create TestNg Test using Playwright Java

Tutorial 21​​ ​​ Create TestNg Test​​ using​​ Playwright​​ Java​​ 

What you will Learn​​ in this​​ blog:

  • How to​​ create TestNG test​​ using Playwright Java

  • Solve 2 exceptions (unsupportedClassVersion, IllegalAccess)

  • Code snippets

How to​​ create TestNG test​​ using Playwright

First of all,​​ we need to include TestNg dependency in pom.xml file

Goto​​ https://mvnrepository.com/artifact/org.testng/testng​​ ​​ 

Copy dependency

Paste in xml


Save.​​ 

The testng jar would be seen under maven folder as shown below

One more thing to note here.​​ 

Notice below. The scope is ‘test’. What this means is that,​​ the TestNg jar file will be avaiable only for the src/test/java project​​ 


In other words, the TestNg jar will​​ not​​ be recognized under src/main/java. If we want TestNg jar to be recognized under src/main/java, we have to remove the test scope from xml. ​​ Let us leave the scope as it is.

Let us now​​ create TestNg test by using the above TestNg library.

Create a package under src/test/java and further create a java file

​​ 

​​ Create 3 annotations:​​ 
@BeforeMethod, @Test, @AfterMethod

Inside @BeforeMethod, we would be initializing the stuff (creating PW instance, browser instance etc)

Next, under @Test method, we will put our actual Test​​ 

The reason we are getting error is because, the ‘page’ object that we created under @BeforeMethod is local to @BeforeMethod. The @Test method does not recognize the ‘page’ object.

To correct this error, create the instance variables. These can now be accessed throught the class

Change the @BeforeMethod accordingly

Notice that the error is no more under @Test​​ 

Next, we can close the browser and page instance under @AfterMethod

Save the code.

Next, run as TestNG Test

You might get below exception (unsupportedClassVersion error)

To resolve this, download​​ and install​​ a higher version​​ jre​​ viz jre-11

Next, go to project properties, select JRE and Edit

Select as shown below

Apply and Close.

Run​​ 

This time we get another exception “IllegalAccessError”

The reason being, the environment variable might be pointing to old jdk, so lets correct it

Similarly change JAVA_HOME

Next, Windows > Preferences >Expand Java > Installed JREs > make sure JRE-11 is selected​​ 

Similarly, the​​ compiler should be greater than​​ 1.8

Apply and Close

Exit eclipse

Open eclipse

Next, run as TestNG Test

Notice that our TestNg test is pass

Code snippet

package​​ com.testng;

 

import​​ org.testng.annotations.AfterMethod;

import​​ org.testng.annotations.BeforeMethod;

import​​ org.testng.annotations.Test;

import​​ com.microsoft.playwright.Browser;

import​​ com.microsoft.playwright.BrowserContext;

import​​ com.microsoft.playwright.BrowserType;

import​​ com.microsoft.playwright.Page;

import​​ com.microsoft.playwright.Playwright;

 

public​​ class​​ PWTestNg {

private​​ Browser​​ browser;

private​​ BrowserContext​​ browserContext;

private​​ Page​​ page;

@BeforeMethod

public​​ void​​ setUp() {

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

browser​​ =​​ pw.chromium().launch(new​​ BrowserType.LaunchOptions().setHeadless(false));

browserContext​​ =​​ browser.newContext();

page​​ =​​ browserContext.newPage();

 }

@Test

public​​ void​​ test1() {

page.navigate("https://www.way2automation.com/angularjs-protractor/registeration/#/login");

page.locator("input >> nth=0").fill("angular");

page.locator("input >> nth=1").fill("password");

page.locator("input >> nth=2").fill("angular");

page.locator("button").click();

 }

@AfterMethod

public​​ void​​ tearDown() {

browser.close();

page.close();

 }

 

}

 

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 - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024