Showing posts with label webdriver. Show all posts
Showing posts with label webdriver. Show all posts

Monday, 8 April 2013

Writing simple Data Driven Framework scripts using TestNG

Purpose: Writing simple Selenium Scripts by implementing Data Driven Framework using TestNG

Data Driven Framework scripts means executing a set of steps with multiple sets of data. Selenium is not have any default way of implementing but by using TestNG support we can implement it successfully.

Step 1: JXL API Jar - We can download it form the following URL
http://sourceforge.net/projects/jxls/files/ and download latest version

Step 2: Prepare Data Source - Open an excel and prepare data source as shown below


In this example we are discussing reading inputs from an excel sheet and writing the results in reports sheet.

Step 3: Following example will demonstrate how to access from an excel sheet and how to write the results into an excel sheet.

package com.google.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GMail{
 private WebDriver driver;
 private String baseURL;
 private FileInputStream fi;
 private Sheet s;
 private Workbook wb;

 @BeforeClass
 public void setup() throws BiffException, IOException
 {
  driver = new FirefoxDriver();
  baseURL = "http://www.gmail.com";
  fi = new FileInputStream("TestDate\\Book1.xls");
  wb = Workbook.getWorkbook(fi);
  s = wb.getSheet(0);
 }

 @AfterClass
 public void teardown()
 {
  driver.quit();
 }
 @Test
 public void testGmail() throws InterruptedException
 {
  for(int row=0; row <s.getRows();row++)
  {
    String username = s.getCell(0, row).getContents();
    System.out.println("Username "+username);
    driver.get(baseURL);
    driver.findElement(By.name("Email")).sendKeys(username);
    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);
    driver.findElement(By.name("Passwd")).sendKeys(password);
    Thread.sleep(10000);
    Thread.sleep(30000);
    writeExcel(0, 0, "Passes");
  }
 }

public void writeExcel(int a, int b, String text) {
    try {
        File excelFile = new File("TestDate\\Results.xls");
        WritableWorkbook book;
        WritableSheet sheet;
        Workbook existingBook = null;
        if (!excelFile.exists()) {
            book = Workbook.createWorkbook(excelFile);
            sheet = book.createSheet("TESTRESULTS", 0);
        } else {
            existingBook = Workbook.getWorkbook(excelFile);
            book = Workbook.createWorkbook(excelFile, existingBook);
            sheet = book.getSheet("TESTRESULTS");
        }
        Label i = new Label(a, b, text);
        sheet.addCell(i);
        book.write();
        book.close();
        if (existingBook != null)
            existingBook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}



















 

How to Run Webdriver Script under Internet Explorer and Google Chrome

Purpose: How to Run Webdriver Script for Internet Explorer and Google Chrome

Running Webdriver script for Internet Explorer & Chrome is not as same as working with Firefox browser. Following are the steps to Run:

Step 1: In order to run, we need to download IE Driver & Chrome Driver and we can download from https://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_Win32_2.31.0.zip
https://code.google.com/p/chromedriver/downloads/list

Step 2: Include the respective drivers in to the Path

Step 3: Set the System property using the following Command which takes two parameters Driver and location of the driver.

System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");

Step 4: Now create an Driver Object for IE using the following command

driver = new InternetExplorerDriver();

Once we start running the script Internet Explorer will open and execute script.

Following is the sample code for IE and kept commented code for Google Chrome

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class DriversExample{
private static InternetExplorerDriver driver;
//private static ChromeDriver driver;
@BeforeClass
 public void beforeClass()
 {
  System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
  driver = new InternetExplorerDriver();
//  System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
//  driver = new ChromeDriver();
  }
@Test
 public void testDrivers() throws Exception
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
@AfterClass
 public void afterClass()
 {
  driver.close();
 }
}

Tuesday, 19 March 2013

Maximizing Browser Window using Selenium Rc and Web Driver

Purpose: Maximizing Browser window using Selenium RC and Web Driver

When ever we are opening Web Browser using Selenium RC and Web Driver, it will open in Resized state. But that will not give complete view of the Browser conten. In order to maximize the the window we need to use two different methods in RC and Web Driver.

Maximizing the Browser Window using RC

Once you started the Browser use the following Selenium RC command to maximize the Browser Window
(browser is the object of my DefaultSelenium Class)

browser.windowMaximize();

Maximizing the Browser Window using Web Driver:

It is not as same as RC in Web Driver. We need to use the following command

( driver is the Object of Respective Browser Driver Class )

driver.manage().window().maximize();
 

Unsupported Major/Minor Version issue while executing Selenium Scripts / Java Programs using Eclipse IDE

Purpose: When ever we are executing Selenium Scripts / Java Program which is developed / written in some othere version than our version we will get the error "Java.lang.UnsupportedClassVersionError: .............................Unsupported major.minor version"

If you are executing the program using Console / Command Prompt, using additional parameter -source <version number>

For example, if your file name is VersionError.java and executing through Command Prompt use the following command to execute successfully:

javac -source 1.4 VersionError.java

The program will compile successfully.

If you are executing the code using Eclipse IDE follow the steps mentioned below:

Step 1: Click on 'Project' menu option and select 'Properties' submenu.



Step 2: Click on Project Facts from RNP and Select Java in RNP




Step 3: Select required version of Java and click on OK.

Now Run the application and the issue will be resolved.

Monday, 25 February 2013

Switching between Windows using Selenium Webdriver

Purpose: Switching between Windows using Selenium Webdriver

We come across to handle switching b/w windows while accessing different webpages. Inroder to automate switching b/w windows, following steps will help you to write Selenium Webdriver Script with the help of Java

It is not as easy as handling with Selenium Rc. We can achive with the help of getWindowHandle() and switchTo().window() methods.

Following is the Selenium Webdriver with Java script to handle popup windows:


package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwitchWindow {

 public static void main(String[] args) throws Exception
 {
  FirefoxDriver driver = new FirefoxDriver();
  driver.get("http://www.quackit.com/html/codes/");
  // Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
  String parentWindow = driver.getWindowHandle().toString();
  driver.findElement(By.linkText("Pop up windows")).click();
  Thread.sleep(3000);
  assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
    driver.findElement(By.linkText("Open a popup window")).click();
    // to switch control to the new popup window by name
    driver.switchTo().window("popUpWindow");
    assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
    // closing the popup window
  driver.close();
  // to switch control from popup window to another window, here parent window
  driver.switchTo().window(parentWindow);
  assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
  driver.close();
 }
}

// Code for TestNG

package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class SwitchWindow {
 FirefoxDriver driver;
  @Test
  public void testSwitchWindow() {
   driver.get("http://www.quackit.com/html/codes/");
   // Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
   String parentWindow = driver.getWindowHandle();
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*HTML Codes - Free[\\s\\S]*$"));
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Pop up windows[\\s\\S]*$"));
   driver.findElement(By.linkText("Pop up windows")).click();
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
   driver.findElement(By.linkText("Open a popup window")).click();
   // to switch control to the new popup window by name
   driver.switchTo().window("popUpWindow");
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
   // closing the popup window
   driver.close();
   // to switch control from popup window to another window, here parent window
   driver.switchTo().window(parentWindow);
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
  }
  @BeforeClass
  public void beforeClass() {
   driver = new FirefoxDriver();
   driver.manage().window().maximize();
  }
  @AfterClass
  public void afterClass() {
   driver.close();
  }
}