In this blog we will be learning how to do Data Driven testing using Data Providers and Login using parameters in testNG.XML . The use of Data Provider and Parameterization in testNG Selenium.
TestNG is preferred by developers for its ability to write powerful test cases with the help of annotations, grouping, and parametrizing. It covers all classifications of test automation like Unit testing, Functional testing, End-to-End, and integration testing.
Parameterization is require to create Data Driven Testing.
TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using@DataProvider
In @Parameter+TestNG.xml parameters can be placed in suite level and test level. If The Same parameter name is declared in both places; test level parameter will get preference over suit level parameter.
using @Parameter+TestNG.xml only one value can be set at a time, but @DataProvider return an 2d array of Object.
If DataProvider is present in the different class then the class where the test method resides,DataProvider should be static method.
There are two parameters supported by DataProvider are Method and ITestContext.
Data Driven testing using Data Provider
What are the Benefits of TestNG?
It gives the ability to produce HTML Reports of execution.
Annotations made testers life easy.
Test cases can be Grouped & Prioritized more easily.
Parallel testing is possible.
Generates Logs.
Data Parameterization is possible.
Parameterization in Selenium
Parameterization in Selenium is a process to parameterize the test scripts in order to pass multiple data to the application at runtime. It is a strategy of execution which automatically runs test cases multiple times using different values. The concept achieved by parameterizing the test scripts is called Data Driven Testing.
DataProviders pass the different parameters on a single test in a single execution
testNG.xml File
TestNG.xml
specify package name .class name
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Login Test Suite" >
<test name="Login To Application">
<classes>
<class name="loginUsingDataProvider.Login"></class>
</classes>
</test>
</suite>
Class.java
@DataProvider annotation helps us write data-driven test cases. The @DataProvider annotation enables us to run a test method multiple times by passing different data-sets. The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
DataProvider feature is one of the important features provided by TestNG. It is the second way of passing parameters to test methods. It allows the users to write data-driven tests in which we can run multiple times the same test method with different sets of test data. The sets of test data can be any format.
package loginUsingDataProvider;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Login {
String[][] data={
{"rekaharisri@gmail.com","Admi2/888'''"},
{"rekaha@gmail.com","Admin123///888'''"},
{"rekahari@gmail.com","Admi2/888'''"},
{"rekaharisri@gmail.com","Admin123///888'''"}
};
@DataProvider(name="loginData")
public String[][] loginDataProvider()
{
//return the test data
return data;
}
@Test(dataProvider ="loginData" )
public void loginWithBothCorrect(String userName,String passWord) {
WebDriverManager.chromedriver().setup();
WebDriverManager.chromedriver().clearDriverCache();
WebDriverManager.chromedriver().clearResolutionCache();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setScriptTimeout(Duration.ofSeconds(30));
chromeOptions.setPageLoadTimeout(Duration.ofMillis(30000));
chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(20));
chromeOptions.addArguments("--remote-allow-origins=*");
WebDriver driver=new ChromeDriver(chromeOptions);
//WebDriver driver=new ChromeDriver();
driver.get("https://practice.automationtesting.in/my-account/");
WebElement UsernameBox=driver.findElement(By.id("username"));
UsernameBox.sendKeys(userName);
WebElement passwordBox=driver.findElement(By.id("password"));
passwordBox.sendKeys(passWord);
WebElement login=driver.findElement(By.xpath("//*[@id=\'customer_login\']/div[1]/form/p[3]/input[3]"));
login.click();
driver.quit();
}
}
TestNG. xml file is a configuration file that helps in organizing our tests. It allows testers to create and handle multiple test classes, define test suites and tests. It makes a tester's job easier by controlling the execution of tests by putting all the test cases together and run it under one XML file.
We have to run the code through testNG.xml
class name =package name.class name
To execute the code , go to testNg.xml , Right click -> Run As -> TestNG Suite
Console Output
Parameterization
Here we are passing parameter values of username and password
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Login Test Suite" >
<test name="CorrectUserNameAndPassword">
<parameter name="username" value="rekaharisri@gmail.com"></parameter>
<parameter name="password" value="Admi2/888'''"></parameter>
<classes>
<class name="loginUsingParameterization.BothCorrect"></class>
</classes>
</test>
</suite>
@Parameters describes how to pass parameters to a @Test method. There are mainly two ways through which we can provide parameter values to test-methods: Through testng XML configuration file. Through DataProviders.
Parameters pass the parameters just once per execution in TestNG.
package loginUsingParameterization;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BothCorrect {
@Test
@Parameters({"username","password"})
public void loginWithBothCorrect(String userName,String passWord) {
// System.setProperty("webdriver.chrome.driver","C:\\Users\\Reka\\Drivers\\chromedriver.exe");
// WebDriver driver=new ChromeDriver();
WebDriverManager.chromedriver().setup();
WebDriverManager.chromedriver().clearDriverCache();
WebDriverManager.chromedriver().clearResolutionCache();
//WebDriverManager.chromedriver().browserVersion("110.0.0").setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setScriptTimeout(Duration.ofSeconds(30));
chromeOptions.setPageLoadTimeout(Duration.ofMillis(30000));
chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(20));
chromeOptions.addArguments("--remote-allow-origins=*");
WebDriver driver=new ChromeDriver(chromeOptions);
driver.get("https://practice.automationtesting.in/my-account/");
WebElement UsernameBox=driver.findElement(By.id("username"));
UsernameBox.sendKeys(userName);
WebElement passwordBox=driver.findElement(By.id("password"));
passwordBox.sendKeys(passWord);
WebElement login=driver.findElement(By.xpath("//*[@id=\'customer_login\']/div[1]/form/p[3]/input[3]"));
login.click();
driver.quit();
}
}
To execute the code , go to testNg.xml , Right click -> Run As -> TestNG Suite
Console Output
Similarly we can try with all possible options for login with
"CorrectUserName"
"CorrectPassword"
"BothWrong"
testng.xml File
<test name="Login To Application">
<classes>
<class name="loginUsingDataProvider.Login"></class>
</classes>
</test>
<test name="CorrectUserName">
<parameter name="username" value="rekaharisri@gmail.com"></parameter>
<parameter name="password" value="Admi2/888'''"></parameter>
<classes>
<class name="loginUsingParameterization.CorrectUserName"></class>
</classes>
</test>
<test name="CorrectPassword">
<parameter name="username" value="rekaha@gmail.com"></parameter>
<parameter name="password" value="Admin123///888'''"></parameter>
<classes>
<class name="loginUsingParameterization.CorrectPassword"></class>
</classes>
</test>
<test name="BothWrong">
<parameter name="username" value="rekahari@gmail.com"></parameter>
<parameter name="password" value="Admi2/888'''"></parameter>
<classes>
<class name="loginUsingParameterization.BothWrong"></class>
</classes>
</test>
Conclusion:
I hope, This article will help you to understand how to do Data Driven using Data provider and testNG.xml parameters.
You must have got an idea on the topics explained in this blog. Lets explore more and learn New Topics.
Happy Learning
Comentários