Automation Frameworks With Example (Selenium)
There are 5 types of
Framework
1. Linear Framework
2. Library Architecture Framework
3. Keyword driven framework
4. Data Driven Framework
5. Hybrid Framework
Each and Every framework has its own merits and demerits.
These all frameworks can be applied to any Automation Tool.
How to choose a particular framework?
There is no thumb rule to choose a framework, but it depends
on many factors like Type of Application (AUT), Number of available resources,
Reusability of Test Script.
We will see each and every framework with Example.
Example Scenario: User books a flight
ticket on Travel portal and logout .
1.
Linear Framework
This is very simple framework and good for beginner.
Merits
: Not much coding expertise is required .
Demerits
: 1.Test case is not reusable .
2. Lots of Rework will be
required in case of any Change.
Following code is an example of Linear
Framework.
package com.project1;
import java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.*;
import
org.openqa.selenium.support.ui.Select;
public class Linear_Framework {
public static void main(String[] args)
{
//Initiate Driver
WebDriver driver = new FirefoxDriver();
//Login
driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("Mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
//Book a Ticket
Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
Passangers.selectByVisibleText("2");
Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
Departingfrom.selectByVisibleText("Frankfurt");
Select FromMonth = new
Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
FromMonth.selectByVisibleText("September");
Select ArrivingIn = new
Select(driver.findElement(By.cssSelector("select[name='toPort']")));
ArrivingIn.selectByVisibleText("New
York");
Select ToMonth = new
Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
ToMonth.selectByIndex(10);
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
driver.findElement(By.name("findFlights")).click();
driver.findElement(By.name("reserveFlights")).click();
driver.findElement(By.name("passFirst0")).sendKeys("Name1");
driver.findElement(By.name("passLast0")).sendKeys("LastName");
driver.findElement(By.name("creditnumber")).sendKeys("1234566");
driver.findElement(By.name("buyFlights")).click();
//Logout
driver.findElement(By.linkText("SIGN-OFF")).click();
//Close
Browser
driver.quit();
}
}
2.
Library Architecture Framework
This
is a approach where all test cases are analysed to find out reusability and these test cases are
created as functions and will be stored in other files and can be called
whenever needed ,In Short Test case will be fragmented into functions .
Merits:
It will save time and Effort due to reusability.
Demerits:
1.Programming Knowledge is required.
2. Data is hardcoded, so
Test script cannot be run using different data values.
Following code is an example of Library
Architecture, In Main () only functions are present.
package com.project1;
import java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.*;
import
org.openqa.selenium.support.ui.Select;
public class Library_Framework {
//Initiate Driver
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args)
{
//Login
Login("Mercury", "mercury");
FlightBook();
Logout();
}
//Function
public static void Login(String
Username,String Password)
{
driver.navigate().to("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys(Username);
driver.findElement(By.name("password")).sendKeys(Password);
driver.findElement(By.name("login")).click();
}
public static void FlightBook()
{
Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
Passangers.selectByVisibleText("2");
Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
Departingfrom.selectByVisibleText("Frankfurt");
Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
FromMonth.selectByVisibleText("September");
Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
ArrivingIn.selectByVisibleText("New
York");
Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
ToMonth.selectByIndex(10);
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
driver.findElement(By.name("findFlights")).click();
driver.findElement(By.name("reserveFlights")).click();
driver.findElement(By.name("passFirst0")).sendKeys("Name1");
driver.findElement(By.name("passLast0")).sendKeys("LastName");
driver.findElement(By.name("creditnumber")).sendKeys("1234566");
driver.findElement(By.name("buyFlights")).click();
}
public static void Logout()
{
driver.findElement(By.linkText("SIGN-OFF")).click();
}
public static void quitdriver()
{
driver.quit();
}
}
3.
Keyword driven Framework
There are 2 types of keyword driven
framework
1. Operation level
2. Function level
Merits: 1.Keyword Driven Framework is very Generic so
it can be used to other Test cases
as
well
Demerits : It requires lots of efforts
and expertise to prepare complex
Libraries
Here I have prepared a code for Function Level
Keyword driven Framework.
Steps:
1.
Create an Excel sheet for Test case Flow.
2. Create individual functions for test cases
(Login(),FlightBook(),Logout())
3.
Create a function Which Can read Excel sheet and can execute mapped
function.
By following above
steps, final code should look like as
below.
package com.project1;
import java.io.File;
import java.io.IOException;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import
jxl.read.biff.BiffException;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.*;
import
org.openqa.selenium.support.ui.Select;
public class Keyword_Driven {
//Initiate Driver
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args)
{
//Login
driver.navigate().to("http://newtours.demoaut.com/");
Workbook
workbook;
try {
workbook
= Workbook.getWorkbook(new File("C:\\Users\\Public\\Documents\\Test
Excel\\Keyword_driven.xls"));
Sheet
sheet = workbook.getSheet(0);
//code to read Test
Case
int row=sheet.getRows();
for(int i=1;i<row;i++)
{
String
Testcasename=sheet.getCell(0,i).getContents().toString();
//Call function
ExecuteFunction to Read Mapped Function
ExecuteFunction(Testcasename);
}
//Close Browser
quitdriver();
}
catch (BiffException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
public static void
ExecuteFunction(String Casename)
{
String
Option=Casename.trim();
if(Option.equalsIgnoreCase("TC_Login") )
{
//Execute Login Function
Login("Mercury", "mercury");
}
else if(Option.equalsIgnoreCase("TC_Book
Flight"))
{
//Execute FlighBook Function
FlightBook();
}
else if(Option.equalsIgnoreCase("TC_Logout"))
{
//Execute Logout
Logout();
}
}
public static void Login(String
Username,String Password)
{
driver.findElement(By.name("userName")).sendKeys(Username);
driver.findElement(By.name("password")).sendKeys(Password);
driver.findElement(By.name("login")).click();
}
public static void FlightBook()
{
Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
Passangers.selectByVisibleText("2");
Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
Departingfrom.selectByVisibleText("Frankfurt");
Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
FromMonth.selectByVisibleText("September");
Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
ArrivingIn.selectByVisibleText("New
York");
Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
ToMonth.selectByIndex(10);
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
driver.findElement(By.name("findFlights")).click();
driver.findElement(By.name("reserveFlights")).click();
driver.findElement(By.name("passFirst0")).sendKeys("Name1");
driver.findElement(By.name("passLast0")).sendKeys("LastName");
driver.findElement(By.name("creditnumber")).sendKeys("1234566");
driver.findElement(By.name("buyFlights")).click();
}
public static void Logout()
{
driver.findElement(By.linkText("SIGN-OFF")).click();
}
public static void quitdriver()
{
driver.quit();
}
}
For Operation level framework different keywords are written
in excel sheet and can be read using Function .this is quite complex.
Excel sheet can be as follows:
4.
Data-Driven framework
In this type of framework Data is not hardcoded with script
,but data is provided from external source.
This external source can be Excel sheet, TXT file, Database etc.
Merits: As data is provided from external source same
script can be run for different datasets.
Demerits: Good
efforts and technical expertise is required to create functions which connects to
Database
Following code is an example of Data-driven Framework
package com.project1;
import java.io.File;
import java.io.IOException;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.*;
import
org.openqa.selenium.support.ui.Select;
import jxl.*;
import
jxl.read.biff.BiffException;
public class DataDriven_Framework
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
//Login
driver.navigate().to("http://newtours.demoaut.com/");
try {
Workbook
workbook = Workbook.getWorkbook(new File("C:\\Users\\Public\\Documents\\Test
Excel\\Excel1.xls"));
Sheet
sheet = workbook.getSheet(0);
driver.findElement(By.name("userName")).sendKeys(sheet.getCell(0,
1).getContents());
driver.findElement(By.name("password")).sendKeys(sheet.getCell(1,
1).getContents());
driver.findElement(By.name("login")).click();
Select
Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
Passangers.selectByVisibleText(sheet.getCell(2,
1).getContents());
Select
Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
Departingfrom.selectByVisibleText(sheet.getCell(3,
1).getContents());
Select FromMonth = new
Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
FromMonth.selectByVisibleText(sheet.getCell(4,
1).getContents());
Select ArrivingIn = new
Select(driver.findElement(By.cssSelector("select[name='toPort']")));
ArrivingIn.selectByVisibleText(sheet.getCell(6,1).getContents());
Select ToMonth = new
Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
ToMonth.selectByVisibleText(sheet.getCell(5,1).getContents());
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
driver.findElement(By.name("findFlights")).click();
driver.findElement(By.name("reserveFlights")).click();
driver.findElement(By.name("passFirst0")).sendKeys(sheet.getCell(7,
1).getContents());
driver.findElement(By.name("passLast0")).sendKeys(sheet.getCell(8,
1).getContents());
driver.findElement(By.name("creditnumber")).sendKeys(sheet.getCell(9,
1).getContents());
driver.findElement(By.name("buyFlights")).click();
}
catch (BiffException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
}
5.
Hybrid Frameworks
This is combination of Keyword Driven and Data Driven
Framework ,Both above examples can be
used to explain it
Merits:
Best features can be used of any type of framework


No comments:
Post a Comment