- Start by Downloading the latest binaries and unpack them into a directory. From now on, we’ll refer to that as $SELENIUM_HOME.
- Now, open your favorite IDE , Lets assume your favorite language is java. Hence you should use Eclipse or Netbeans as the IDE. Both are free btw
- Start a new project in your favourite IDE/editor
- Add a reference to all the libraries in $SELENIUM_HOME
- Note : You can see that WebDriver/Selenium2.0 acts just as a normal library does: it’s entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it, as opposed to the proxy server with Selenium-RC.
- You’re now ready to write some code. Lets see the following example
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// 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());
//Close the browser
driver.quit();
}
}
The above test would run on a firefox browser since we are using the firefox driver. Hence you would need firefox installed in your machine.
In case you’d like to run the same test in other browsers you can use the Internet Explorer Driver or Chrome Driver or HtmlUnit Driver.
HTMLunit driver does not run against any browser but is meant for speedier testing. We’ll be looking at them later.
Now the code above uses the webdriver apis. Now if we wanted to use a selenium instance , we can create a webdriver backed selenium instance as shown below.
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
As mentioned in the website the pros and cons of using webdriver backed selenium are
Pros
- Allows for the WebDriver and Selenium APIs to live side-by-side
- Provides a simple mechanism for a managed migration from the Selenium RC API to WebDriver’s
- Does not require the standalone Selenium RC server to be run
Cons
- Does not implement every method
- More advanced Selenium usage (using “browserbot” or other built-in JavaScript methods from Selenium Core) may not work
- Some methods may be slower due to underlying implementation differences
As we learn more we’ll try to come up with how better to use these.
String baseUrl = "http://www.google.com";