Quick Contact

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

    In this framework, input values are read from data files and are stored into a variable in test scripts. DDT (Data Driven testing) enables building both positive and negative test cases into a single test. In Data-driven test automation framework, input data can be stored in single or multiple data sources like xls, XML, csv, and databases.

    What is Automation Framework?

    A framework is considered to be a combination of set protocols, rules, standards and guidelines that can be incorporated or followed as a whole so as to leverage the benefits of the scaffolding provided by the Framework. An “Automation Framework” is a set of guidelines like coding standards, test- data handling which provide an execution environment for the automation test scripts. The framework provides the user with various benefits that help them to develop, execute and report the automation test scripts efficiently.

    In other words, a framework is a constructive blend of various guidelines, coding standards, concepts, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections to pillar automation testing. The framework enable a user to follow these guidelines while automating application to take advantages of various productive results such as ease of scripting, scalability, modularity, understandability, process definition, re-usability, cost, maintenance.

    Features of Automation Framework

    The various features of Automation Framework can be categorized as below:

    General features
    • Re-use of existing code by writing code once and invoking it elsewhere
    • Consistency of test automation implementation by defining standards
    • Tool independence by implementing the framework as separate software
    • Ease of framework installation and maintenance
    • Ease of upgrading the deployed framework
    • Supported operating systems and browsers
    Features related to test design
    • Simple to learn and use
    • Common operations already built-in the framework
    • Minimal changes to the test automation in case the application changes
    • Integration with version control systems and test management systems
    Features related to test execution
    • Ability of the framework to run on multiple systems
    • Support for test configuration e.g. application name, server name/ URL and test cases to be executed
    • Support for scheduled execution
    • Supported invocations of the framework e.g. by GUI or by command-line
    • Support for invoking and closing/ shutting down the application(s) under test
    • Ability to connect to myriad test data sources and collect test data
    • Error recovery e.g. by dismissing unknown windows, retrying a failed operation with/ without configurable delays, proceeding to the next step/ test case and even resetting or re- starting the application(s) under test
    • Integration with defect tracking systems
    • Clean up test data in the application(s) under test after completing each test
    Features related to test reporting
    • Logging states and results
    • Reporting test results (in one or multiple formats e.g. Excel or HTML) with metrics such as number of test cases in the test plan, test cases executed, test cases passed, test cases failed and the time taken to execute the test cases
    • Distributing test results e.g. by publishing to web or email
    Benefits of Using Automation Framework

    The benefits of automation framework are:

    • Ease of scripting: An automation framework in place ensures consistent coding and that best practices are followed. Standard scripting will result in team consistency during test library design and prevent individuals from following their own coding standards, thus avoiding duplicate coding. It helps to streamline testing projects that involve a group of testers working on multiple features of an application.
    • Scalable: Whether multiple web pages are being added or Objects or data, a good automation framework design is scalable. A framework should be much easier to extend to larger projects.
    • Modularity: Automation framework ensures that the complex task is broken down to smaller manageable tasks which can then be re-used whenever required.
    • Understand-ability: An automation framework enables quick transition (or understand) the overall architecture & bring people up-to-speed.
    • Re-usability: Common library files can be reused when required, no need to develop them every time. Modularity allows testers to re-use common modules in different scripts to avoid unnecessary & redundant tasks.
    • Cost & Maintenance: A well designed automation framework helps in maintaining the code in light of common changes like Test data, Page Objects, Reporting structure, etc.
    • Better error handling: A good automation framework helps us catch different recovery scenarios and handle them properly.
    • Minimal manual intervention:. Automation framework can take care of mundane & redundant manual tasks.
    • Easy Reporting: The reporting module within framework can handle all the report requirements. Any changes, if required, can be incorporated in the same module for easy & efficient reporting. A framework allows for user friendly interface and reporting options.
    • Segregation: A framework helps segregate the test script logic and the corresponding test data. The Test data can be stored into an external database like property files, xml files, excel files, text files, CSV files, ODBC repositories etc.
    • Test configuration: Test suites covering different application modules (or functionalities) can be configured easily using an automation framework.
    • Continuous integration: An automation framework helps in today’s time of continuous integration.
    Different Types of Automation Framework

    There is a divergent range of Automation Frameworks available. These frameworks differ from each other based on their support to different key factors to do automation like reusability, ease of maintenance. The few most popularly used Test Automation Frameworks are as below:

    • Module Based Testing Framework: In the modular testing framework, testers create test scripts on module wise by breaking down the complete application under test into smaller, independent tests. These individual test scripts can be combined to make larger test scripts by using a master script to achieve required scenarios. This master script is used to invoke the individual modules to run end to end test scenarios. In this framework, testers write function libraries to use it whenever required.
    • Linear Architecture Testing Framework: Linear Architecture Framework is a basic level test automation framework which is in the form of ‘Record and Playback’ in a linear fashion. This framework is also known as ‘Record and Playback’ framework. This type of framework is used to test small sized applications. In this type, creation, and execution of test script are done individually for each test case individually.
    • Data Driven Testing Framework: Data driven test automation framework is focused on separating the test scripts logic and the test data from each other. It allows to create test automation scripts by passing different sets of test data. The test data set is kept in the
      external files or resources such as MS Excel Sheets, MS Access Tables, SQL Database, XML files. The test scripts connect to the external resources to get the test data. This framework gives more test coverage with reusable tests and flexibility in execution of tests only when required and by changing only the input test data.
    • Keyword Driven Testing Framework: It is also known as table-driven testing or action word based testing. In Keyword-driven testing, a table format is used to define keywords or action words for each function or method. It performs automation test scripts based on the keywords specified in the excel sheet. By using this Framework, testers can work with keywords to develop any test automation script, testers with less programming knowledge would also be able to work on the test scripts. The logic to read keywords and call the required action mentioned in the external excel sheet is placed in the main class.
    • Hybrid Testing Framework: Hybrid Test automation framework is the combination of two or more frameworks mentioned above. It attempts to leverage the strengths and benefits of other frameworks for the particular test environment it manages.
    • Behavior Driven Development Framework: The purpose of this Behavior Driven Development framework is to create a platform which allows everyone (such as Business Analysts, Developers, Testers) to participate actively. It requires increased collaboration between Development and Test Teams.
    What is the Page Object Model?

    The Page Object Model is a design pattern of testing, derived from the Object Oriented Programming concepts. The POM describes the web application into the number of web pages being used, and contains the elements as properties, and actions as methods. This offers low maintenance on the tests developed.

    Creating the test automation framework

    The creation and designing a simple test automation framework for running tests on web applications, using Selenium webdriver, TestNG and Maven, using Java programming language is shown here. This framework should help to run the tests from an Eclipse IDE or an IntelliJ IDE.

    The various tools used are:

    • Apache Maven: It defines the project structure
    • Selenium Webdriver: It provides Test automation tool/library
    • Selenium Grid: It provides a feature to run tests on multiple test environments on a network
    • TestNG: It provodes test runner and report engine
    Test suite Structure

    The test suite structure is:

    • BasePage: BasePage is a class which contains the reusable methods that will be used by the various pages. For e.g., the BasePage class contains “getWebElement” method which will return a web-element when it pass the locator and the driver instances.
    • Pages: Pages are a set of classes, named based on the name of the webpage. For e.g., Login- Page, Home-Page, are so named because of the webpage they represent. The Login-Page will contain the public methods to perform login, and private attributes for username and password fields. The private attributes for username and password fields will help maintain encapsulation of the unwanted information for the tests.
    • BaseTest: BaseTest is a class which contains the reusable methods that will be used by the various tests. The BaseTest is expected to contain the “@BeforeTest”, “@AfterTest”,
      “@BeforeClass”, “@AfterClass” annotated methods to help manage the test sequence. The BaseTest will also contain the reference to the public web driver variable which will be instantiated and initialized in the @BeforeTest or @BeforeClass method when the test initiates and continue as a static variable till the @AfterTest or @AfterClass methods is called to end the test.
    • Tests: Tests are a set of classes, named based on the test to be conducted for each webpage. For e.g., LoginTest, is so named because of the test method that would be used to test the Login page/action. The LoginTest will contain the public methods annotated with “@Test” to test the login functionality. The LoginTest will instantiate the LoginPage class, using the PageFactory.initElements method, and then will invoke the LoginPage log in method with data parameters for username and password which will set the values in the username and password fields in the web application, and click on the submit button. Validations need to be inserted post the submit button click so that the login can be validated. Boolean values are returned from the Login-Page, and assertions are performed at the test level for validations.
    • Utilities: Utilities contain the various reusable classes and methods for overall run of the test suite. An example of a utility class could be a TestListener implementation to capture screenshots in case of failures.
    Project Structure of the Test Automation Framework

    The project structure in Eclipse or IntelliJ could be as follows:

    Steps to create the framework in Eclipse
    1. Create a new Maven Project
    2. Provide a suitable package name for the project :: com… is a good naming convention it can follow

      The folder structure would look like this: src/main/java, src/main/resources, src/test/java and src/test/resources

    3. All the code would code into two parts.

      Test Objects and Data Providers would go into src/test/java :: Meaning all testng related code should go into src/test/java

      Page Objects should go into /src/main/java

      Named the package when creating the maven package structure

      Within the src/main/java and src/test/java see the defined package name and a sample java file will be found. Remove these files.

    4. The project structure should look something like below.

      Under src/main/java it can have com….pages

      Under src/test/java it can have com….tests and com….datastore

      It can have sub packages within these main packages to identify with modules

    5. Based on the choice of browsers you can use the TestNG structure to set up the base test.
    6. TestNG setup: Give the browser name, version and OS platform in the TestNG file to distinguish between classes, tests or suite. Since here tests are used for differentiation of browsers, so use the following structure for testng.xml
    7. PageObjects

      For the page objects, you can have differences in how you identify and interact with elements. This is a sample code which is given here

      public class LoginPage {
              By txtUserName = By.id(“uid”);
              By txtPassword = By.id(“password”); 
              By pageTitle =By.xpath(“html_title”); 
              By btnLogin = By.id(“btnLogin”);
    8. TestObjects

      For the tests, it would be using @Test annotations to define the tests.

      @Test
               public void TestClass1() {
               // Test Code here
           }

      For the base test, it would need a @BeforeTest method which defines the browser and driver to be used. This set up will be based on the parameters passed, or any initial configuration which have set up. From the code above, in the TestNG.xml, it has parameters defined for every test. Generally these have to be very similar. The parameter in the test2 is superflous (not recommended)

      @BeforeTest
           public void beforeTest(String parameter1) {
           // Here you define how you can set the driver 
           WebDriver driver = null;
           If (parameter == “chrome”) 
           driver = new ChromeDriver();
       
      
           else if (parameter == “firefox”) 
           driver = new FirefoxDriver();
      }
      
    What is Data Driven Framework?

    While automating or testing any application, at times it may be required to test the same functionality multiple times with the different set of input data. Thus, in such cases, the test data
    can’t be let embedded in the test script and need to be retained in data into some external database outside the test scripts. Data Driven Testing Framework helps a user to segregate the test script logic and the test data from each other. It lets the user store the test data into an external database. The external databases can be property files, xml files, excel files, text files, CSV files, ODBC repositories. The data is conventionally stored in “Key-Value” pairs. The key can be used to access and populate the data within the test scripts.

    The Data driven testing framework can be represented as below:

    Steps to create a Data driven Framework are:

    Example: Consider to Test Login functionality of an application

    1. Identify the Test Cases as below

      Input correct username and password – Login Success

      Input incorrect username and correct password – Login Failure

      Input correct username and incorrect password – Login Failure

    2. Create detailed test Steps for above 3 Test Cases
    3. Create Test Script
    4. Create an excel/csv with the Input Test Data
    5. 5. Modify the Scrip to Loop over Input Test Data. The input commands should also be parameterized

    This framework purely depends on data and data source can be anything like Excel file, CSV File, database.

    In data driven framework script will be separated from Data part, it means so if any changes happen we do not have to modify all the test cases.

    Example

    To create 50 Gmail accounts so two approaches are there to do this

    First- Create 50 scripts and run them.

    Second- Keep all the data separate in the file and changes the data only that is required for script and script will be only one. In future, any changes in the application then it has to modify one script only not the scripts.

    In simple words when it has to execute the same script with multiple sets of data then adopt data driven framework

    In this post, taking data from 2D Array and passing the data into script.

    Open Facebook and type username and password and login

    This test case should run 2 times with different set of data(data we have provided in the 2D array)

    Let’s implement the same

    <span style="font-size: 14pt; font-family: arial, helvetica, sans-serif;">package DataDrivenTesting;
    
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.testng.annotations.DataProvider; 
    import org.testng.annotations.Test;
    
    public class TestDDT {
    
    
    // this will take data from dataprovider which we created 
    @Test(dataProvider="testdata")
    public void TestFireFox(String uname,String password){
     
    
    // Open browsre
    WebDriver driver=new FirefoxDriver();
    
    // Maximize browser 
    driver.manage().window().maximize();
    
    // Load application 
    driver.get("http://www.facebook.com");
    
    // clear email field
    
    
    driver.findElement(By.id("email")).clear();
    
    
    // Enter usename 
    driver.findElement(By.id("email")).sendKeys(uname);
    
    // Clear password field 
    driver.findElement(By.id("pass")).clear();
    
    // Enter password 
    driver.findElement(By.id("pass")).sendKeys(password);
    }
    
    
    // this is DataProvider which actually feed data to our test cases here I have taken 2 D //array with 2 rows and 2 column it means. It will run our test case two times because we //have taken 2 rows.
    While first iteration this will pass username and password to test
    //case and in second iteration perform the same for second rows 
    
    @DataProvider(name="testdata")
    public Object[][] TestDataFeed()
    
    // Create object array with 2 rows and 2 column- first parameter is row and second is //column
     
    
    Object [][] facebookdata=new Object[2][2];
    
    // Enter data to row 0 column 0 
    facebookdata[0][0]="Selenium1@gmail.com";
    
    // Enter data to row 0 column 1 
    facebookdata[0][1]="Password1";
    
    // Enter data to row 1 column 0 
    facebookdata[1][0]="Selenium2@gmail.com";
    
    // Enter data to row 1 column 0 
    facebookdata[1][1]="Password2";
    
    // return arrayobject to testscript return facebookdata;
    }
    
    }</span>
    
    Advantages:
    • The most important feature of this framework is that it considerably reduces the total number of scripts required to cover all the possible combinations of test scenarios. Thus lesser amount of code is required to test a complete set of scenarios.
    • Any change in the test data matrix would not hamper the test script code.
    • Increases flexibility and maintainability
    • A single test scenario can be executed altering the test data values.
    Disadvantages:
    • The process is complex and requires an extra effort to come up with the test data sources and reading mechanisms.
    • Requires proficiency in a programming language that is being used to develop test scripts.
    What is Modular Driven Framework?

    Module based Testing Framework is based on one of the popularly known OOPs concept,
    Abstraction. The framework divides the entire “Application under Test” into a number of logical and isolated modules. For each module, a separate and independent test script is created. The modules are separated by an abstraction layer in such a way that the changes made in the sections of the application doesn’t yield effects on this module. The representation of framework is as below:

    Types of Modular driven Framework
    • Test Script Modularity Framework: Enables creation of Small, Independent Scripts representing Modules& Functions of the Application under Test (AUT).
    • Test Library Architecture Framework: Enables creation of Library Files representing Modules & Functions of the Application under Test (AUT)
    How to do Modular driven Framework

    Look for repeated functionality in the application for example the ‘login’ functionality. It can simple wrap this functionality in a method and can give it a sensible name.

    1. Create a ‘New Package‘ file and name it as ‘appModule’, by right click on the Project and
      select New > Package. We will be creating different packages for Page Objects, Utilities, Test Data, Test Cases and Modular actions. It is always recommended to use this structure, as it is easy to understand, easy to use and easy to maintain.
    2. Create ‘New Class‘ and name it as SignIn_Action by right click on package ‘appModule‘ and select New > Class. It will add new class ‘SignIn_Action’ under package ‘appModule’.
    3. Now create a Public Static Void Method and name it as Execute and club the following steps in to it:

      Click on the My Account link.

      Enter Username

      Enter Password

      Click on the Submit button

      This method will not have any Argument (driver) and Return value as it is a void method.

      import org.openqa.selenium.WebDriver; 
              	import framework.pageObject.Home_Page; 
              	import framework.pageObject.LogIn_Page;
                  public class SignIn_Action{
                  public static void Execute(WebDriver driver){ 
                  Home_Page.lnk_SignIn(driver).click(); 
                  LogIn_Page.txtbx_UserName(driver).sendKeys("testuser_1");
                  LogIn_Page.txtbx_Password(driver).sendKeys("Test@123"); LogIn_Page.btn_LogIn(driver).click();
           }
         }
    4. Create a New Class and name it as Module_TC by right click on the ‘automationFramework‘ Package and select New > Class. It will be creating all the test cases under this package.

      Now convert the old POM_TC in to the new Module based test case.

      package automationFramework;
              import java.util.concurrent.TimeUnit; 
              import org.openqa.selenium.WebDriver;
              import org.openqa.selenium.firefox.FirefoxDriver;
              // Import package appModule.* 
              import appModules.SignIn_Action;
       
      
              import pageObjects.Home_Page; 
              public class Module_TC {
              private static WebDriver driver = null; 
              public static void main(String[] args) {
              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
              driver.get("http://www.store.demoqa.com");
              // Use your Module SignIn now SignIn_Action.Execute(driver);
              System.out.println("Login Successfully, now it is the time to Log Off buddy.");
      
              Home_Page.lnk_LogOut(driver).click(); 
              driver.quit();
      }
      }
      

      Call to SignIn_Action will automatically execute all the steps mentioned under it. Project explorer window will look like this now.

    Advantages:
    • The framework introduces the high level of modularization which leads to easier and cost- efficient maintenance.
    • The framework is pretty much scalable
    • If the changes are implemented in one part of the application, only the test script representing that part of the application needs to be fixed to leave all the other parts untouched.
    Disadvantages:
    • While implementing test scripts for each module separately, the test data is embed (Data which supposed to perform testing) into the test scripts. Thus, whenever a test is being supposed with a different set of test data, it requires the manipulations to be made in the test scripts.
    What is Keyword Driven Framework?

    The Keyword driven testing framework is an extension to Data driven Testing Framework in a sense that it not only segregates the test data from the scripts, it also keeps the certain set of code belonging to the test script into an external data file.

    These set of code are known as Keywords and hence the framework is so named. Keywords are self- guiding as to what actions need to be performed on the application.

    The keywords and the test data are stored in a tabular like structure and thus it is also popularly regarded as Table driven Framework. Take a notice that keywords and test data are entities independent of the automation tool being used.

    In the above example, keywords like login, clicking and verify Link are defined within the code.

    Depending upon the nature of application keywords can be derived. And all the keywords can be reused multiple times in a single test case. Locator column contains the locator value that is used to identify the web elements on the screen or the test data that needs to be supplied.

    All the required keywords are designed and placed in the base code of the framework.

    Advantages
    • In addition to advantages provided by Data Driven testing, the Keyword driven
      framework doesn’t require the user to possess scripting knowledge, unlike Data Driven Testing.
    • A single keyword can be used across multiple test scripts.
    Disdvantages
    • The user should be well versed with the Keyword creation mechanism to be able to efficiently leverage the benefits provided by the framework.
    • The framework becomes complicated gradually as it grows and a number of new keywords are introduced.
    What is Hybrid Driven Framework?

    As the name suggests, the Hybrid Testing Framework is a combination of more than one above mentioned frameworks. The best thing about such a setup is that it leverages the benefits of all kinds of associated frameworks.

    In the above example, keyword column contains all the required keywords used in the particular test case and data column drives all the data required in the test scenario. If any step does not need any input then it can be left empty.

    Test Data Files
    Why Data Driven Testing?

    There are number of data sets that have to be used to test a feature of an application. Running the same test with different data manually is time-consuming, error prone and a boring task.

    Let us understand this scenario with an example.

    Suppose there is a need to test the login/Register/ any form with multiple input fields with 100 different data sets. To test this there are three different approaches:

    • Create 100 scripts one for each dataset and execute each test one by one.
    • Change the data in the script and execute it multiple times.
    • Import the data from the excel sheet and execute the script multiple times with different data.

    First two scenarios are laborious, time-consuming implying low ROI. Hence, we must follow the third approach. In the third approach, are implementing the Data Driven framework, where all our data resides in an excel sheet, where it is imported from and used to test the features of the application.

    How to create a Data Driven Automation Framework

    The steps to test Login functionality of an application are below

    1. Identify the Test Cases as below
      • Input Correct username and password – Login Success
      • Input incorrect username and correct password – Login Failure
      • Input correct username and incorrect password – Login Failure
    2. Create detailed test Steps for above 3 Test Cases
      Description Test Steps Test Data Expected Results
      Check Login for valid credentials
      1. Launch the application
      2. Enter Username password
      3. Click Okay
      4. Check Results
      Username: valid

      password: valid

      Login Success
      Check Login for invalid credentials
      1. Launch the application
      2. Enter Username Password
      3. Click Okay
      4. Check Results
      Username: invalid

      password: valid

      password: valid
      Check Login for invalid credentials
      1. Launch the application
      2. Enter Username password
      3. Click Okay
      4. Check Results
      Username: valid

      password: invalid

      Login Fail
    3. Create Test Script

      If you observe the Test Steps remain common through the 3 Test Steps. You need to create a Test Script to execute these steps

      // This is Pseudo Code
                 // Test Step 1: Launch Application 
                 driver.get("URL of the Appliation");
                 // Test Step 2: Enter Password 
                 txtbox_username.sendKeys("valid");
                // Test Step 3: Enter Password 
                txtbox_password.sendKeys("invalid");
                // Test Step 4: Check Results
                If (Next Screen) print success else Fail
      
    4. Create an excel/csv with the Input Test Data
    5. Step Modify the Scrip to Loop over Input Test Data. The input commands should also be parameterized
      / This is Pseudo Code
      // Loop 3 Times
      for (i = 0; i & lt; = 3; i++) {
      // Read data from Excel and store into variables 
      int input_1 = ReadExcel(i, 0);
      int input_2 = ReadExcel(i, 1);
      
      // Test Step 1: Launch Application 
      driver.get("URL of the Application");
      
      // Test Step 2: Enter Password 
      txtbox_username.sendKeys(input_1);
      // Test Step 3: Enter Password
      
      txtbox_password.sendKeys(input_2);
      // Test Step 4: Check Results 
      If(Next Screen) print success
      
      else Fail
      }
      

    Above are just 3 test cases. The test script can be used to loop over following test cases just by appending test data values to Excel

    Input incorrect username and incorrect password – Login Fail

    Input correct username and password blank – Login Fail

    Input blank username and blank password– Login Fail

    Best practices of Data Driven testing:

    Below given are Best testing practices for Data-Driven testing:

    • It is ideal to use realistic information during the data-driven testing process
    • Test flow navigation should be coded inside the test script
    • Drive virtual APIs with meaningful data
    • Use Data to Drive Dynamic Assertions
    • Test positive as well as negative outcomes
    • Repurpose Data Driven Functional Tests for Security and Performance
    Advantages of Data-Driven testing
    • Allows to test application with multiple sets of data values during Regression testing
    • Test data and verification data can be organized in just one file, and it is separate from the test case logic.
    • Base on the tool, it is possible to have the test scripts in a single repository. This makes the texts easy to understand, maintain and manage.
    • Actions and Functions can be reused in different tests.
    • Some tools generate test data automatically. This is useful when large volumes of random test data are necessary, which helps to save the time.
    • Data-driven testing can perform any phase of the development. A data-driven test cares are generally merged in the single process. However, it can be used in multiple test cases.
    • Allows developers and testers to have clear separation for the logic of their test cases/scripts from the test data.
    • The same test cases can be executed several times which helps to reduce test case and scripts.
    • Any changes in the test script do not affect the test data
    Disadvantages of Data Driven testing
    • Quality of the test is depended on the automation skills of the Implementing team
    • Data validation is a time-consuming task when testing large amount of data.
    • Maintenance is a big issue as large amount of coding needed for Data-Driven testing.
    • High-level technical skills are required. A tester may have to learn an entirely new scripting language.
    • There will be more documentation. Mostly related to scripts management tests infrastructure and testing results.
    • A text editor like Notepad is required to create and maintain data files.
    TestCore Class
    What is Apache POI

    Apache POI is an open source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It is a popular API that allows working around excel files using Java Programs. In short, you can read and write MS Excel files using Java. Apache POI is your Java Excel solution. You use HSSF if you needed to read or write an Excel file using Java (XLS) and you use XSSF if you need to read or write an OOXML Excel file using Java (XLSX). It has many predefined methods, classes, and interfaces.

    Loads xls file

    The excel files are considered to be the data source/data providers for test script execution. These files store the test data into key-value pairs. Each test script can has its own test data file. The name of the test script and the corresponding test data files/ excel sheet has been kept same for the traceability perspective.

    Each of the columns represents a key and each of the rows represents a test data/value. Mark that the test data formats are solely user defined. Thus based on the requirements, the test data files can be customize.

    Follow the below steps to R]read excel files using Apache POI in Selenium WebDriver:

      1. Download Apache POI jar file

        https://poi.apache.org/download.html

      2. Add download jar files
      3. Select Project and Right click on the Project – Go to ‘Build path’ – Go to ‘Configure build path’ – Click on ‘lib’ section – Add external jar

    Precondition

    1. Create an xlsx file and save it at particular location. Enter some data to read using Selenium. Close the created excel file before executing the script. (Here excel file is used as ‘Test.xlsx’ in the D Driver and the sheet name is used as TestData.)
    2. Go to option “Format Cells” and under Number Tab option, select Text and click on OK. By default it will be general, you need to make it as Number. If it don’t make it as text, there is a chance of NullPointerException error.

    Below mentioned script shows to read excel files in Selenium using Apache POI:

    import java.io.FileInputStream; 
    	import java.io.FileNotFoundException; 
    	import java.io.FileOutputStream; 
    	import java.io.IOException;
        import org.apache.poi.ss.usermodel.Cell; 
        import org.apache.poi.ss.usermodel.Row;
        import org.apache.poi.xssf.usermodel.XSSFCell; 
        import org.apache.poi.xssf.usermodel.XSSFRow; 
        import org.apache.poi.xssf.usermodel.XSSFSheet; 
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
        //to read excel files using Apache POI 
        public class ReadExcel {
        public static void main (String [] args) throws IOException{ 
        FileInputStream fis = new FileInputStream("D:\\Test.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis); 
        XSSFSheet sheet = workbook.getSheetAt(0);
        //Cell A1 = row 0 and column 0. It reads first row as 0 and Column A as 0.
        Row row = sheet.getRow(0); 
        Cell cell = row.getCell(0);
        System.out.println(cell); 
        System.out.println(sheet.getRow(0).getCell(0));
    }
    }
    
    Output
    SoftwareTestingMaterial.com
    Run Selenium Server Through code

    When test automating a web application using Selenium, it have to start the Selenium server first, so that a new Selenium session is created to talk to the web browser. This can be either done manually,
    i.e. user running a command line to start the Selenium server, or to get the pure automation effect of Selenium, it is best to start the Selenium server via a program code. The code below is written in

    Java and starts the Selenium server when called. Normally this would be the first action within your main() function.

    import org.openqa.selenium.server.RemoteControlConfiguration; 
    	import org.openqa.selenium.server.SeleniumServer;
        import com.thoughtworks.selenium.Selenium; 
        import java.net.BindException;
        import java.net.ServerSocket;
    
        public class Server {
    
        public static SeleniumServer server;
        public static void startSeleniumServer() throws Exception {
    
    try {
    ServerSocket serverSocket = new 
    ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
    serverSocket.close();
    //Server not up, start it
    try {
    RemoteControlConfiguration rcc = new 
    RemoteControlConfiguration();
    rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
    server = new SeleniumServer(false, rcc);
    
    } catch (Exception e) {
    System.err.println("Could not create Selenium Server 
    because of: " +	+
    e.getMessage()); 
    e.printStackTrace();
    }
    try {
    server.start();
    
    System.out.println("Server started");
    } catch (Exception e) {
    System.err.println("Could not start Selenium Server because of: "
    + e.getMessage()); 
    e.printStackTrace();
    }
    } catch (BindException e) {
    System.out.println("Selenium server already up, will reuse...");
    
    }
    }
    
    public static void stopSeleniumServer(Selenium selenium){ 
    	selenium.stop();
        if (server != null)
    {
     
    try
    {
    
    }
     
    selenium.shutDownSeleniumServer(); 
    server.stop();
    
    server = null;
     
    catch (Exception e)
    {
    e.printStackTrace();
    
    }
    }
    }
    }
    

    Another very simple and easy way to start selenium server inside the program, and if it is not bothered about setting specific port, just call these few lines in your program (before starting selenium session):

    SeleniumServer _server = new SeleniumServer();
    _server.boot();
    _server.start();
    _server.stop(); and once it has finished with the tests.
    
    
    Object.properties File to Store Xpaths

    According to name, a Properties file is used to define properties of keys which would have been used in our Selenium project. A Property file contains data in Key=Value format. It helps external users to communicate with the code and generate output on the basis of inputs. The properties file in Selenium Webdriver requires a custom Property class to provide a method to read locators. Or it can also achieve it by writing a Static method to access properties file and get locators.

    Features of a Property File
    • Can be used to access user input
    • Can be used to store XPath values
    • Changes can be made easily without accessing class files
    • A user can create as many as properties file as per requirement of Project.
    • Easy to maintain test data or user input
    • Used in the framework to keep the similar types of variable in separate files.
    • It stores data in the Key=Value format without any special characters like “” or ;
    • By default property file returns key values in String format.
    • Can store values of any data type in the properties file but we have to parse the values in required data type.
    • We can also set multiple values for a single key using any separator
    • Comments can be included in the file using double hash(#) symbols.
    Creating Properties File

    The Steps to create properties file are:

    1. Create Project
    2. Now right-click on a folder, Select New -> File
    3. Give File name as “testdata.properties”
    4. Click Finish
    5. Testdata.properties file would be added to your selected folder.
    6. Now open the properties file to add key=values in following format:

      testURL=http://www.allinoneblogs.com/

      searchXpath=//*[@id=”search-3″]/form/label/input

    Accessing Properties file in Class files
    1. Locating properties file in project:
      File file=new File(filePath);
    2. Convert file in byte form using FileInputStream
      FileInputStream fis=newFileInputStream(file);
    3. Create object of Properties Class
      Properties prop=new Properties();
    4. Load data from input stream to properties object using load()>
      prop.load(fis);
    5. Accessing values from Property file to a variable in Class file
      String testData=prop.getProperty(“testURL”); 
      		driver.get(testData);

    Example:

    Properties File: (objectRepository.properties) 
    	testURL=http://www.allinoneblogs.com/ 
    	searchXpath=//*[@id="search-3"]/form/label/input
     
        Sample.java
        import java.io.File;
        import java.io.FileInputStream; 
        import java.io.IOException; 
        import java.util.Properties; 
        public class Sample {
        public static void main(String[] args) throws IOException
       {
        // Step #1: Locate Properties File 
        File file=new
        File("configuration//objectRepository.properties");
    
        // Step #2: Convert data into bytes 
        FileInputStream fis=new FileInputStream(file);
    
        // Step #3: Creating object of Properties Class 
        Properties prop=new Properties();
    
        // Step #4: Load data from inputstream to Properties
        //object prop.load(fis);
    
        // Step #5: Store values from Properties file to local
        //variable.
        String URL=prop.getProperty("testURL"); 
        String Xpath=prop.getProperty("searchXpath");
    
        System.out.println("URL: "+URL); 
        System.out.println("Xapth: "+Xpath);
    }
    }
    
    Configuration Files

    Create a configuration properties file to store parameters such as log file path, test data path, results path, selenium execution speed, wait period, etc, then make changes to the parameters whenever needed. Java provides a way to create configuration file for storing configurable parameters of an application. Selenium Test Suite also needs such a file to keep global configuration settings.

    Example to create and to access values from the properties file.

    • Create a file text file (using any text editor such as notepad) Selconfig.properties (as in the file – view file) and save the file as “Selfconfig.properties”
    • Create classes as in the following code snippets:
    package com.selftechy.readdata; 
    	import com.selftechy.library.*; 
    	import java.io.IOException; 
    	public class TestRun {
        public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub 
        config cfg = new config("F:\\Helios-
        Workspace\\Config\\SelConfig.properties"); 
        System.out.println(cfg.ReadProperty("ConfigPath"));
    }
    }
    
        import java.io.*; 
        import java.util.*; 
        public class config {
        String str, key;
        private String filepath;
    
        public config(String filepath){ 
        this.filepath=filepath;
    }
    
    public String ReadProperty(String propkey){
     
    
    String propval=""; 
    try{
    int check = 0; 
    while(check == 0){
    check = 1;
    File cfgfile = new File(filepath); 
    if(cfgfile.exists()){
    Properties props = new Properties();
    
    FileInputStream propin = new FileInputStream(cfgfile);
    props.load(propin); propval=props.getProperty(propkey);
    }
    else{
    check = 0;
    }
    }
    }
    catch(IOException e){ e.printStackTrace();
    }
    return propval;
    }
    }
    

    Save both the classes and then execute the first one (TestRun.java) as Java application. In the Selconfig.properties file, TestData & ConfigPath are the keys and “F:\\Helios-Workspace\\TestData & F:\\Helios-Workspace\\Config” are the respective values. In .properties file comments can put using “#”. Any sentence that starts with # will be considered as comment.

    To access the values of any properties, use the “key” parameter. For example, to access the value of “TestData” and “ConfigPath” write the code as follows:

    config cfg = new config("F:\\Helios- 
    	Workspace\\Config\\SelConfig.properties");
    	cfg.ReadProperty("ConfigPath")
        cfg.ReadProperty("TestData")
    
    Emailing Test Results

    There might be situations, where it might be tasked by the management people or the clients to send email after every test execution.

    The program which is need include in Selenium Framework is,

    //The jar files which has used here are activation.jar and mail.jar Can be downloaded from Internet.

    import javax.activation.DataHandler; 
    	import javax.activation.DataSource; 
    	import javax.activation.FileDataSource; 
    	import javax.mail.*;
        import javax.mail.internet.*; 
        import java.util.*;
        public class SendMail
    {
        //reportFileName = TestExecutionResultFileName
        public static void execute(String reportFileName) throws Exception
    {
        String path=<Report file path>;
        String[] to={"<stakeholder1>","<stakeholder2>"}; 
        String[] cc={};
        String[] bcc={"<AutomationTester>"};
        SendMail.sendMail("<AutomationTesterUserName>", "<AutomationTesterPassword>",  
        "smtp.gmail.com",
        "465",
        "true",
        "true", 
        true,
        "javax.net.ssl.SSLSocketFactory", 
        "false",
        to,
        cc, 
        bcc,
        "<Subject line>", 
        "<Contents if
        any>", path, 
        reportFileName);
      }
        public static boolean sendMail(String userName, 
        String passWord,
        String host, 
        String port, 
        String starttls, 
        String auth, 
        boolean debug,
        String socketFactoryClass, 
        String fallback,
        String[] to, 
        String[] cc, 
        String[] bcc, 
        String subject, 
        String text,
        String attachmentPath, 
        String attachmentName){
        //Object Instantiation of a properties file. 
        Properties props = new Properties();
        props.put("mail.smtp.user", userName); 
        props.put("mail.smtp.host", host);
        if(!"".equals(port)){ 
        props.put("mail.smtp.port", port);
    }
        if(!"".equals(starttls)){ 
        props.put("mail.smtp.starttls.enable",starttls); 
        props.put("mail.smtp.auth", auth);
    }
        if(debug){ 
        props.put("mail.smtp.debug", "true");
        }else{
        props.put("mail.smtp.debug", "false");
    }
        if(!"".equals(port)){ 
        props.put("mail.smtp.socketFactory.port", port);
    }
        if(!"".equals(socketFactoryClass)){ 
        props.put("mail.smtp.socketFactory.class",
        socketFactoryClass);
    }
        if(!"".equals(fallback)){ 
        props.put("mail.smtp.socketFactory.fallback", fallback);
    }
        try{
        Session session = Session.getDefaultInstance(props, null);
     
        session.setDebug(debug);
        MimeMessage msg = new MimeMessage(session); 
        msg.setText(text);
        msg.setSubject(subject);
        Multipart multipart = new MimeMultipart(); 
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachmentPath); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(attachmentName); 
        multipart.addBodyPart(messageBodyPart);
        msg.setContent(multipart);
        msg.setFrom(new InternetAddress(userName));
        for(int i=0;i<to.length;i++){ 
        msg.addRecipient(Message.RecipientType.TO, 
        new InternetAddress(to[i]));
    }
       for(int i=0;i<cc.length;i++){ 
       msg.addRecipient(Message.RecipientType.CC, 
       new InternetAddress(cc[i]));
    }
       for(int i=0;i<bcc.length;i++){ 
       msg.addRecipient(Message.RecipientType.BCC, 
       new InternetAddress(bcc[i]));
    }
       msg.saveChanges();
       Transport transport = session.getTransport("smtp"); 
       transport.connect(host, userName, passWord); 
       transport.sendMessage(msg, msg.getAllRecipients()); 
       transport.close();
       return true;
    }  catch (Exception mex){ 
       mex.printStackTrace(); 
       return false;
    }
    }
    }
    

    The above Source code will do the job of triggering email after every execution.

    Add the below snippet at the end of the test execution report creation.

    [sourcecode language=”java”] SendMail.execute(ExecutionFileName);[/sourcecode]

    With this the whole setup of automatic email triggering functionality is integrated with the framework.

    Generating Reports

    A perfect test automation tool is what it need for the successful execution of the testing requirements in the Agile process. And there are multiple factors which play a critical role in the formation of a robust automation framework. One such element is reporting which not only make it aware of the status of the success or failure but helps in finding out the potential bugs. The essential qualities of a good test report are:

    • Brevity: A report should be short and concise.It should reveal the total no. of successes as well as failures.
    • Trackability: Captures all the footprints that could lead to the root-cause of a failure.
    • Traceability: It must provide the ability to review the following.
      • Historical data for test cases and failures
      • Age of a particular defect
    • Sharable: It should support a format which you can share through email or integrate with CI tools like Jenkins/Bamboo.
    • Test coverage: It should highlight the test coverage for the following.
      • Test coverage of the module under test.
      • Test coverage of the application under test.
    TestNG Framework Test report

    TestNG framework library provides a test report in HTML format every time the execution of the test suite is completed. It creates a test-output folder that is present at the root of the test project. This folder contains two types of test reports which are as follows:

    HTML: This html report file contains the test report of a recently executed test suite. It contains details like an error, reporter logs, groups, time, TestNG xml files, etc. as shown below.

    Emailable-report.html: This html report file contains the summary of current test execution that has test cases message in color codes. The test cases that are passed are in green color and the test cases which are failed are in red color as shown below.

    Customization of TestNG Report

    TestNG default reports are very detailed and handy. But still sometimes, it need to customize the report as per the team requirements such as remove or add one or more fields from the report, change the report layout, display the report in different format such as PDF, html, excel for all of such requirement TestNG framework has two interfaces that help in report customization as follows:

    ITestListener Interface

    This interface is used to customize real-time test report i.e. when it execute a handful of test cases using TestNG framework and we desire a report for an individual test case. In this scenario, for each test case, it have to implement ITestListener interface which will override onTestFailure, onTestSuccess, onTestStart, onTestSkipped, onFinish methods to convey the correct status of the currently executing test case into the report.

    IReporter Interface

    This interface is implemented to customize the final test report that is generated by TestNG framework. It has only one method to override i.e. generateReport. This method possesses all the required information to complete the test execution in the List<ISuite> with which report can be generated.

    Generating Application and Selenium Logs

    Below are the steps to generate log file in selenium webdriver test for software web application.

    1. Download log4j jar file from logging.apache.org
    2. Click on Zip folder link as shown in bellow image. Current version of log4j jar file is log4j-
      1.2.17. Version of log4j jar file may be different in future due to the frequent version change.
    3. On click the zip folder link, it will show you zip mirror links to download folder. Download and save zip folder by clicking on mirror link.
    4. Now Extract that zip folder and look inside that extracted folder. log4j-1.2.17.jar file will be there.
    5. Import log4j-1.2.17.jar file in the eclipse project
      Right click on your project folder and go to Build Path -> Configure Build path. It will open java build path window as shown in bellow image.
    6. Click on Add External JARs button and select log4j-1.2.17.jar file. It will add log4j-1.2.17.jar file in the build path.
    7. Click on OK button to close java build path window.
    8. Create Properties folder under the project folder and add log4j.properties file in it.

      Create folder with name = Properties under your project by right clicking on the project folder.

      Now the properties folder under project folder will looks like bellow.

    9. Set Run Configuration

      This is optional step. It need to set and Run configure if log file is not generated and display bellow given warning message in the console when the test case will run.

      log4j:WARN No appenders could be found for logger (dao.hsqlmanager). Go to Run -> Run Configurations -> Classpath

      Select User Entries -> Click on ‘Advanced’ button -> Select Add Folder -> And then select Properties folder from the project and click on OK. Now the Class tab will looks like bellow.

    10. Create example (Sample) test case under the project.
    11. Import log4j header file in the project
      import org.apache.log4j.*;
    12. Run the test case.
      public void test () throws InterruptedException
      {
      Logger log;
      driver.findElement(By.id("text1")).sendKeys("My First Name"); 
      log = Logger.getLogger(Mytesting.class);
      
      log.info("Type In Text field.");
      Select mydrpdwn = new 
      Select(driver.findElement(By.id("Carlist")));
      mydrpdwn.selectByVisibleText("Audi");  
      log = Logger.getLogger(Mytesting.class); 
      log.info("Select value from drop down.");
      WebDriverWait wait = new WebDriverWait(driver, 15);
      
      wait.until(ExpectedConditions.elementToBeClickable(By.id("te xt2")));
      }
      

    When this test case for software web application is executed, Go to folder = C:\Logs. log.log file will be created over there. Open that file and look in to it. Log written in the software test case will be inserted in the log file. In above example, Syntax marked with Pink color are written for inserting log. Now log can be write at any place in the test case of software web application as shown in above example.

    Summary
    • An Automation Framework is a set of guidelines like coding standards, test-data handling which provide an execution environment for the automation test scripts
    • An automation framework in place ensures consistent coding and that best practices are followed
    • Automation framework ensures that the complex task is broken down to smaller manageable tasks which can then be re-used whenever required.
    • In the modular testing framework, testers create test scripts on module wise by breaking down the complete application under test into smaller, independent tests
    • Data driven test automation framework is focused on separating the test scripts logic and the test data from each other
    • Data-driven is a test automation framework which stores test data in a table or spread spreadsheet format
    • In Data-driven test automation framework, input data can be stored in single or multiple data sources like xls, XML, csv, and databases
    • When test automating a web application using Selenium, it have to start the Selenium server first, so that a new Selenium session is created to talk to the web browser
    • Properties file is used to define properties of keys which would have been used in our Selenium project
    • TestNG framework library provides a test report in HTML format every time the execution of the test suite is completed
    • ITestListener Interface and IReporter Interface are used to customize TestNG Report
    • There are series of steps that needs to be followed to generate application and selenium logs

    Copyright 1999- Ducat Creative, All rights reserved.