Are you having difficulty in maintaining the huge sets of examination cases for your awarding? Is the exam data scattered across diverse test scripts? Do you accept to maintain separate examination scripts for every examination environment and then search beyond all scripts if one value changes in the examination data? Information technology is fourth dimension-taking and hell of an effort, isn't it? We all wish the examination cases are consistent and written in a uniform manner, following a fix of rules similar nosotros have traffic rules and everyone tries to follow the same when on road. This is where information driven framework comes into play.

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

  • What is an Automation framework?
    • Why practice we need an Automation testing framework?
    • What are the different types of automation frameworks in Selenium?
  • What is Data Driven Framework?
    • Also, what are the benefits of using the Information 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 prepare of guidelines or rules used for creating and designing test cases. The guidelines include coding standards, object repositories, test-data treatment methods, processes to shop test results, or any other information on how to admission external resource.

A tester can always write tests without a framework, it is not a mandatory step but using an organized framework provides additional benefits like increased code re-usage, higher portability, reduced script maintenance toll, and higher code readability. Information technology as well helps the team to write down the examination script in a standard format. Using an automation testing framework, efficient blueprint and development of automatic test scripts enable and it ensures reliable assay of problems or bugs for the system or application nether exam. The below section lists a few of the important benefits which justifies the need for an automation testing framework:

Why do nosotros need an Automation testing framework?

It is important to use a framework for automated testing equally information technology improves the automation testing team's efficiency and test development speed. Some of the benefits of using the automation framework are every bit 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 unlike types of automation frameworks in Selenium?

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

framework types data driven framework

  • Data Driven Test Framework.
  • Keyword Driven Test Framework.
  • Hybrid Test Framework.

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

  • Data Driven Testing Framework is used to separate the examination script from the test data. You can examination the same script with multiple sets of information. Nosotros volition discuss this framework in detail in the following topics.
  • Keyword Driven Testing Framework is an extension of the Data Driven framework. It allows storing a set of code called 'keywords' in a separate 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 Information-Driven and Keyword-Driven framework. Here, the keywords, as well every bit the test data, are external. We maintain Keywords in a split file and test data in excel file or CSV files or database. For details refer - Hybrid Framework.

Hither, in this article, Permit us accept a deep dive into the Data Driven Exam Framework.

What is Data Driven Framework?

Mostly, when we test an application manually, we run the same scenario for multiple exam data. Additionally, we keep the same test information in some files like Excel Files, Text Files, CSV Files, or any database. The aforementioned is true for automation also, where nosotros would like to run the aforementioned test scenario for multiple exam data. Let'south say y'all have written an automation script to fill the student registration form on the ToolsQA Demo site. There can be many students for registration, the just thing that is differentiating in the code is input values (Proper name, Address, Phone, Gender, etc..). Volition you write a separate script for every student to annals? Is in that location a way, we tin can reuse the lawmaking and simply change the student data?

Yep, this is where the Data Driven Framework comes into play and makes the test scripts work properly for different sets of test data. It saves fourth dimension to write additional code. It'south like write one time and run many times mechanism as you lot can run the aforementioned Selenium script multiple times.

In elementary words, we use the Data Driven Framework when nosotros have to execute the same script with multiple sets of test information, whose storage is at a unlike place and non present inside the test script. Whatever changes done to the data will not impact the lawmaking 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 tin reap when he/she develops the automation framework using the Data-Driven technique:

  • Test cases tin be modified without much changes to lawmaking.
  • Information technology allows testing the application with multiple sets of data values, specially during regression testing.
  • It helps u.s.a. to separate the logic of the examination cases/scripts from the test data.

One of the nigh commonly used, data sources for the test is Microsoft Excel Sheets. We tin maintain the data in excel sheets and use them in the test script. Let's encounter how we can create a Data Driven UI automation framework by reading the examination data from Excel files.

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

We have learned in the previous commodity "Read & Write Data from Excel in Selenium" how to read and write data in Excel files using Apache POI and and then pass the same data sets as examination data to the Selenium tests. But in that script, all the deportment of reading data from an Excel file, writing information to the Excel file, passing the data to the Selenium actions were happening in the chief method of the class. That format is acceptable if nosotros are only writing one or two test cases. However, when nosotros take to develop an automation framework that will have multiple test scenarios, then it should properly organize and should accept a defined folder bureaucracy.

A bones pollex rule for the information driven testing framework would be to segregate the test information from the exam scripts. Besides, 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 basic Data Driven framework, which will be used to automate the "Student Registration Form".

  • Create three New Packages in your Project for testCases, testData, and utilities.
  • Nether the testData package, put your Excel Canvass that has exam data. Using this, we carve up the test data from the testCases.
  • Under the utilities, create a New Class and proper name information technology "ExcelUtils". It volition contain all functions related to Excel used for reading and writing.
  • Under the utilities package, create another grade "Constants". It will contain the constant values across the framework like testdata file path, URL of the awarding, etc.
  • Under the testCases packet, we will create the examination files that incorporate the Selenium lawmaking for interacting with web elements. (For Example, RegisterStudentTest.java)

project Structure in datadrive framework

After performing the above steps, the folder structure volition look like:

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

  1. ExcelUtils Class - It is a utility class that will contain all the methods related to Excel Sheet read and write operations along with initializing the Workbook. You can then reuse the methods in dissimilar exam cases, by creating an object of Excel Utils Form. The lawmaking for this class volition be as beneath -
                      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            class            ExcelUtils            {            private            static            HSSFWorkbook workbook;            private            static            HSSFSheet sheet;            private            static            HSSFRow row;            private            static            HSSFCell cell;            public            void            setExcelFile            (Cord excelFilePath,String sheetName)            throws            IOException {            //Create an object of File class to open xls file            File            file            =            new            File(excelFilePath);            //Create an object of FileInputStream form to read excel file            FileInputStream            inputStream            =            new            FileInputStream(file);            //creating workbook instance that refers to .xls file            workbook=new            HSSFWorkbook(inputStream);            //creating a Sheet object            sheet=workbook.getSheet(sheetName);     }            public            Cord            getCellData            (int              rowNumber,int              cellNumber){            //getting the cell value from rowNumber and cell Number            jail cell =sheet.getRow(rowNumber).getCell(cellNumber);            //returning the cell value as string            return            cell.getStringCellValue();     }            public            int            getRowCountInSheet            (){            int            rowcount            =            sheet.getLastRowNum()-sheet.getFirstRowNum();            return            rowcount;     }            public            void            setCellValue            (int              rowNum,int              cellNum,String cellValue,String 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 above code contains different methods like setExcelFile to initialize the Excel Workbook, getCellValue to think the value nowadays in a detail cell in the file, setCellValue to set some value into a newly created cell. In a similar way, you tin can create dissimilar methods related to excel operations in this class.

  1. Constants Class- It is used to put abiding values in a file and 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 across diverse tests if at that place is any change in whatsoever of the values, yous will just have to update in 1 place. For example, if the file path is changed, then instead of updating all the test cases with the new value, you can just update it here in one file. The structure and values nowadays in this class are every bit beneath -
                      package            utilities;            public            class            Constants            {            public            static            final            String            URL            =            "https://demoqa.com/automation-practice-form";            public            static            final            String            Path_TestData            =            "Due east:\\Projects\\src\\testData\\";            public            static            concluding            String            File_TestData            =            "TestData.xls"; }                  
  1. RegisterStudentTest- Information technology is the test script for the student registration grade, which nosotros used to enter the start proper noun, last name, mobile, e-mail, gender, etc for a particular student. Since we have now separated the excel related methods in a split file, the code of our test case also changes.

We will create an object of ExcelUtils class in this test file and likewise use Constants to refer to the path of the file.

The updated code at present looks like -

                      parcel            testCases;            import            org.openqa.selenium.By;            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            java.util.concurrent.TimeUnit;            public            grade            RegisterStudentTest            {            //creating object of ExcelUtils form            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            primary            (String args[])            throws            IOException {            //set the Chrome Driver path            Arrangement.setProperty("webdriver.chrome.driver","Due east:\\Projects\\chromedriver.exe");            //Creating an object of ChromeDriver            WebDriver            driver            =            new            ChromeDriver();            //launching the specified URL            driver.become("https://demoqa.com/automation-practice-form");            //Place the WebElements for the student registration course            WebElement firstName=driver.findElement(Past.id("firstName"));         WebElement lastName=driver.findElement(By.id("lastName"));         WebElement email=commuter.findElement(By.id("userEmail"));         WebElement genderMale= driver.findElement(Past.id("gender-radio-i"));         WebElement mobile=driver.findElement(By.id("userNumber"));         WebElement accost=driver.findElement(By.id("currentAddress"));         WebElement submitBtn=driver.findElement(By.id("submit"));            //calling the ExcelUtils class method to initialise the workbook and sheet            excelUtils.setExcelFile(excelFilePath,"STUDENT_DATA");            //iterate over all the row to print the data present in each jail cell.            for(int            i=ane;i<=excelUtils.getRowCountInSheet();i++)         {            //Enter the values read from Excel in firstname,lastname,mobile,electronic mail,address            firstName.sendKeys(excelUtils.getCellData(i,0));         	lastName.sendKeys(excelUtils.getCellData(i,1));         	email.sendKeys(excelUtils.getCellData(i,2));         	mobile.sendKeys(excelUtils.getCellData(i,iii));         	address.sendKeys(excelUtils.getCellData(i,4));            //Click on the gender radio button using javascript            JavascriptExecutor            js            =            (JavascriptExecutor) commuter;         	js.executeScript("arguments[0].click();", genderMale);            //Click on submit button            submitBtn.click();            //Verify the confirmation message            WebElement            confirmationMessage            =            driver.findElement(By.xpath("//div[text()='Thanks for submitting the class']"));            //check if confirmation message is displayed            if            (confirmationMessage.isDisplayed()) {            // if the message is displayed , write Laissez passer in the excel canvass using the method of ExcelUtils            excelUtils.setCellValue(i,half dozen,"Pass",excelFilePath);             }            else            {            //if the bulletin is not displayed , write FAIL in the excel canvas using the method of ExcelUtils            excelUtils.setCellValue(i,6,"FAIL",excelFilePath);             }            //shut the confirmation popup            WebElement closebtn=driver.findElement(By.id("closeLargeModal"));             closebtn.click();            //wait for page to come up back to registration folio subsequently close button is clicked            driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);         }            //closing the commuter            driver.quit();     } }                  

The above course will perform the actions on the educatee registration folio. Withal, if you notice, methods of the ExcelUtils  handle all the excel related code.

So, this is one of the ways you can use the data driven framework in Selenium. Additionally, you can utilize the advantage of running the same test beyond multiple sets of data.

Cardinal Takeaways

  • Information-driven is a examination automation framework that stores examination data in a table or spread spreadsheet format.
  • Additionally, a Data-driven Testing framework helps to split up test data from Functional tests. Moreover, information technology allows testing applications with multiple sets of data values without rewriting the same lawmaking across different test scripts.
  • Also, nosotros perform information-driven Testing in Selenium using Apache POI. Information technology is a library that helps to read and write information in the Excel Sheet.