Data Driven Framework Part 1

What is Data Driven Testing?

Data-driven is a test automation framework which stores test data in a table or spreadsheet format. This allows automation engineers to have a single test script which can execute tests for all the test data in the table.


Why Data Driven Testing?

Example 1:
We want to test the login system with multiple input fields with 100 different data sets.
To test this, you can take following different approaches:
Approach 1) Create 100 scripts one for each dataset and runs each test separately one by one.
Approach 2) Manually change the value in the test script and run it several times.
Approach 3) Import the data from the excel sheet. Fetch test data from excel rows one by one and execute the script.
In the given three scenarios first two are laborious and time-consuming. Therefore, it is ideal to follow the third approach.
Thus, the third approach is nothing but a Data-Driven framework.

Example 2: Test different products in e-commerce application.

Advantages of Data Driven Framework
  • Advantages of using Data Driven Test Framework
  • Re-usability of code
  • Improves test coverage
  • Faster Execution
  • Less maintenance
  • Permits better error handling
How to Implement?

Using DataProvider in TestNG
An important features provided by TestNG is the testng DataProvider feature. It helps you to write data-driven tests which essentially means that same test method can be run multiple times with different data-setsTo test this.

To use the DataProvider feature in the tests, you have to declare a method annotated by @DataProvider and then use the said method in the test method using the ‘dataProvider‘ attribute in the @Test annotation.
Data provider returns a two-dimensional JAVA object and test method will invoke M times in a M*N type of object array. 

Lets have a look Data driven example without using excel sheet just to understand the data driven test --  Data driven testing of login functionality.

Base Class: In this base class we have created following methods

setup() - to launch the browser and navigate to application
tearDown() - quit the browser
getData() - to setup the test data using 2-D Array.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import com.utility.NewExcelLibrary;

public class BaseClass {
 public WebDriver driver;
 NewExcelLibrary obj= new NewExcelLibrary("D:\\Workspace_Eclipse\\DataDriven\\TestData\\User.xlsx");
 
 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://opensource-demo.orangehrmlive.com/");
 }

 @AfterMethod
 public void tearDown() {
  driver.quit();
 }

 @DataProvider(name = "Credentials")
 public Object[][] getData() {

  Object[][] data = new Object[3][2];

  data[0][0] = "admin";
  data[0][1] = "admin123";

  data[1][0] = "admin1";
  data[1][1] = "admin123";

  data[2][0] = "admin2";
  data[2][1] = "admin";

  return data;
 }
}

Test Class: Supply the dataProvider name in test method
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class DataDrivenTest extends BaseClass {

 @Test(dataProvider = "Credentials")
 public void loginTest(String username,String password) {
  
  driver.findElement(By.id("txtUsername")).sendKeys(username);
  driver.findElement(By.id("txtPassword")).sendKeys(password);
  driver.findElement(By.id("btnLogin")).click();
  String actualURL=driver.getCurrentUrl();
  String expectedURL="https://opensource-demo.orangehrmlive.com/index.php/dashboard";
  Assert.assertEquals(actualURL, expectedURL);
 }
}

Output: Refer the below output, you can see our test executed with different set of test data
Please refer the below YouTube video to understand better:

No comments:

Post a Comment