Moving to Selenium 2 on WebDriver, Part No.2

In the following examples the location is always of type org.openqa.selenium.By, while locator is the old Selenium string.

Select a single item in select by label

Selenium 1:
selenium.select(locator,"label=" + optionLabel);

Selenium 2:
Select select = (Select)driver.findElement(location);

Select a single item in select by value

Selenium 1:
selenium.select(location,"value=" + optionValue);

Selenium 2:

Write text to an input field

Selenium 1:
selenium.type(locator, text);

Selenium 2:

Get text content of an element

Selenium 1:

Selenium 2:

Setting a checkbox to 'checked' state

Selenium 1:

Selenium 2:

Javascript in MSIE

When using Javascript (through JavascriptExecutor's executeScript()) you can get unexpected behaviour - MSIE crashes without useful error message when you access some Javascript objects in browser.

To cope with this on Windows 7, check your Protected Mode settings (Tools > Internet Options > Security) and set it to the same value for all zones. I don't know if Windows XP is also affected and how to se it it such case.

Execute Javascript

For all following examples let's define:
JavascriptExecutor js = (JavascriptExecutor) driver;

Selenium 1:

Selenium 2:

Execute Javascript and get result

Selenium 1:
scriptResult = selenium.getEval(scriptReturningString);

Selenium 2:
String scriptResult = (String)js.executeScript(scriptReturningString);

WebDriver requires the script to begin with "return". It's easy to miss it in documentation. Script runs in context of a currently selected window - you do not need any tricks for that as in Selenium 1. Also the result is casted to matching Java type.


Moving to Selenium 2 on WebDriver, Part No.1

I am in the process of trying to move code of some test to the Selenium 2 WebDriver. This is the first part of series of short articles about how to "translate" old Selenium API to the WebDriver-based one.

In following, let's state that xpathLocator is Selenium 1 locator, while xpathFinder is more or less By.xpath(xpathLocator),selenium is instance of Selenium, driver is instance of WebDriver (FirefoxDriver).

Is an element present on a page ?

Selenium 1:

Selenium 2:

Get HTML source of a page

Selenium 1:

Selenium 2:

Is a text present on a page ?

Selenium 1:

Selenium 2:

Go to relative URL
The relativeUrl is URL relative to initial URL, i.e. without the "http://hostname".

Selenium 1:

Selenium 2:
driver.get( initialUrl + relativeUrl );

This call is blocking, i.e. further processing is stopped until the page is loaded.

To open an URL relative to the currently used  one, use  driver.getCurrentUrl():
driver.get( driver.getCurrentUrl() + relativeUrl );

Is an element visible on a page?

Selenium 1:

Selenium 2:
driver.findElement(location) instanceof RenderedWebElement


Firefox WebDriver on Windows Behind HTTP Proxy

Playing with Selenium (2.0a7) I was quite suprised that FirefoxDriver does not recognize HTTP proxy. As I wanted my code to work behind firewall I was pushed to do some "research" on the topic and found working solution using Proxy and FirefoxProfile . I hope publishing it can help somebody with the same problem:
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


Proxy proxy = new Proxy();
//hard-coded: proxy.setProxyAutoconfigUrl("http://pac.your.com");
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver =  new FirefoxDriver (profile);