Tuesday 25 March 2014

TestNG Annotations

What’s the hierarchy of TestNG annotations? 


Ans:
  1. org.testng.annotations.Parameters (implements java.lang.annotation.Annotation)
  2. org.testng.annotations.Listeners (implements java.lang.annotation.Annotation)
  3. org.testng.annotations.Test (implements java.lang.annotation.Annotation)
  4. org.testng.annotations.AfterMethod (implements java.lang.annotation.Annotation)
  5. org.testng.annotations.BeforeTest (implements java.lang.annotation.Annotation)
  6. org.testng.annotations.BeforeMethod (implements java.lang.annotation.Annotation)
  7. org.testng.annotations.Optional (implements java.lang.annotation.Annotation)
  8. org.testng.annotations.AfterTest (implements java.lang.annotation.Annotation)
  9. org.testng.annotations.Guice (implements java.lang.annotation.Annotation)
  10. org.testng.annotations.BeforeGroups (implements java.lang.annotation.Annotation)
  11. org.testng.annotations.ExpectedExceptions (implements java.lang.annotation.Annotation)
  12. org.testng.annotations.TestInstance (implements java.lang.annotation.Annotation)
  13. org.testng.annotations.NoInjection (implements java.lang.annotation.Annotation)
  14. org.testng.annotations.AfterSuite (implements java.lang.annotation.Annotation)
  15. org.testng.annotations.AfterClass (implements java.lang.annotation.Annotation)
  16. org.testng.annotations.AfterGroups (implements java.lang.annotation.Annotation)
  17. org.testng.annotations.DataProvider (implements java.lang.annotation.Annotation)
  18. org.testng.annotations.BeforeSuite (implements java.lang.annotation.Annotation)
  19. org.testng.annotations.BeforeClass (implements java.lang.annotation.Annotation)
  20. org.testng.annotations.Factory (implements java.lang.annotation.Annotation)
  21. org.testng.annotations.Configuration (implements java.lang.annotation.Annotation)
  22. org.testng.annotations.ObjectFactory (implements java.lang.annotation.Annotation)

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();
}

}