Part 4: Hybrid Framework Development…

This post is the continuation of Hybrid Framework Development. For a complete understanding of Hybrid Framework Development, please go through the previous posts on Hybrid Framework Development.

Part 1: Hybrid Framework Development…
Part 2: Hybrid Framework Development…
Part 3: Hybrid Framework Development…

In this post, we will be creating a TestNG script. We will create two tests, although the flow is same we will use a different set of test data.

First Test Case: Navigate to New Tours application and perform a successful login. Search for a one-way flight departing from London on 7th.

Second Test Case: Navigate to New Tours application and perform a successful login. Search for a one-way flight departing from Paris on 10th.

  • Right click on the ‘src/test/java‘ folder and click New -> Package. Name the package as ‘com.testmadness.testcase’ and click Finish to close the window.
  • Inside ‘com.testmadness.testcase’ package, we will place our TestNG class. Right click on ‘com.testmadness.testcase’ package and select TestNG -> Create TestNG class. Name the class as ‘BookFlightTest.java’. Copy the below code into ‘BasePage.java’ file.
    package com.testmadness.testcase;
    
    import org.testng.annotations.Test;
    import com.testmadness.pages.HomePage;
    import com.testmadness.utils.Config;
    import com.testmadness.utils.Constants;
    import com.testmadness.utils.ExcelUtil;
    import com.testmadness.utils.Log;
    import com.testmadness.utils.Reports;
    import org.testng.annotations.BeforeMethod;
    import java.lang.reflect.Method;
    import org.apache.log4j.xml.DOMConfigurator;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeClass;
    
    public class BookFlightTest {
    	WebDriver driver;
    	HomePage homePage;
    	Reports report;
    	
      // First Test Case	
      @Test
      public void firstTestCase() {
    	  homePage = new HomePage(driver);
    	  try {
    		homePage.navigateToURL(ExcelUtil.getCellData(1, 1));
    		homePage.loginUser(ExcelUtil.getCellData(1, 2), ExcelUtil.getCellData(1, 3)).findFlights(ExcelUtil.getCellData(1, 4), ExcelUtil.getCellData(1, 5));
    	} catch (Exception e) {
    		e.printStackTrace();
    	} 
      }
      
      //Second Test Case	 
      @Test
      public void secondTestCase() {
    	  homePage = new HomePage(driver);
    	  try{
    		  homePage.navigateToURL(ExcelUtil.getCellData(2, 1));
    		  homePage.loginUser(ExcelUtil.getCellData(2, 2), ExcelUtil.getCellData(2, 3)).findFlights(ExcelUtil.getCellData(2, 4), ExcelUtil.getCellData(2, 5));
    	  }catch (Exception e) {
    			e.printStackTrace();
    		}  
      }
      
      // Before Method to start logging
      @BeforeMethod
      public void beforeMethod(Method method) {
    	  
              // Passing the test case method name
    	  Log.startTestCase(method.getName());
    	  report.startTest(method.getName());
    	  System.out.println(Config.getProp().getProperty("selenium.browser")); 
      }
      
      // After Method to end logging
      @AfterMethod
      public void afterMethod() {
    	    homePage.endTest();
    	    homePage.endReport();
    	    Log.endTestCase("firstTestCase");
    	  
      }
      
      //Before Class to initialize Log4j
      @BeforeClass
      public void beforeClass() {
    	    System.setProperty(Config.getProp().getProperty("logfoldername"), Constants.logFolderName);
    	    System.setProperty(Config.getProp().getProperty("logfilename"), Constants.logFileName);
    	    DOMConfigurator.configure("src/main/java/com/testmadness/utils/log4j.xml");
    	    try {
    		   ExcelUtil.setExcelFile(Constants.PATH_DATA, "Sheet1");
    		} catch (Exception e) {
    		   e.printStackTrace();
    		}
    		 report = new Reports();
    	}	
    }
    
  • In the above code we have used two ‘@Test’ methods for our two test cases. We have set the path of our test data sheet and completed the Log4j configuration in ‘@BeforeClass’ method. In ‘@BeforeMethod’ we started the Extent Report and Log4j logging. In ‘@AfterMethod’, we stopped the logging as well as closed the test.
  • After successful completion till here, our project will look like as displayed below.

    ‘src/test/java’ folder
  • We can create as many as test scripts using TestNG class. More details are on Part 1: Scripting using TestNG….

With this we completed the scripting part inside our Hybrid Framework. We automated two test cases. In the next post we will go through the reporting and logging which is going to be an interesting topic. Till then, Happy Learning!