Tuesday 28 May 2013

PageObject

Copy all 3 scripts into your selenium2+TestNG environment and run the K7 script

LoginPage:

package PageObject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;


public class LoginPage {

final WebDriver driver;

 @FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[2]/td/input")
private WebElement usernameEditbox;

 @FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[4]/td/input")
private WebElement passwordEditbox;

 @FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[5]/td/input")
private WebElement signinButton;

 public LoginPage(WebDriver driver) {
this.driver = driver;

}



public void enterUsername(String login) {
usernameEditbox.clear();
usernameEditbox.sendKeys(login);

}


public void enterPassword(String password) {
passwordEditbox.clear();
passwordEditbox.sendKeys(password);

}


public void clickSigninButton() {
signinButton.click();

}


public LogOff login(String login, String password) {
enterUsername(login);
enterPassword(password);
clickSigninButton();
return PageFactory.initElements(driver, LogOff.class);

}

}

LogOff:

package PageObject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class LogOff {

  final WebDriver driver;

 public LogOff(WebDriver driver) {

this.driver = driver;

}

 @FindBy(how = How.XPATH, using=".//*[@id='pg']/div[1]/div[1]/div[2]/div[4]/ul/li[5]/a")
private WebElement signOff;

 public LoginPage singOff(){
signOff.click();
return PageFactory.initElements(driver, LoginPage.class);
}


}


K7:

package PageObject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import PageObject.LogOff;
import PageObject.LoginPage;

public class K7 {
WebDriver driver;

private static String login = "user";
private static String pass = "user@123";

@BeforeClass

public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.get("http://localhost/k7biz/static/index.htm");

}

@Test
public void testLogin() {
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
LogOff findPage = loginPage.login(login, pass);
loginPage = findPage.singOff();

}
@AfterClass
public void tearDown() throws Exception {
driver.quit();

}

}


Tuesday 14 May 2013

Take Screenshot and save into specified location using WebDriver


Write the script in the following manner:

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class screenshot {
WebDriver driver;
@BeforeMethod
public void BM()
{
driver=new FirefoxDriver();
driver.get("http://google.com");
}
@Test
public void test()
{
try {
//Take the screenshot
File scrnsht =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Copy screenshot into a specific location
FileUtils.copyFile(scrnsht, new File("C:\\CANVAS\\k7.jpeg"));
}
catch (Exception e)
   {
e.printStackTrace();
}
}
@AfterMethod
public void AM()
{
driver.close();
driver.quit();
}
}

Data driven testing with WebDriver


Download the jxl jar file from http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm
Add the jxl jar file into project

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class dd{

WebDriver driver;
@BeforeMethod
public void setUp()
{
driver = new FirefoxDriver();
}
@Test
public void searchGoogle() throws Exception
{
FileInputStream fi = new FileInputStream("C:\\poi-3.9\\excel.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
for(int row=1; row <=s.getRows();row++)
{
String username = s.getCell(0, row).getContents();
System.out.println("Username "+username);
driver.get("http://fb.com");
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(username);
String password= s.getCell(1, row).getContents();
System.out.println("Password "+password);
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys(password);
driver.findElement(By.xpath(".//*[@id='u_0_b']")).click();
}
}
@AfterMethod
public void tearDown()
{
driver.close();
driver.quit();
}
}


Run all the scripts in a package using TestNG


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="DDTNG" verbose="1" >
  <test name="Regression"   >
    <packages>
      <package name="NG" />
   </packages>
 </test>
</suite>


Suite Name:Give your suite name

Test Name:Give Your test name

Package Name:Give your package name

Execute WebDriver script paralally on multiple browsers using TestNG anntations

Download the IE x86/x64 jar files from https://code.google.com/p/selenium/downloads/list
Give the jar file path in  System.setProperty("webdriver.ie.driver", "C:\\Jarfiles\\IEDriverServer.exe");
Download the Chrome driver from https://code.google.com/p/chromedriver/downloads/list
Give the jar file path in System.setProperty("webdriver.chrome.driver", "C:\\Jarfiles\\chromedriver_win_26.0.1383.0\\chromedriver.exe");

Write the script in the following manner


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
//import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;

public class Browser {

static WebDriver driver;
@BeforeMethod

@Parameters({"browser"})

public void openBroswer(String browser){

if (browser.equalsIgnoreCase("FF"))
{
System.out.println("Firefox webdriver would be used");
    driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("IE"))
{
System.out.println("IE webdriver would be used");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "C:\\Jarfiles\\IEDriverServer.exe");
    driver = new InternetExplorerDriver();
}
else
{
System.setProperty("webdriver.chrome.driver", "C:\\Jarfiles\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
driver = new ChromeDriver();
}
}

@AfterMethod

public void closeBrowser()
{try{
driver.wait(15000);
}
catch(Exception e){}
driver.close();
driver.quit();
}

@Test

public void test() throws Exception{
driver.get("http://gmail.com");
driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("administrator");
driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("desk@123");
driver.findElement(By.xpath(".//*[@id='signIn']")).click();
}
}



Write the XML file as:


<suite name="DDTNG" verbose="1" parallel="tests">

<test name="Generic test" >
<parameter name="browser" value="FF"></parameter>

<classes>
<class name="NG.Browser" />
</classes>
</test>

<test name="Generic test_ie" >
<parameter name="browser" value="IE"></parameter>
<classes>
<class name="NG.Browser" />
</classes>
</test>
</suite>


If u want to run the scripts on multiple browsers in sequential way ->you can use "linear" in place of "parallel" in xml file


Note:You can run the scripts in IE without jar files also.


Monday 13 May 2013

Add Apache-poi jar file into a project in Eclipse and Create Excel sheet



1.Browse the URL as http://poi.apache.org/
2.Click on downoads link
3.Click on The latest stable release is Apache POI 3.9 link
4.Clcik on the poi-bin-3.9-20121203.zip
5.Extract the zip file and Add the poi-3.9-20121203 jar file into a project

Write the below script for Excel sheet creartion


import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class excel {
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
            //Create sheet as 'newsheet'
   Sheet sh1=wb.createSheet("newsheet");
           //Create sheet as '2ndsheet'
   Sheet sh2=wb.createSheet("2ndsheet");
   FileOutputStream fileOut = new FileOutputStream("kalyan.xlsx");
   wb.write(fileOut);
   fileOut.close();
}

}

Please go through the below link for more examples:
http://poi.apache.org/spreadsheet/quick-guide.html

Handle dropdown lists using WebDriver

For Example:Browse: http://apsrtconline.in/oprs-web/

Findout how many vallues in the Concession dropdown list
Select name(APSRTC-RETIRED EMPLOYEE) in the Concession drop-down list

Write the script in the following way:


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class webdriver

{
public WebDriver driver;
 @BeforeMethod
      public void BM()
     {
         //Launch the firefox browser
    driver=new FirefoxDriver();
     }
@Test
     public void Test()
    {
        //Browse the google website
        driver.get("http://apsrtconline.in/oprs-web/");
     
        Select select = new Select(driver.findElement(By.xpath(".//*[@id='concessionId']")));
     
    //finding the number of options
    int i =select.getOptions().size();
   
    //printing the number of options
    System.out.print("number of options ="+i);
   
    // select option in Drop-down using Visible Text
    select.selectByVisibleText("APSRTC-RETIRED EMPLOYEE");
   
   
    }
@AfterMethod

     public void AM()
    {
   //close the firefox window
         // driver.quit();
    }
}

You can select options in the drop-down list based on the index value also:

select.selectByIndex(index value);

simple WebDriver script with TestNG annotations

Configure WebDriver and TestNG on Eclipse

Write the script in the following way:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class webdriver

{
public WebDriver driver;
 @BeforeMethod
      public void BM()
     {
         //Launch the firefox browser
    driver=new FirefoxDriver();
     }
@Test
     public void Test()
    {
        //Browse the google website
        driver.get("http:\\gogle.com");
    }
@AfterMethod

     public void AM()
    {
   //close the firefox window
          driver.quit();
    }
}


Sunday 12 May 2013

WebDriver configuration on eclipse


1.Browse URL as http://docs.seleniumhq.org/download/ and navigate to Selenium Client & WebDriver Language Bindings section
2.Click on Download button adjacent to the java
3.Extract the downloaded zip file and add all the extracted jar files in the lib folder and outside of the lib folder to webdriver project


Saturday 11 May 2013

TestNG Configuration on Eclipse


1.Follow the below steps to install the TestNG plug-in on the eclipse IDE.

2.Open the Install/Update window from Help->Software Updates->Find and Install

3.Click on the radio button "Search for new feature to install" and click "Next" button

4.Click on the "New remote site..." and supply name as "TestNG" and URL as "http://beust.com/eclipse" and click on "OK"

5.Make sure the TestNG is checked and click on the "Finish" Button


If you want to watch the video with more clarity ,Browse this link:http://www.youtube.com/watch?v=GduRcAYGIdo