In my last post I gave an introduction on Data Driven Framework. In this post we will mainly concentrate on developing a sample Data Driven Framework.
- Launch Eclipse. Create a new Java Project by clicking on File -> New -> Java Project.
- Give the project name as ‘DatadrivenFramework’ and click on Finish to close the window. Once the project is created, we will have ‘src‘ folder and ‘JRE System Library‘ by default.
- Next step is to add the Selenium Jars. Right Click on the project name and click Properties. Click on Java Build Path -> Add External Jars. Go to the local folder where ‘selenium-server-standalone-3.0.1’ is saved. We will be using 3.0.1 version from our last project. Add the Jar and click OK to close the window. Once complete, we will see ‘Referenced Libraries‘ included in our project.
- Next step is add the Chrome Driver. Right click on the project name and click New -> Folder. Give the folder name as ‘Chrome’ and click Finish to close the window. Now place the Chrome driver that we already downloaded in our last project into the ‘Chrome’ folder.
- To work with input files like Excel, CSVs etc we need to download Apache POI jars and add them into our project.
- Go to Google and type in ‘apache poi jar download’. Click on the first link ‘Apache POI – Download Release Artifacts’. URL: Apache POI
- Click on the latest stable version. As of today Apache POI 3.15 is the stable version. Go to Binary Distribution section and click on the zip file to start the download.
- Unzip the downloaded file. Now you have to add all the Jars into your project. To import the Jars, right click on the project name and click Properties. Click on Java Build Path -> Add External Jars. Go to the local folder where ‘poi-3.15’ is saved. Add all Jars in the ‘poi-3.15’ folder. Make sure to add the Jars in ‘lib’ and ‘ooxml-lib’ folders too. Click OK to close the window. The ‘Referenced Libraries‘ in the project will be updated with the Apache POI Jars.
- The next step is to create the test data Excel Sheet. You can create one test data Excel sheet as shown below and save it as ‘TC_SearchSelenium.xlsx’. Apache POI works in MS Excel. It will not work for Mac Numbers. So even if you are working in Mac machine, make sure to have MS Excel installed.
- Right click on the project name and click New -> Folder. Give the folder name as ‘TestResults’ and click Finish to close the window. In this folder the test data Excel sheet with ‘PASS/FAIL’ result will be placed once the execution completes.
- Now we need to create a new package inside the ‘src‘ folder of our project. Right click on the ‘src‘ folder and click New -> Package. Name the package as ‘com.selenium.data’ and click Finish to close the window.
- We have to add the test data Excel into this package. You can do this by drag and drop or else you can copy the Excel and directly paste it into your Project/src/com/selenium/data folder.
- Right click on the ‘src‘ folder and click New -> Package. Name the package as ‘com.selenium.utils’ and click Finish to close the window.
- On the ‘com.selenium.utils’ package, right click on New -> Class to create a new class file. Name the class as ‘Constants’. Click Finish to create a new class. Copy the below code and paste it into the ‘Constants’ class file.
package com.selenium.utils; public class Constants { // TestResults folder location public static final String PATH_RESULTS = System.getProperty("user.dir") + "/TestResults/"; // Test Data Excel location public static final String PATH_DATA = System.getProperty("user.dir") + "/src/com/selenium/data/TC_SearchSelenium.xlsx"; // Test Data Excel Sheet name public static final String SHEETNAME = "Sheet1"; // Test Data Excel File Name public static final String FILE_NAME = "TC_SearchSelenium.xlsx"; }
- On the ‘com.selenium.utils’ package, right click on New -> Class to create a new class file. Name the class as ‘ExcelUtil’. Click Finish to create a new class.
- Copy the below code in to the ‘ExcelUtil’ class and save it.
package com.selenium.utils; import java.io.FileInputStream; import java.io.FileOutputStream; 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 ExcelUtil { private static XSSFSheet excelWSheet; private static XSSFWorkbook excelWBook; private static XSSFCell cell; private static XSSFRow row; /* * This method is to set the File path and to open the Excel file. Pass * Excel Path and Sheet name as Arguments to this method */ public static void setExcelFile(String Path, String SheetName) throws Exception { try { // Open the Excel file FileInputStream ExcelFile = new FileInputStream(Path); // Access the Excel workbook excelWBook = new XSSFWorkbook(ExcelFile); // Access the Excel Sheet excelWSheet = excelWBook.getSheet(SheetName); } catch (Exception e) { e.printStackTrace(); } } /* * This method is to read the test data from the Excel cell. We are passing * parameters as Row number and Column number */ public static String getCellData(int RowNum, int ColNum) throws Exception { try { // Access a particular cell in a sheet cell = excelWSheet.getRow(RowNum).getCell(ColNum); String CellData = cell.getStringCellValue(); return CellData; } catch (Exception e) { return null; } } /* * This method is to write in the Excel cell. Passing Row number and Column * number are the parameters */ public static void setCellData(String Result, int RowNum, int ColNum) throws Exception { try { cell = excelWSheet.getRow(RowNum).getCell(ColNum); if (cell == null) { cell = row.createCell(ColNum); cell.setCellValue(Result); } else { cell.setCellValue(Result); } // Update the Test Results folder with 'PASS/FAIL results' FileOutputStream fileOut = new FileOutputStream(Constants.PATH_RESULTS + Constants.FILE_NAME); excelWBook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (Exception e) { e.printStackTrace(); } } }
- Next we will create our actual test case. We will take the same code from our first test case, however we will do some modification on the input parameters.
- Right click on the ‘src‘ folder and click New -> Package. Name the package as ‘com.selenium.testcase’ and click Finish to close the window.
- On the ‘com.selenium.testcase’ package, right click on New -> Class to create a new class file. Name the class as ‘TC_SeleniumSearch’. Click Finish to create a new class. Copy the below code and paste it into the ‘TC_SeleniumSearch’ class file.
package com.selenium.testcase; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import com.selenium.utils.Constants; import com.selenium.utils.ExcelUtil; public class TC_SeleniumSearch { public static void main(String[] args) throws Exception { // Setting System properties System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Chrome/chromedriver"); // Initialize WebDriver WebDriver driver = new ChromeDriver(); //Set the File path of our test data Excel ExcelUtil.setExcelFile(Constants.PATH_DATA, Constants.SHEETNAME); /* Navigate to Google Website. Test data taken from row 1 column 2 */ driver.get(ExcelUtil.getCellData(1, 2)); // Maximize browser window driver.manage().window().maximize(); // Relative XPath for the text box driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys(ExcelUtil.getCellData(1, 3)); /* Find the Web Element Search icon. After finding, click the search icon */ driver.findElement(By.id("_fZl")).click(); /* Wait for page to load. Thread.sleep() throws InterruptedException */ Thread.sleep(5000); //Enter text from test data Excel row 1 column 4 driver.findElement(By.partialLinkText(ExcelUtil.getCellData(1, 4))).click(); /* Wait for page to load. Thread.sleep() throws InterruptedException */ Thread.sleep(3000); // Store the title in a variable String title = driver.getTitle(); /* Title verification. Get text from test data Excel row 1 column 5 */ if (title.equals(ExcelUtil.getCellData(1, 5))) { System.out.println("TITLE VERIFIED."); /*Set the PASS result in test data Excel row 1 column 6 */ ExcelUtil.setCellData("PASS", 1, 6); System.out.println("TEST CASE PASSED"); } else { /*Set the FAIL result in test data Excel row 1 column 6 */ ExcelUtil.setCellData("FAIL", 1, 6); System.out.println("TITLE NOT VERIFIED."); } // Close and quit the driver to close the Browser driver.close(); driver.quit(); } }
- As you see in the above code, we have removed all the hard coded test data from our test case and instead gave a reference to the test data Excel sheet. Our test case became more maintainable with this approach and any further updates to test data is also simple.
- The overall project structure will look like:
- The final step is to execute our test case. Right click on “TC_SeleniumSearch” class and click Run As -> Java Application. As we progress with our execution we can see the all the input data is taken from our test data Excel sheet. Test Case execution successfully completes and we see the following output in the console.
- As we check the ‘TestResult’ folder, a new Excel sheet is placed with the test result. Open the Excel sheet and we can see the test results are updated with ‘PASS results’.
With this we came to an end of our Data Driven Framework creation. You can modify this code to take the input of different browsers. For that you just need to update the “TC_SeleniumSearch” class file with ‘if-else’ logic.
Happy Learning and do share your comments!
Additional Information:
If you refer to ‘ExcelUtil’ class, we have used instances of XSSFWorkbook, XSSFSheet etc to access a particular cell in an Excel. Apache POI provides documentation on all of these classes. You can visit the below URL for more details: