In this blog we will be utilizing Playwright Java for the execution of Data Driven Testing with ease.

Topics that we will cover:

  • Parameterize Test using dataProvider in same file
  • Parameterize Test using dataProvider in separate file
  • Source code (dataProvider in same file)
  • Source code (dataProvider in different file)
  • Source code (external dataProvider)

Parameterize Test using dataProvider in same file  

On certain occasions, we might have the need to perform a Test using various sets of data. 

To understand this, navigate to

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

 It may be necessary to use various user IDs when logging in for different tests. For example, in first test we may want to login with user id “TestUser1”, in the second test we may want to login as “TestUser2” and so on.... 

Automating repetitive login functionality for various user ids is redundant, as the code logic remains constant while only the login id varies. It is illogical to repeatedly automate the same process.

Instead, our approach would be to write code for login functionality and then parameterize the code for multiple user ids. 

Let us see how to perform data driven testing. 

Inspect ‘Login’ field 

 The idea is to use @DataProvider TestNg annotation that will contain an ‘Object’ array holding 3 test user names

 This ‘dataProvider’ can than be used in our @Test method (‘username’ will capture multiple user ids during runtime)

 Complete code shown below

 Execute.

Test executes 3 times (once for each test id) 

So this way we can perform data driven testing using playwright.

Parameterize Test using dataProvider in separate file

If you want, the test data can also be kept in a separate file

 Comment the below piece of code from previous code

Execute. 

Test gets executed 3 times

This is how data driven testing can be performed.

Source code (dataProvider in same file)

package com.rsa.playwrightjava;


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 Blog24_dataProviderSameFile {

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();

}


}


Source code (dataProvider in different file)

package com.rsa.playwrightjava;


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 Blog24_dataProviderExternalFile {

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 = Blog24_DataProvider.class)

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();

}


}


Source code (external dataProvider)

package com.rsa.playwrightjava;


import org.testng.annotations.DataProvider;


public class Blog24_DataProvider {

@DataProvider(name="TestData")

public Object[] getData(){

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

{"TestUser1"},

{"TestUser2"},

{"TestUser3"}

};

return obj;

}


}


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"}