Are you having difficulty in maintaining the huge sets of examination cases for your awarding? Is the test data scattered beyond diverse exam scripts? Practise yous have to maintain carve up exam scripts for every test environment and then search beyond all scripts if one value changes in the exam data? It is time-taking and hell of an endeavor, isn't it? We all wish the test cases are consistent and written in a uniform manner, following a gear up of rules like we have traffic rules and everyone tries to follow the same when on road. This is where data driven framework comes into play.

You can also make your exam cases adhere to a uniform design and style, frame guidelines for your peers working on the application, to write the scripts based on the framed rules. In test automation, we tin can achieve all this using the Information Driven Framework. Let's understand how we can create a Data Driven Testing Framework for automating a web application using Selenium WebDriver, by covering the details in the following topics:

  • What is an Automation framework?
    • Why do nosotros need an Automation testing framework?
    • What are the dissimilar types of automation frameworks in Selenium?
  • What is Data Driven Framework?
    • Also, what are the benefits of using the Data Driven Testing Framework?
  • How to create a Data Driven Framework in Selenium using Apache POI?

What is an Automation Framework?

An automation testing framework is a set of guidelines or rules used for creating and designing test cases. The guidelines include coding standards, object repositories, test-information handling methods, processes to store test results, or whatever other information on how to access external resources.

A tester can always write tests without a framework, it is not a mandatory step only using an organized framework provides additional benefits similar increased code re-usage, higher portability, reduced script maintenance toll, and college code readability. It likewise helps the team to write down the exam script in a standard format. Using an automation testing framework, efficient design and development of automated examination scripts enable and it ensures reliable analysis of issues or bugs for the organization or application under examination. The below section lists a few of the of import benefits which justifies the demand for an automation testing framework:

Why do we need an Automation testing framework?

It is of import to utilize a framework for automated testing as it improves the automation testing team'due south efficiency and test development speed. Some of the benefits of using the automation framework are as below:

  • Standard Format for all the tests
  • Improved test efficiency
  • Lower script maintenance costs
  • Maximum test coverage
  • Reusability of code
  • Efficient Test Data Direction

What are the dissimilar types of automation frameworks in Selenium?

When testing an application using Selenium WebDriver, there are three main types of frameworks that we can use to create automatic tests for any web application:

framework types data driven framework

  • Data Driven Examination Framework.
  • Keyword Driven Examination Framework.
  • Hybrid Examination Framework.

Each of these frameworks has its own architecture and different benefits and disadvantages. When building out a test plan, it's important to cull the framework that is right for you.

  • Data Driven Testing Framework is used to separate the test script from the test data. Y'all can test the same script with multiple sets of information. We will discuss this framework in detail in the following topics.
  • Keyword Driven Testing Framework is an extension of the Data Driven framework. Information technology allows storing a gear up of lawmaking called 'keywords' in a dissever code file, externally from the test script. We can reuse these keywords across multiple test scripts. For details refer - Keyword Driven Framework
  • Hybrid Driven Framework is a combination of both the Data-Driven and Keyword-Driven framework. Hither, the keywords, as well as the test data, are external. Nosotros maintain Keywords in a separate file and test data in excel file or CSV files or database. For details refer - Hybrid Framework.

Here, in this commodity, Let u.s. take a deep dive into the Data Driven Examination Framework.

What is Information Driven Framework?

More often than not, when nosotros test an application manually, nosotros run the same scenario for multiple test data. Additionally, we keep the same test data in some files like Excel Files, Text Files, CSV Files, or any database. The same is true for automation also, where nosotros would like to run the same test scenario for multiple test data. Let's say you have written an automation script to fill the student registration form on the ToolsQA Demo site. There can be many students for registration, the merely thing that is differentiating in the code is input values (Name, Address, Phone, Gender, etc..). Volition you write a split up script for every educatee to register? Is at that place a fashion, nosotros can reuse the code and only change the student data?

Yes, this is where the Data Driven Framework comes into play and makes the examination scripts piece of work properly for different sets of test information. Information technology saves time to write additional code. It's like write one time and run many times machinery as you tin run the same Selenium script multiple times.

In simple words, we use the Data Driven Framework when nosotros have to execute the same script with multiple sets of test data, whose storage is at a different identify and not present inside the test script. Whatsoever changes done to the data will non impact the code of the test.

data driven framework

What are the benefits of using the Information Driven Testing Framework?

Post-obit are a few of the major benefits which a QA can reap when he/she develops the automation framework using the Data-Driven technique:

  • Test cases can be modified without much changes to code.
  • It allows testing the application with multiple sets of information values, especially during regression testing.
  • Information technology helps us to separate the logic of the test cases/scripts from the exam data.

Ane of the most commonly used, data sources for the test is Microsoft Excel Sheets. Nosotros can maintain the data in excel sheets and use them in the test script. Allow's see how we tin create a Data Driven UI automation framework by reading the test data from Excel files.

How to create a Data Driven Framework in Selenium using Apache POI?

We have learned in the previous article "Read & Write Data from Excel in Selenium" how to read and write data in Excel files using Apache POI and so laissez passer the same information sets equally test data to the Selenium tests. But in that script, all the actions of reading information from an Excel file, writing data to the Excel file, passing the data to the Selenium actions were happening in the master method of the class. That format is acceptable if we are just writing one or ii test cases. However, when we have to develop an automation framework that will have multiple test scenarios, so it should properly organize and should take a divers folder hierarchy.

A basic thumb rule for the information driven testing framework would be to segregate the test data from the test scripts. Also, the actions to read/write the data from files should segregate and be available as utilities.

Follow the steps as mentioned below to create a bones Data Driven framework, which will be used to automate the "Educatee Registration Form".

  • Create three New Packages in your Project for testCases, testData, and utilities.
  • Nether the testData packet, put your Excel Sheet that has examination data. Using this, we split the exam data from the testCases.
  • Nether the utilities, create a New Grade and name it "ExcelUtils". Information technology volition incorporate all functions related to Excel used for reading and writing.
  • Under the utilities package, create another course "Constants". It will comprise the constant values beyond the framework like testdata file path, URL of the application, etc.
  • Under the testCases package, we will create the exam files that contain the Selenium code for interacting with web elements. (For Example, RegisterStudentTest.java)

project Structure in datadrive framework

After performing the above steps, the folder construction volition look similar:

Let's understand the details of each of these classes:

  1. ExcelUtils Class - Information technology is a utility class that will comprise all the methods related to Excel Sheet read and write operations along with initializing the Workbook. You lot can and so reuse the methods in different test cases, by creating an object of Excel Utils Class. The code for this class will exist as below -
                      packet            utilities;            import            org.apache.poi.hssf.usermodel.HSSFCell;            import            org.apache.poi.hssf.usermodel.HSSFRow;            import            org.apache.poi.hssf.usermodel.HSSFSheet;            import            org.apache.poi.hssf.usermodel.HSSFWorkbook;            import            java.io.File;            import            java.io.FileInputStream;            import            java.io.FileOutputStream;            import            java.io.IOException;            public            form            ExcelUtils            {            private            static            HSSFWorkbook workbook;            individual            static            HSSFSheet sheet;            private            static            HSSFRow row;            individual            static            HSSFCell cell;            public            void            setExcelFile            (String excelFilePath,String sheetName)            throws            IOException {            //Create an object of File course to open xls file            File            file            =            new            File(excelFilePath);            //Create an object of FileInputStream class to read excel file            FileInputStream            inputStream            =            new            FileInputStream(file);            //creating workbook case that refers to .xls file            workbook=new            HSSFWorkbook(inputStream);            //creating a Sheet object            canvass=workbook.getSheet(sheetName);     }            public            Cord            getCellData            (int              rowNumber,int              cellNumber){            //getting the cell value from rowNumber and prison cell Number            cell =sheet.getRow(rowNumber).getCell(cellNumber);            //returning the jail cell value as string            return            prison cell.getStringCellValue();     }            public            int            getRowCountInSheet            (){            int            rowcount            =            sheet.getLastRowNum()-canvass.getFirstRowNum();            return            rowcount;     }            public            void            setCellValue            (int              rowNum,int              cellNum,Cord cellValue,Cord excelFilePath)            throws            IOException {            //creating a new prison cell in row and setting value to it                        sheet.getRow(rowNum).createCell(cellNum).setCellValue(cellValue);            FileOutputStream            outputStream            =            new            FileOutputStream(excelFilePath);     	workbook.write(outputStream);     } }                  

The in a higher place code contains different methods like setExcelFile to initialize the Excel Workbook, getCellValue to recall the value present in a item cell in the file, setCellValue to set some value into a newly created cell. In a similar style, yous can create different methods related to excel operations in this course.

  1. Constants Class- It is used to put abiding values in a file so that the same can be reused across test cases. Ane more advantage of placing values in separate files is that since these values are common beyond various tests if there is any alter in whatsoever of the values, you will just have to update in i place. For case, if the file path is changed, then instead of updating all the test cases with the new value, you can merely update it hither in one file. The structure and values present in this class are equally below -
                      bundle            utilities;            public            class            Constants            {            public            static            final            String            URL            =            "https://demoqa.com/automation-practice-class";            public            static            final            Cord            Path_TestData            =            "Due east:\\Projects\\src\\testData\\";            public            static            terminal            String            File_TestData            =            "TestData.xls"; }                  
  1. RegisterStudentTest- It is the test script for the student registration class, which we used to enter the offset name, last name, mobile, e-mail, gender, etc for a particular student. Since we have at present separated the excel related methods in a carve up file, the code of our test example also changes.

Nosotros will create an object of ExcelUtils form in this exam file and besides employ Constants to refer to the path of the file.

The updated code now looks like -

                      packet            testCases;            import            org.openqa.selenium.Past;            import            org.openqa.selenium.JavascriptExecutor;            import            org.openqa.selenium.WebDriver;            import            org.openqa.selenium.WebElement;            import            org.openqa.selenium.chrome.ChromeDriver;            import            utilities.Constants;            import            utilities.ExcelUtils;            import            coffee.io.IOException;            import            coffee.util.concurrent.TimeUnit;            public            grade            RegisterStudentTest            {            //creating object of ExcelUtils class            static            ExcelUtils            excelUtils            =            new            ExcelUtils();            //using the Constants class values for excel file path                        static            String            excelFilePath            =Constants.Path_TestData+Constants.File_TestData;            public            static            void            main            (Cord args[])            throws            IOException {            //set the Chrome Driver path            Arrangement.setProperty("webdriver.chrome.driver","E:\\Projects\\chromedriver.exe");            //Creating an object of ChromeDriver            WebDriver            driver            =            new            ChromeDriver();            //launching the specified URL            driver.go("https://demoqa.com/automation-practice-form");            //Identify the WebElements for the student registration grade            WebElement firstName=commuter.findElement(By.id("firstName"));         WebElement lastName=driver.findElement(By.id("lastName"));         WebElement email=driver.findElement(By.id("userEmail"));         WebElement genderMale= driver.findElement(Past.id("gender-radio-i"));         WebElement mobile=driver.findElement(Past.id("userNumber"));         WebElement address=driver.findElement(By.id("currentAddress"));         WebElement submitBtn=driver.findElement(By.id("submit"));            //calling the ExcelUtils class method to initialise the workbook and canvass            excelUtils.setExcelFile(excelFilePath,"STUDENT_DATA");            //iterate over all the row to impress the data present in each cell.            for(int            i=1;i<=excelUtils.getRowCountInSheet();i++)         {            //Enter the values read from Excel in firstname,lastname,mobile,email,address            firstName.sendKeys(excelUtils.getCellData(i,0));         	lastName.sendKeys(excelUtils.getCellData(i,1));         	email.sendKeys(excelUtils.getCellData(i,two));         	mobile.sendKeys(excelUtils.getCellData(i,three));         	address.sendKeys(excelUtils.getCellData(i,4));            //Click on the gender radio button using javascript            JavascriptExecutor            js            =            (JavascriptExecutor) driver;         	js.executeScript("arguments[0].click();", genderMale);            //Click on submit button            submitBtn.click();            //Verify the confirmation message            WebElement            confirmationMessage            =            driver.findElement(By.xpath("//div[text()='Thank you for submitting the grade']"));            //check if confirmation bulletin is displayed            if            (confirmationMessage.isDisplayed()) {            // if the message is displayed , write Laissez passer in the excel sail using the method of ExcelUtils            excelUtils.setCellValue(i,six,"Laissez passer",excelFilePath);             }            else            {            //if the message is not displayed , write Neglect in the excel sheet using the method of ExcelUtils            excelUtils.setCellValue(i,6,"Neglect",excelFilePath);             }            //close the confirmation popup            WebElement closebtn=driver.findElement(By.id("closeLargeModal"));             closebtn.click();            //expect for page to come back to registration page after shut push is clicked            driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);         }            //closing the driver            driver.quit();     } }                  

The in a higher place class will perform the actions on the student registration page. However, if y'all observe, methods of the ExcelUtils  handle all the excel related code.

So, this is one of the means y'all tin utilise the data driven framework in Selenium. Additionally, you can employ the advantage of running the same test across multiple sets of information.

Primal Takeaways

  • Data-driven is a test automation framework that stores exam information in a tabular array or spread spreadsheet format.
  • Additionally, a Information-driven Testing framework helps to separate test information from Functional tests. Moreover, information technology allows testing applications with multiple sets of data values without rewriting the same lawmaking beyond unlike test scripts.
  • Besides, we perform data-driven Testing in Selenium using Apache POI. Information technology is a library that helps to read and write data in the Excel Sheet.