
Keyword Driven Framework
This is working example for Keyword driven framework, here in the code it will check in excel sheet for the test cases to execute.
for example here we have used: TC_Login, TC_Book Flight, TC_Logout
we have created methods for all the cases and added the business login inside the method.
Pre-requisite:
- As we are running this code in firefox hence geko driver is required.
- Download the keyword excel file.
- Changed the path of geko driver and excel sheet in done.
you are good to go, please run the code and try yourself.
package rkdemo1;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Keyword_Driven {
//Initiate Driver
public static WebDriver driver;
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
//@SuppressWarnings("deprecation")
System.setProperty("webdriver.gecko.driver","F:\\Selenium\\geckodriver.exe");
//DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//capabilities.setCapability("marionette", true);
driver = new FirefoxDriver();
//Login
driver.get("https://demo.guru99.com/test/newtours");
Workbook workbook;
try {
workbook = Workbook.getWorkbook(new File("F:\\Selenium\\topics\\Keyword.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("submit")).click();
System.out.println("Loggdin Successfully");
System.out.println(driver.getCurrentUrl());
}
public static void FlightBook()
{
driver.findElement(By.xpath("/html/body/div[2]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/a")).click();
//driver.navigate().to("https://demo.guru99.com/test/newtours/reservation.php");
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.xpath("/html/body/div[2]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td/a/img")).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();
driver.findElement(By.xpath("/html/body/div[2]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a")).click();
}
public static void quitdriver()
{
driver.quit();
}
}