Thursday, July 28, 2011

WebDriver First look

WebDriver has been for a while but never get a chance to look at it. finally today I downloaded the webdriver and executed the simple testcase. Let's see how to configure webdriver for java.

First download the selenium-java-2.2.0.zip (version may differ) from http://code.google.com/p/selenium/downloads/list

The first thing you will notice when you will extract this zip is, too much JAR files, too much as compared to only two in selenium RC.

Now create the simple java project in eclipse. Put all the extracted JAR files in the buildpath of the project.

Copy and paste the following code and run using Junit. (you don't require to run any selenium server)

package Tests;

import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
 * 
 * @author Gaurang
 *
 */
public class GoogleTest  {

 public static WebDriver driver;
 @BeforeClass
 public static void setup(){
 // Create a new instance of the Firefox driver
        driver = new FirefoxDriver();
        
        // And now use this to visit Google
        driver.get("http://www.google.com");
 }
 
 @AfterClass
 public static void tearDown(){
        //Close the browser
        driver.quit();

 }
 @Test
 public void testSearch() throws InterruptedException {
        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));
        // Enter something to search for
        element.sendKeys("Cheese!");
        //driver.findElement(By.name("btnG")).click();
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();
        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        Thread.sleep(3000);
        assertTrue(driver.getTitle().contains("cheese!"));
  }
}

To run the webdriver testcase in Internet Explorer
replace the driver = new FirefoxDriver(); line from the setup() method with following line.
driver = new InternetExplorerDriver();

To run the webdriver testcase in Opera
replace the driver = new FirefoxDriver(); line from the setup() method with following line.
driver = new OperaDriver();

To run the Webdriver testcase in Chrome
Download the chrome driver form the below URL
Download Chrome driver

Now extract the zip file and set the path of executable in PATH variable.

replace the driver = new FirefoxDriver(); line from the setup() method with following line.

driver = new ChromeDriver();

1 comments:

Rakesh said...

theres nothing about htmlunit and google chrome driver. i think those two are important drivers o webdriver

Post a Comment