Tuesday, 2 April 2013

Why need a data driven framework?

Understanding Data Driven Testing

Data driven testing generally means executing tests by using multiple sets of data which is separate from test cases. Let us try to understand it with the help of an example:

We need to write a test for let say a sign-up page that asks for 3 things:
1) First Name
2) Last Name
3) Password

Let's try to find out some scenario's for the above mentioned situation:

Scenario 1:   First name is blank. Last name and password are provided.

Scenario 2:   First name and password is provided. Last name left blank.
Scenario 3:   First and last name are provided. Password is not provided.

Now there are two routes to achieve the above functionality:

Route 1: We can write individual tests for each of the scenario above.
Route 2: We can write a single test with different test data for each scenario.

Route 2 is preferable as there will be only one test that will deal with all the scenario's. Here is how the test would be written traditionally:

@Test
public void testSignUp()
{
      //Scenario 1
      String firstName = "";
      String lastName = "X";
      String password = "10";
      
      driver.findElement(By.xpath("111")).sendKeys(firstName);
      driver.findElement(By.xpath("222")).sendKeys(lastName);
      driver.findElement(By.xpath("333")).sendKeys(password);

      driver.findElement(By.xpath("login button")).click();

        
      //Scenario 2
      firstName = "A";
      lastName = "";
      password = "10";
      
      driver.findElement(By.xpath("111")).sendKeys(firstName);
      driver.findElement(By.xpath("222")).sendKeys(lastName);
      driver.findElement(By.xpath("333")).sendKeys(password);

      driver.findElement(By.xpath("login button")).click();


      //Scenario 3
      firstName = "A";
      lastName = "X";
      password = "";
      
      driver.findElement(By.xpath("111")).sendKeys(firstName);
      driver.findElement(By.xpath("222")).sendKeys(lastName);
      driver.findElement(By.xpath("333")).sendKeys(password);

      driver.findElement(By.xpath("login button")).click();
}

Few observations from the above code:

1) Values are hard coded which is not advisable. Tightly coupled data.
2) Clearly lots of duplicate code. 
3) There can be more scenarios. What if first name, last name and password, all three are provided or all three are not provided. 

To handle new scenario's more code would need to be added which will further lead to complexity and more duplicate code. Inefficient isn't it?

Clearly the above code cannot be considered to be data driven since the data is tightly coupled. Any change in the test data will result in changing the test case.

There is a need to make this test case generic so that it could execute variety of test data. Clearly we need to take the data out of the test case.

To do that we need to follow data driven automation framework by using JUnit's Parameterized runner functionality.

Please provide comments so that this blog can futher be refined.

Thanks :)

2 comments: