MainMenu

Home Java Overview Maven Tutorials

Saturday, 14 January 2017

Hybrid Driven Framework in Selenium for beginner

For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Hybrid Driven Framework in selenium


For Video : CLICK HERE For Part1



For Video : CLICK HERE For Part2



When data driven & Keyword driven framework are combined then it will become hybrid driven frame work. This framework take the input data from excel sheet & executed test as per keyword of excel.

Advantages:
The Hybrid framework is build with a number of reusable modules / function libraries that are developed with the following features in mind:
Maintainability – Hybrid framework significantly reduces maintenance effort
Re-usability – It allows to reuse test cases and library functions
Manageability - effective test design, execution, and traceability
Accessibility – easy to design, develop, modify and debug test cases while executing
Availability – Allows to schedule automation execution
Reliability – due to advanced error handling and scenario recovery
Flexibility – framework independent of system or environment under test
Measurability – customizable reporting of test results ensure the quality output.
Scenarios :
1).Open the Firefox Browser
2).Navigate to the URL
3).Enter data in searchbox
4).Click enter button
5).Click on back button
First of all we will create an excel file(hybrid1.xlsx) which will have data that will be entered in text box.

We will also create an excel file(hybrid2.xlsx) which will have keywords of above described scenarios.

Now we will create a class(Getdatafromexcel) and create two method to access the data from excelsheet, one method to access the data from hybrid1.xlsx sheet & second method to access data from hybrid2.xlsx sheet.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;
public class GetDataFromExcel
{
static XSSFWorkbook wb;
static XSSFSheet sheet;
static File src;
static FileInputStream fis;
String data;
public static String inputdata(int sheetnum, int rownum, int cellnum) throws Exception
{
try
{
src = new File("D:\\study material\\Selenium Final complete\\HybridDriven framework file\\hybrid1.xlsx");
fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheetAt(sheetnum);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
String data = sheet.getRow(rownum).getCell(cellnum).getStringCellValue();
return data;
}
public static String keyword(int sheetno, int rowno, int cellno) throws IOException
{
src = new File("D:\\study material\\Selenium Final complete\\HybridDriven framework file\\hybrid2.xlsx");
fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheetAt(sheetno);
String data2 = sheet.getRow(rowno).getCell(cellno).getStringCellValue();
return data2;
}
}


Now i will create a class(ActionKeyword) which will have separate methods for each keyword that are present in keywords.xlsx sheet.

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Keyword
{
WebDriver driver;
public static void openbrowser()
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
public static void gotolink()
{
driver.navigate().to("https://www.google.co.in/?gfe_rd=cr&ei=mvzbWOyZNOzs8Af2hbLIDg&gws_rd=ssl");
}
public static void enterdata(int t) throws Exception
{
WebElement searchbox = driver.findElement(By.xpath(".//*[@id='lst-ib']"));
String text = GetDataFromExcel.inputdata(0, t, 0);
searchbox.sendKeys(text);
}
public static void hitenter()
{
Actions act = new Actions(driver);
act.sendKeys(Keys.ENTER).build().perform();
}
public static void goback()
{
driver.navigate().back();
}
}



Finally i will create a class(ExecutionEngine) which will get the keywords from excel & based on that keyword it will execute methods of ActionKeyword Class.

import java.io.IOException;
import org.testng.annotations.Test;
public class ExecutionEngine
{
int j;
@Test
public void i() throws Exception
{
Keyword object = new Keyword();
for(int i = 0; i<6 ;i++)
{
String data = GetDataFromExcel.keyword(0, i, 0);
if(data.equals("openbrowser"))
{
kyword.openbrowser();
}
if(data.equals("gotolink"))
{
kyword.gotolink();
}
if(data.equals("enterdata"))
{
kyword.enterdata(1);
}
if(data.equals("hitenter"))
{
kyword.hitenter();
}
if(data.equals("goback"))
{
kyword.goback();
}
}
}
}

-------------------------------------------------------------------------------------
Before proceeding to Hybrid driven Framework, lets have a look on java reflection.


An easy example to learn the concept of reflection :
there is a class names as csc which have a number of methods & i want to execute them
to do so....
i will create another class ReflectDemo , within this class i will create the object of class csc, then i will create a Method array, and i will store all the methods of class csc & by for or any other loop i will eccess them.
below is Code :
public class csc
{
public static void g()
{
System.out.println("chandan singh");
}
public static void h()
{
System.out.println("chandan singh chauhan");
}
public static void i()
{
System.out.println("adhiraj singh");
}
}
--------------------------------------------------------------------------------------------------------
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectDemo
{
public static csc obj;
public ReflectDemo()
{
obj = new csc();
}
public static void main(String args[]) throws Exception
{
ReflectDemo ref = new ReflectDemo();
Method[] list = obj.getClass().getDeclaredMethods();
for(int i =0; i<3 ;i++)
{
list[i].invoke(obj);
}
}
}

--------------------------------------------------------

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ExecutionEngine2
{
public static Keyword kyword;
public static String skeyword;
public static Method[] m1;
public static void main(String args[]) throws Exception
{
ExecutionEngine2 ee = new ExecutionEngine2();
kyword = new Keyword();
Method[] m1 = kyword.getClass().getMethods();
for(int i = 1; i <6 ; i++)
{
skeyword = GetDataFromExcel.keyword(0, i, 0);
for(int t =0; t<m1.length; t++)
{
if(m1[t].getName().equals(skeyword))
{
System.out.println(m1[t].getName());
m1[t].invoke(kyword);
}
}
}
}
}
Tags :- Easy Example of Java Reflection,Hybrid Driven Framework, How to create hybrid driven framework in selenium, hydrid driven framework with example.

No comments:

Post a Comment