MainMenu

Home Java Overview Maven Tutorials

Saturday 14 January 2017

Keyword Driven framework in selenium



Keyword driven Framework also known as Table-Driven testing or Action word based Testing.

For Video : CLICK HERE




Here we divide the Test cases in four steps :
1). Test Step
2). Object of test step
3). Action on Test object
4). Data for Test object.
Suppose we have to do the following task :
1).Open the browser
2).Navigate to URL
3).click on "discussion forum(QA)

What is Keywrod Driven Framework?


As the name it self says that "keywrod driven", means we are going to deal with keywords, means our selenium code will work on keywords that are written inside an excel file.
Main advantage of this framework is that re-usability of code.
First of all create an excel sheet like below :
then create a class(Actionkeywords) and inside that class create the method for each test step :

package kw;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Actionkeywords
{
static WebDriver driver;
public static void openbrwoser()
{
driver = new FirefoxDriver();
}
public static void navigate()
{
driver.navigate().to("http://www.way2testing.com");
}
public static void click_df()
{
driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[2]/div[2]/div[2]/p[2]/a")).click();
}
}

Now create a class(Excelutility ) and create a method inside it to access the keywords(column data) from the excel sheet :

package kw;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excelutility
{
static XSSFWorkbook wb;
static XSSFSheet sheet;
public static void setexcel(String path, int sheetnum) throws Exception
{
try
{
File src = new File(path);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheetAt(sheetnum);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static String getdata(int rownum, int cellnum)
{
String data = sheet.getRow(rownum).getCell(cellnum).getStringCellValue(); return data;
}
}

Now create a class(Execution) and create a method inside it, which will get the keyword from excel(from class Excelutility ) and based on that execute method from first created class(Actionkeywords):

package kw;
public class Execution
{
public static void main(String args[]) throws Exception
{
String path = "D:\\study material\\Selenium Final complete\\Selenium Test data\\MyDataEngine.xlsx";
Excelutility.setexcel(path, 0);
for(int i=0; i<=3; i++)
{
String keyword = Excelutility.getdata(i, 3);
if(keyword.equals("openbrowser"))
{
Actionkeywords.openbrwoser();
}
else if(keyword.equals("navigate"))
{
Actionkeywords.navigate();
}
else if(keyword.equals("click_DF"))
{
Actionkeywords.click_df();
}
}
}
}

Tags :

Keyword driven framework in selenium

,

Example of keyword driven framework in selenium

,

easy example of keyword driven framework

,

How to design keyword driven framework in selenium in java

No comments:

Post a Comment