2011/03/26

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.selectByVisibleText(optionLabel);

Select a single item in select by value

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

Selenium 2:
select.selectByValue(optionValue);

Write text to an input field

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

Selenium 2:
driver.findElement(location).sendkeys(text);

Get text content of an element

Selenium 1:
selenium.getText(locator);

Selenium 2:
driver.findElement(location).getText();

Setting a checkbox to 'checked' state

Selenium 1:
selenium.check(locator);

Selenium 2:
driver.findElement(location).setSelected();


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.runScript(script);

Selenium 2:
js.executeScript(script);

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.

2011/03/15

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.isElementPresent(xpathLocator);

Selenium 2:
driver.findElements(xpathFinder).size()>0;

Get HTML source of a page

Selenium 1:
selenium.getHtmlSource();

Selenium 2:
driver.getPageSource();

Is a text present on a page ?

Selenium 1:
selenium.isTextPresent(text);

Selenium 2:
driver.getPageSource().contains(text);

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

Selenium 1:
selenium.open(relativeUrl);

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.isVisible(location)

Selenium 2:
driver.findElement(location) instanceof RenderedWebElement
((RenderedWebElement)driver.findElement(location)).isDisplayed()

2011/03/03

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();
proxy.setAutodetect(true);
//hard-coded: proxy.setProxyAutoconfigUrl("http://pac.your.com");
FirefoxProfile profile = new FirefoxProfile();
profile.setProxyPreferences(proxy);
WebDriver driver =  new FirefoxDriver (profile);