Parameterization (Data Driven Testing) using Playwright Java

Tutorial 22​​ ​​ Parameterization (Data Driven Testing)​​ using​​ Playwright​​ Java​​ 

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

  • Parameterize a Test​​ using PW-java​​ (dataProvider in same file)

  • Parameterize a Test using PW-java (dataProvider in​​ different​​ file)

  • Code snippet

Parameterize a Test​​ using​​ PW-java​​ (dataProvider in same file)​​ ​​ 

Sometimes​​ we may want to execute a Test with multiple sets of data.​​ 

As an example, let us launch

http://zero.webappsecurity.com/login.html​​ 

We have a ‘Login’ field here. We may want to login with different user ids for​​ different tests. It does not make​​ sense to automate the same login functionality again and again for different user ids (since the code logic would be same).

So instead, what we do is that, we write a piece of code for login functionality and then parametrize it for different user ids.​​ 

Before we practically see this, let us first inspect the above ‘Login’ field

Make a note of ‘id’, we would be using it in our script to locate this field.

To begin with, we will first create a​​ @DataProvider TestNg annotation that will contain an ‘Object’ array​​ holding the 3 test user names

We will​​ now use this ‘dataProvider’ in our @Test method​​ (the ‘username’ variable will hold the different user ids at runtime)

So the complete​​ code looks like below

Let us save and execute, notice below that the test gets executed 3 times (for each of the 3 user ids)​​ 

See the output below

This is how we can easily parametrize our tests in playwright.

Parameterize a Test using PW-java (dataProvider in different file)

We can keep our dataProvider in a separate class file

In this case, we can add the below argument to fetch the data from external file​​ 

Save an execute. Notice that the test gets executed 3 times

Code snippet​​ (dataProvider in same file)

package​​ com.testng;

 

import​​ org.testng.annotations.AfterMethod;

import​​ org.testng.annotations.BeforeMethod;

import​​ org.testng.annotations.DataProvider;

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(dataProvider =​​ "TestData")

public​​ void​​ test1(String​​ username) {

page.navigate("http://zero.webappsecurity.com/login.html");

page.locator("#user_login").fill(username);

 }

@DataProvider(name="TestData")

public​​ Object[] getData(){

 Object[][]​​ obj​​ =​​ new​​ Object[][] {

 {"TestUser1"},

 {"TestUser2"},

 {"TestUser3"}

 };

return​​ obj;

 }

@AfterMethod

public​​ void​​ tearDown() {

//browser.close();

//page.close();

 }

 

}

 

Code snippet (dataProvider in different file)

package​​ com.testng;

 

import​​ org.testng.annotations.AfterMethod;

import​​ org.testng.annotations.BeforeMethod;

import​​ org.testng.annotations.DataProvider;

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(dataProvider =​​ "TestData", dataProviderClass = ExternalDataProvider.class)

public​​ void​​ test1(String​​ username) {

page.navigate("http://zero.webappsecurity.com/login.html");

page.locator("#user_login").fill(username);

 }

@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 31st DEC 2024