Skip to main content

Selenium Framework[Test Data Driven Framework] - Framework Designing

Before starting your framework designing, Check below two topics


Test Data Driven Framework

    This type explains it's the definition in its name. The key idea in this framework is, test data in this framework will be managed by an external file. So, it will be helpful in changing the test data and can able to run the same test with multiple test data. This will help in reducing the number of scripts. By using Apache POI, we can able to read and write in the spreadsheet. Apache POI is used widely so I mentioned that other than this there are some more tools that provide methods to read and write in an excel spreadsheet.

    For writing one or two test scripts, a basic package is necessary but when we try to build more test cases, it should be an easily understandable format. Different packages should be used to differentiate it's used in the framework.

  • Package 1 - Excel file and methods to read and write excel data
  • Package 2 - Global variables that are used across the site. It will help you in changing values in the future
  • Package 3 - Contains test cases

Step -1: Build the packages and necessary files

    At the beginning of the designing follow the below steps

  • Create a Java project
  • Add Selenium, Apache POI, TestNG Jar files in the build path of the created project
  • Based on the above note create packages. I created 4 packages in this example(globalVariable - library of variables that are used throughout the project, testCases - Contains test scripts, testDataAndOtherStartupMethods - Contains test data related methods and browser startup methods


Step - 2: Browser Instantiation

  • Under testDataAndOtherStartupMethods, create class BrowserInstantiation and create method browserStart() to set the property.
BrowserInstantiation.java

        package testDataAndOtherStartupMethods;

        public class BrowserInstantiation {
        public void browserStart() {
        System.setProperty("webdriver.chrome.driver","F:\\Selenium    drivers\\chromedriver.exe");
        }
        }

  • In the testCases package, call the BrowserInstantiation method at the first line of the main method. Create class under testCases package.
BasicRead.java

        package testCases;

        import java.io.IOException;
        import testDataAndOtherStartupMethods.BrowserInstantiation;

        public class BasicRead {

        public static void main(String[] args) throws IOException {
        BrowserInstantiation browserInstantiation = new BrowserInstantiation();
        browserInstantiation.browserStart();
                }
        }

Step 3 - Place Excel file and create methods to access those methods

  • Import test data export file to the package testDataFile. Create class testDataAccessMethods under testCases package. Create methods based on the requirements that were mentioned in Read and Write in Excel using Apache POI
TestDataAccessMethods.java

        package testDataAndOtherStartupMethods;

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.IOException;
        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;

        public class TestDataAccessMethods {
        File file;
        public void testDataFileSet(String fileLocation) {
        this.file = new File(fileLocation);
        }
        public String testDataRead(int rowNumber, int CellNumber) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
            XSSFWorkbook wb=new XSSFWorkbook(inputStream);
            XSSFSheet sheet=wb.getSheet("UserDetails");
            XSSFRow row=sheet.getRow(rowNumber);
            XSSFCell cell=row.getCell(CellNumber);
            return cell.getStringCellValue();
        }
        }

Step 4 - Create an object for this class and access the data

  • In the BasicRead.java test case script, create an object reference for the class testDataAccessMethods, and access the method testDataReadAndRight to get the cell value of the particular value.
        TestDataAccessMethods testDataAccessMethods = new TestDataAccessMethods();
        testDataAccessMethods.testDataRead(1, 0)

Step 5 - Create a sample Script

  • Put all the locators, site URL in the globalVariable package. It can be used anywhere in the program with its object reference
Below are the class file screenshots

GlobalVariables.java


BrowserInsantiation.java


TestDataAccessMethods.java


BasicRead.Java




Find the full code in this link - https://github.com/Priyadharshan0297/TDDFramework/tree/main/TDDFramework    


Feel free to post your queries to us

Comments