Friday 21 March 2014

Sample Keyword Driven framework in Selenium Webdriver.


Framework: It is a set of Guidelines designed by an Expert in a generic way to accomplish a task in an effective and efficient manner.

Please find the below sample code for Keyword driven framework in Selenium Web driver.

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
       webdriver driver=new firefoxdriver();
public static void main(String[] args)
{
   
        //Login
        driver.get("http://newtours.demoaut.com/");
     
   
              Workbook workbook;
              try {

                     File f=new File("C:\\Users\\Public\\Documents\\Test Excel\\Keyword_driven.xls");
                  Fileinputstream fis=new Fileinputstream(f);
                     workbook = Workbook.getWorkbook(fis);
                     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();
}

}

5 comments:

  1. nice post! Everything is very clear!

    ReplyDelete
  2. Thanks for sharing such informative article on Loadrunner Automation testing tool. This load testing tool will provide most precise information about the quality of software. Loadrunner Training in Chennai | Best Loadrunner training institute in Chennai

    ReplyDelete
  3. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things. Best Selenium Training in Bangalore

    ReplyDelete
  4. It's a wonderful post and very helpful, thanks for all this information about Software testing.You are including better information regarding this topic in an effective way.Thank you so much.
    Selenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery

    ReplyDelete
  5. Digital marketing is the component of marketing that utilizes internet and online based digital technologies such as desktop computers, mobile phones and other digital media and platforms to promote products and services.Java training in chennai

    python training in chennai

    web designing and development training in chennai

    selenium training in chennai

    digital-marketing training in chennai

    ReplyDelete