Showing posts with label Data Driven Framework. Show all posts
Showing posts with label Data Driven Framework. Show all posts

Data Driven Testing in Cucumber

What is Cucumber data driven testing?

In very simple terms, cucumber data driven testing means that you can pass data from cucumber feature files to your test cases. For example, let us consider that you are automating the login flow of some application. In this case, you can pass user name and password as test data from your feature file to selenium code.

Data-Driven Testing in Cucumber

1. Parameterization without Example Keyword:
Parameterization without Example Keyword
Using Regular Expression: 
("^user enters \"(.*)\" and \"(.*)\"$")
("^user enters \"([^\"]*)\" and \"([^\"]*)\"$")

2. Data-Driven Testing in Cucumber using Scenario Outline
Parameterization with Example Keyword

Parameterization with Examples Keyword
       Using Regular Expression: 
(“user enters (.*) and (.*)$")

Scenario Outline – This is used to run the same scenario for 2 or more different sets of test data. E.g. In our scenario, if you want to register another user you can data drive the same scenario twice.

Examples – All scenario outlines have to be followed with the Examples section. This contains the data that has to be passed on to the scenario.

Using Scenario Outline
Cucumber inherently supports data driven testing using Scenario Outline. Consider the following feature file using Scenario to define the test steps-

 Feature: Check addition in Google calculator
   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result

   Scenario: Addition
   Given I open google
   When I enter "2+2" in search textbox
   Then I should get result as "4"
In order to make it data driven we just have to use Scenario Outline along with Examples. Write the following code in feature file-

   Feature: Check addition in Google calculator
   In order to verify that google calculator work correctly
   As a user of google
   I should be able to get correct addition result

   Scenario Outline: Addition
   Given I open google
   When I enter "<calculation>" in search textbox
   Then I should get result as "<result>"
   
   Examples:
| calculation |result|
| 3+3      | 6 |
| 2+5      | 9 |
Please refer below Youtube video for detail explanation:


What is Data table in Cucumber?

Data Tables are handy for passing a list of values to a step definition. Cucumber provides a rich API for manipulating tables from within step definitions.
Data Tables are also used to handle large amount of data. Tables in Cucumber feature files are represented by using the pipeline “|” sign.
Please refer below Youtube video for detail explanation:



How to explain Selenium Automation Framework (DataDriven+POM) in Interview?

When we give any answer in the interview the one and only important thing we need to keep in mind is It should be brief and to the point so that interviewer should not lose interest from our answer.
 
So whenever we need to give answer about framework we can divide framework in terms of different components which we are using to build a structure which we called a framework and which we use in our company:

We can start like mentioned below.

1. Design - We are using Page Object with Page Factory framework with functional/structural implementation.

Make sure you know what functional/structural implementation in this framework.

2. Build Tool - We are using standardized maven project for build, execution & dependency management.

Make sure you know about a build tool like ant/maven

3. Language and IDE-  We are using JAVA/Ruby as our binding language and eclipse as IDE. We use java because it is known to most people when we started automation.

4. Action Driver - We also have library package to maintain common functions related to All action method/Selenium/waits/directory creations etc

5. BaseClass - We also have a base page class for common functions use by all pages – to load config.properties file and Web Driver initialization.

Make sure you know why we have Base Page class in page object

5. Test Class -  We have maintained a page class for every page in our application and a page test class to maintain test for that pages. E.g. Product listing page, Add to cart page, Payment page, Invoice generation page.

Make sure you know we maintain different page and all different annotations in page factory

Page Objects Package- com.mystore.pageobjects

Page Class – AddToCart.java

6. We have maintained separate package for page test e.g.

Test Package - com.mystore.testcases

Test Class – AddToCartTest.java

Note:  Maintaining different packages is always a good practice to follow.

7. Utility component - Extent Class, Log4j class, Listeners, Data Providers

Package Name: com.mystore.utility

8. Test Data - For handling data driven cases we are passing data using xlsx file/xls file /csv file.

>> Make sure you know about libraries like openCSV,JXL/APACHE POI/Java Properties class

9. Test Framework - For ordering tests we are using testng framework.

Make sure you know how to use different annotations and run testng.xml using maven

10. Logs - We are using log4j library to maintain logging of our project. We are using all kinds of logging statements like INFO, DEBUG, and ERROR etc. We have maintained a separate class for it in com.mystore.utility package

>> Make sure you know this library usage in java, we can use log4j by mentioning properties of this framework in a xml file or a properties file and putting that file on build path.

11. Reports - We are using Extent Report for reporting purpose. It is a third party report and it is easily available at maven central repo.

We are using maven postman plugin / JAVA API to send generated extent reports as an attachment to client Distribution list.

>> Make sure you know about this plugin of maven or Java API

12. Version Control - We check in our code into client repository using a version controlling tool git bash on windows system.

13. CI tool - We have integrated our project with CI tool i.e. Jenkins to run the build automatically.

Summary: - We should talk about all major components in our project like Logging, Emailing, Page Objects, Page Factory Annotations, TestNG, Exception Handling, Build tool, Version controlling tool, DataDriven usage etc.

Please refer this post as well -  https://www.automationtestinginsider.com/2020/02/selenium-data-driven-framework-with-pom.html

Data Driven Framework Part 2

Data driven example with excel sheet - Please refer below source codes to understand the implementation of Data Driven Testing/Framework using excel sheet.

In this example we are passing the username and password using excel sheet to test the login functionality.

Excel Sheet with user credentials:

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

setup() - to launch the browser and navigate to application
tearDown() - quit the browser
getExcelData() - to get the excel test data and store using 2-D Array.
and we have created the object of NewExcelLibrary to use the different methods to access the excel sheet. Complete excel library you can find here.
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 ="Credentials1")
 public Object[][] getExcelData() {
  //Totals rows count
  int rows=obj.getRowCount("Data");
  //Total Columns
  int column=obj.getColumnCount("Data");
  int actRows=rows-1;
  
  Object[][] data= new Object[actRows][column];
  
  for(int i=0;i<actRows;i++) {
   for(int j=0; j<column;j++) {
    data[i][j]=obj.getCellData("Data", j, i+2);
   }
  }
  return data;
 }
}

TestClass: 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 = "Credentials1")
 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 below YouTube video to understand better.

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: