In this blog post, I will be sharing a sample prototype of a hybrid framework. This is a small prototype, we can use as per different automation need.

How to download it

The Framework is available at git location-
https://github.com/Prashant-11/HybridFramework

Step 1- Download/Clone this framework in your local

Step 2 – Import this project in Eclipse/JBoss/Intellij etc

Features of this Hybrid prototype framework

This framework has features which are-

i- Reporting using iTestListner interface for reporting

ii-Retry failed test cases using iRetryAnalyzer for retrying failed test cases

iii-DataProvidor annotation –TestNG Annotation

iv- Basic Exception handling

How to run it –

Step 1-

Set Yes, No in Excel sheet, which is placed under Resources folder, Refer below screenshot. In below excel on running it only “Scenario A” will run.

Step 2-

Run ” TestEngine ” class or Run testng.xml file, it will execute all rows which has Yes marked against scenarios

Excel sheet which contains Test Data and Result of execution will also be stored in the same(Column F)

Understanding Pom.xml and TestEngine class file

Please refer below pom.xml file, Pom dependency has been added are-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Maven_SDET_28July</groupId>
  <artifactId>Maven_SDET_28July</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SDET_Maven Project</name>
  
  <dependencies>      			
        <dependency>				
            <groupId>org.seleniumhq.selenium</groupId>								
            <artifactId>selenium-java</artifactId>								
            <version>2.45.0</version>								
		</dependency>				
        <dependency>				
            <groupId>org.testng</groupId>								
            <artifactId>testng</artifactId>								
            <version>6.8</version>								
            <scope>test</scope>							  			
       </dependency>		
       <dependency>
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-surefire-plugin</artifactId>
		  <version>3.0.0-M3</version>
		  <type>maven-plugin</type>
	</dependency>
		<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi-ooxml</artifactId>
	    <version>3.16</version>
	</dependency>
	<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.16</version>
    </dependency>	
   
</dependencies>
 <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>2.12.4</version>
               <configuration>
                  <suiteXmlFiles>
                     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                  </suiteXmlFiles>
               </configuration>
            </plugin>     
         </plugins>
      </build>
</project>

Understand TestEngine class

This class contains various TestNG annotation out of which the key methods which we are using are and important to understand are-

a- Method with name- credentials() which is using @DataProvider(name = “DP_Flow”). It is reading data from excel sheet and passing to the method Scenario

b- Method with name Scenario which is using @Test annotation also this is calling dataProvidor=”DP_Flow”. It executes all rows present in excel one by one

c- Inside the switch case statement call your test cases/scenarios.

d- There is a throw new SkipException which is mentioned in the very last of this class file which skips the test which is mentioned as No in Excel file.


e- There is a listener being called inside the above class, with the name “CustomReport”. Fail and Pass is being logged in the excel sheet using this listener. Please refer before class name-@Listeners(FrameworkClasses.CustomReport.class) .Its calling CustomReport class

package FrameworkClasses;


import java.awt.AWTException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.PropertyConfigurator;
import org.apache.poi.ss.usermodel.Cell;
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;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.SkipException;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import junit.framework.Assert;

@Listeners(FrameworkClasses.CustomReport.class)
public class TestEngine  
{
	static public WebDriver driver;
	public static int RowSetter = 0;
	
	
	
	ArrayList<String> row;
	//public StringBuilder htmlStringBuilder;
	
	Setter_Getter o_SetGet = new Setter_Getter();
		
	@BeforeSuite
	public void Prerunsetup() throws IOException
	{
						
	}
	
	
	@BeforeTest
	public void Setup() throws IOException, InterruptedException
	{
		String ViewType, Browser_Value;		
	}
	
	@DataProvider(name = "DP_Flow")
	public Iterator<Object[]> credentials() throws IOException
	{
		String RunType;				
	 	System.out.print("Inside Authentication1 --->");
		Collection<Object[]> arrayExcelData1 =  new ArrayList<>(); 
		File src=new File("./Resources/Read_Write.xlsx");
		FileInputStream fis=new FileInputStream(src);
		XSSFWorkbook wb=new XSSFWorkbook(fis);
		String[][] Arraydata = null;
		XSSFSheet sh1;
		sh1 = null;
		sh1= wb.getSheetAt(0);
		int totalNoOfCols = sh1.getLastRowNum();
		System.out.println("Last row count ++++ " + String.valueOf(totalNoOfCols));
		System.out.println("Count of data-->"+totalNoOfCols);
	    Arraydata=new String[2][totalNoOfCols];	    
	    XSSFRow Row_Excel = sh1.getRow(0);
	    int colNum = Row_Excel.getLastCellNum();
	    System.out.println("Total Number of Columns in the excel is : "+colNum);
	    int rowNum = sh1.getLastRowNum()+1;
	    System.out.println("Total Number of Rows in the excel is : "+rowNum);
	   // CellType type = ((Object) row).getCellTypeEnum();	    
	        for (int i= 1 ; i <= rowNum-1; i++)
			{
		    	ArrayList<String> row = new ArrayList<String>();
			    	for (int j= 0 ; j <= colNum-1; j++)
			    	{
			    		XSSFCell cell = sh1.getRow(i).getCell(j);
			    		RowSetter=i;
						switch(cell.getCellType())
						{
				    		case (Cell.CELL_TYPE_STRING):
				    			//System.out.println("***********Inside String ******"+"i-->"+i+"j-->"+j);
				    			row.add(sh1.getRow(i).getCell(j).getStringCellValue());
				    			break;
//				    			System.out.println("***********Inside Integer ******");
//				    		    int t = (int) sh1.getRow(i).getCell(j).getNumericCellValue();
//				    		   // System.out.println("***********TVALUE ******"+Integer.toString(t));
//				    		    row.add(Integer.toString(t));
				    			//break;		
				    		case (Cell.CELL_TYPE_BLANK):
				    			System.out.println("***********Inside String ******"+"i-->"+i+"j-->"+j);
				    			//System.out.println("***********Blank ******"+sh1.getRow(i).getCell(j).getStringCellValue());
				    			row.add(sh1.getRow(i).getCell(j).getStringCellValue());
				    			break;
			    		}			    	   
			    	 }			    	
			    	arrayExcelData1.add(new Object[]{row});
			}	        
		    return arrayExcelData1.iterator();		    
  }
		
	
	 @Test(dataProvider = "DP_Flow",retryAnalyzer=RetryAnalyzer.class)
	 public void Scenarios(ArrayList<String> row) throws IOException, InterruptedException, AWTException 
	 {				
		System.out.println("Execution-->"+row.get(0));		
		String RunStatus, Team, Scenerio, Location1, Location2, Srch_Param;
			
		o_SetGet.set_Row(row.get(0).toString());
		System.out.println("size of row"+row.size());
		
		for(int i=0;i<row.size();i++)
		{
			System.out.println("***Value before "+i+"***"+row.get(i).toString());
		}
				
				
		if(row.get(1).contains("Yes"))
		{
			System.out.println("***Value before 4***"+row.get(4).toString());
			//System.out.println("VE--Teampath>*(*(*"+Teampath);
			switch(row.get(2)) 
			{
			case "ScenarioA":
				System.out.println("***Inside Scenario A***");
				Assert.fail();
				//Call Test Method here
				
				break;
			case "ScenarioB":
				System.out.println("***Inside Scenario B***");	
				Assert.fail();
				
				break;
        	   default :		        	
		        		System.out.println("***Inside Defaulf***");	
		     
			}
			
		}
		else if(row.get(1).contains("No"))
		{
			
			{
				throw new SkipException("Skipping this Test");
			}
			
		}
	} 
	
	@AfterSuite
	public void PostExecution() throws IOException, ParserConfigurationException, SAXException
	{
		String FileName;
		//ileName = "ExcelReport.xls";
		
	}
}

OverView of iTestListner Class

Whenever a test case is failed or passed this class will be executed and corresponding code will be triggered. In below code, We are logging Pass,Fail in Excel sheet.
package FrameworkClasses;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomReport implements ITestListener {

	
	public void onTestSuccess(ITestResult result) {
		// TODO Auto-generated method stub
		WritetoReport("Fail");
		
	}

	public void onTestFailure(ITestResult result) {
		// TODO Auto-generated method stub
		
		WritetoReport("Fail");
		
		
	}
	
	public void WritetoReport(String status)
	{
		TestEngine obj_TE = new TestEngine();
		try {
			FileInputStream file = new FileInputStream(new File("./Resources/Read_Write.xlsx"));
			XSSFWorkbook wb = new XSSFWorkbook(file);
			XSSFSheet sheet = wb.getSheetAt(0);
			int Last = sheet.getLastRowNum();
			int noofcol = sheet.getRow(0).getLastCellNum();
			sheet.getRow(obj_TE.RowSetter).getCell(noofcol-1).setCellValue(status);
			file.close();
			FileOutputStream outFile = null;
			try
			{
				outFile = new FileOutputStream(new File("./Resources/Read_Write.xlsx"));
			}
			catch(Exception ex)
			{
				
			}
			wb.write(outFile);
			outFile.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
}

Overview of Retry analyzer class

Please have a look at below retry analyzer code-

It will try executing the code twice, first failed, then 2 times it will try to execute it.

This retry analyzer code is being called in TestEngine class @Test annotation.

Below annotation is being used in TestEngine class(refer above code of Test Engine class. Refer Scenario method.

@Test(dataProvider = “DP_Flow”,retryAnalyzer=RetryAnalyzer.class)

package FrameworkClasses;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer{

	int counter = 0;
	int retryLimit = 3;
	
	public boolean retry(ITestResult result) {
		// TODO Auto-generated method stub
		
		
		if(counter < retryLimit)
		{
			System.out.println("***Inside retry counter***"+counter);
			counter++;
			return true;
		}
		return false;		
		}
	

}

Summary

This is a small prototype using testNG Dataprovidor annotation, you can extend this by calling page factory or page object model.

You can integrate this with Jenkins also by running this flow from PoM.xml file to enhance it further.

Please visit facebook page-
https://www.facebook.com/automatorsworld