Data Driven Testing Using JSON File and TESTNG Data Provider

If you want to parameterize your test with multiple sets of data then it is obvious that you have to take the help of @DataProvider annotation of TestNG. In this article, we are going to see how can we fetch data from a JSON File and do parameterization using @DataProvider annotation.

Dependency Required:

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>

</dependency>

JSON File:

{
"url": "http://newtours.demoaut.com",
"loginCredentials": [
{
"username": "mercury",
"password": "mercury",
"loginSuccessTitle": "Find a Flight: Mercury Tours:"
},
{
"username": "mercury",
"password": "!@#$%^&",
"loginSuccessTitle": "Sign-on: Mercury Tours"
},
{
"username": "@$%^&*(",
"password": "mercury",
"loginSuccessTitle": "Sign-on: Mercury Tours"
}
]
}

Test Script:

package jsonFileReader;

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class JSONDataProvider {


WebDriver driver;

@BeforeMethod
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options); 
}

@Test(dataProvider="loginData")
public void verifyLoginFunctionality(String testdata) {
try {


String data[] = testdata.split(",");
driver.get(data[0]);

driver.findElement(By.name("userName")).sendKeys(data[1] + Keys.TAB);
driver.findElement(By.name("password")).sendKeys(data[2] + Keys.ENTER);

Thread.sleep(5000);

Assert.assertEquals(driver.getTitle(), data[3]);

}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}

@DataProvider(name="loginData")
public Object[] loginDataProvider() {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("./src/test/resources/loginDataProvider.json"));
JSONObject jsonObject = (JSONObject)obj;
JSONArray loginDataArray = (JSONArray)jsonObject.get("loginCredentials");
String url = (String) jsonObject.get("url");

Object[] loginCredentials = new Object[loginDataArray.size()];


for(int i=0;i<loginDataArray.size();i++) {
JSONObject testData = (JSONObject)loginDataArray.get(i);
String username = (String) testData.get("username");
String password = (String) testData.get("password");
String expTitle = (String) testData.get("loginSuccessTitle");

loginCredentials[i] = url+","+username+","+password+","+expTitle;

}

return loginCredentials;


}catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
}

@AfterMethod
public void tearDown() {
if(driver !=null) {
driver.quit();
}
}

}













Comments

Popular posts from this blog

Encoding and Decoding in Selenium using Base64 Class of Java

Parallel Execution In TestNG Framework Using ThreadLocal Concept

How to handle calendar or date picker using dynamic XPATH?