top of page
Search

Parameterization/Data driven testing in Selenium TestNG using testng.xml file

  • qatestingthreesixt
  • Dec 1, 2021
  • 1 min read

Data-Driven testing as the name suggests is a test driven by the Data. For ex. You have a user detail form where you need to enter details of multiple users and save them. Here if you have 5 different user data available and you have to write automation cases for these, you may end up writing 5 different automation test cases(one for each user data).


If you apply a Data-Driven approach you will end up with only one test-case, that is filling the form with user data and then submitting it. The test case will get executed based on the data provided to it. In this case it will be 5 and hence the test case will get executed 5 times with different data-set.


The advantage of using a Data-driven approach is that you reduce your effort in writing/maintaing test-cases for your different type of data. In case of additions or deletion of new/old entries , you just have to change the data and not your actual test-case.Let’s use following example:


Below is the example for testng.xml file: <?xml version=”1.0″ encoding=”UTF-8″?>

<suite name=”Simple Suite”>

<test name=”Simple Test”>

<parameter name=”userid” value=”userID” />

<parameter name=”password” value=”abcd” />

<classes>

<class name=“annotate.datapro” />

</classes>

</test>

</suite>


Please change your code accordingly:


CODE:

package Testng_Package;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;


public class Sample_Login {

WebDriver driver = new FirefoxDriver();

@BeforeTest

public void setup() throws Exception {

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver.get(“paste url”);

}

@AfterTest

public void tearDown() throws Exception {

driver.quit();

}

@Test

@Parameters(“userid”)

public void LogIn_Test(String Userid, String Password) {

driver.findElement(By.xpath(“ //input[@name=’Userid’]”)).clear();

driver.findElement(By.xpath(“ //input[@name=’Password’]”)).clear();

driver.findElement(By.xpath(“ //input[@name=’Userid’]”)).sendKeys(Userid);

driver.findElement(By.xpath(“ //input[@name=’Password’]”)).sendKeys(Password);

driver.findElement(By.xpath(“ //input[@value=’Login’]”)).click();

String alrt = driver.switchTo().alert().getText(); driver.switchTo().alert().accept(); System.out.println(alrt);

}

}


Comments


Contact Us

PR World Trade Center, 108, near bus stand, Jalandhar, Punjab 144001

Phone or Whatsapp

+91-7652933997

     

bottom of page