Pages

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 4 March 2012

How can I use/emulate Selenium RC with WebDriver?

The Java version of WebDriver provides an implementation of the Selenium-RC API. It allows those who have existing test suites using the Selenium-RC API to use WebDriver under the covers.


Example:


// 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).getWrappedDriver();

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

How can I check the presence of WebElement on page?

We can check the presence of WebElement on page by implementing custom isElementPresent method as below:


	protected boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

This method will return true, if the WebElement with ‘by’ available otherwise returns false.

Tuesday, 7 February 2012

How can I instantiate InternetExplorerDriver?

The InternetExplorerDriver can be instantiated as below:


WebDriver driver = new InternetExplorerDriver(); // Instantiate the class - org.openqa.selenium.ie.InternetExplorerDriver

Sample code(Java Binding):


package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class InternetExplorerDriverSample {
public static void main(String[] args) {
// Instantiate the InternetExplorerDriver
WebDriver driver = new InternetExplorerDriver();

// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");

// Print the title of the page - It should print "Selenium-WebDriver FAQ's"
System.out.println("Title of the page is: " + driver.getTitle());

//Close the browser window
driver.close();

//Quit the driver
driver.quit();

}
}

Monday, 6 February 2012

How can I instantiate FirefoxDriver?

The FirefoxDriver can be instantiated as below:


WebDriver driver = new FirefoxDriver(); // Instantiate the class - org.openqa.selenium.firefox.FirefoxDriver

Sample code(Java Binding):


package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxDriverSample {
public static void main(String[] args) {
// Instantiate the FirefoxDriver
WebDriver driver = new FirefoxDriver();

// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");

// Print the title of the page - It should print "Selenium-WebDriver FAQ's"
System.out.println("Title of the page is: " + driver.getTitle());

//Close the browser window
driver.close();

//Quit the driver
driver.quit();
}
}

How can I instantiate HTMLUnitDriver?

The HTMLUnitDriver can be instantiated as below:


WebDriver driver = new HtmlUnitDriver(); // Instantiate the class - org.openqa.selenium.htmlunit.HtmlUnitDriver

Sample code:


package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HTMLUnitDriverSample {
public static void main(String[] args) {
// Instantiate the HTMLUnitDriver
WebDriver driver = new HtmlUnitDriver();

// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");

// Print the title of the page - It should print"Selenium-WebDriver FAQ's"
System.out.println("Title of the page is: " + driver.getTitle());

}
}