Monday, November 1, 2010

Selenium Overview

Selenium Overview:-

Selenium is a suite of tools to automate web app testing across many platforms. It is a GUI based automation tool. Initially it is built by ThoughtWorks. It supports various browsers on various platforms

Selenium Projects
Selenium has many projects. Following projects are mostly used by testers.

  1. Selenium IDE (IDE)
    Selenium IDE can be used only in FireFox. It is an add-on for FireFox. User can record the actions and can edit and debug the tests. It can be used to identify IDs, name and XPath of objects. Only one test at a time.
  2. Selenium Core (CORE)
    Selenium Core is the original Javascript-based testing system. This technique should work with any JavaScript enabled browser. It is the engine of both, Selenium IDE and Selenium RC (driven mode), but it also can be deployed on the desired application server. It is used specifically for the acceptance testing.
    User can record the tests using Selenium IDE and can use the same tests to run in other browsers with minimal modifications. It provides support to run the tests in HTA (HTML Applications) Mode. This mode works only in IE.
  3. Selenium Remote Control (RC)
    Selenium Remote Control is a test tool that allows user to write automated web application UI tests in few programming languages against any HTTP website using any mainstream JavaScript-enabled browser. User can write the tests (More expressive programming language than the Selenese HTML table format) in Java, DotNet, Perl, Ruby and PHP. Also it supports few testing frameworks.
  4. Selenium Grid
    Selenium Grid allows easily to run multiple tests in parallel, on multiple machines, in an
    heterogeneous environment by cutting down the time required for test execution. Using this, user can run multiple instances of Selenium Remote Control in parallel.

Supported browsers
Selenium tools can run in following browsers.

  • Internet Explorer
  • FireFox
  • Opera
  • Safari
  • Seamonkey

Supported Operating Systems
Users can execute the selenium tests in following OS.

  • Windows
  • Linux
  • Solaris
  • OS X

Supported Programming languages
Below languages are supported by Selenium RC.

  • C# (DotNet)
  • Java
  • Perl
  • Ruby
  • Python
  • PHP

Sample Code

Following program tells how to launch a browser and to play around with text,list,combobox objects we find normally in any website

package Orkut;
import com.thoughtworks.selenium.*;
import com.thoughtworks.selenium.DefaultSelenium;
public class MySelenium {
static Selenium selenium;
static boolean val,val1;
public static void main(String arg[]) throws Exception
{
selenium = new DefaultSelenium("localhost",
4444, "*iexplore", "http://www.ge.com");
selenium.start();
Thread.sleep(4000);
selenium.windowMaximize();
selenium.windowFocus();
selenium.open("/index.html");
selenium.click("//div[@id='ge_footer']/ul/li[2]/a");
val=selenium.isElementPresent("//img[@alt='GE: imagination at work']");
for (int second = 0;second<=10; second++) {
val=selenium.isElementPresent("//img[@alt='GE: imagination at work']");
if (val)
{
System.out.println(val);
break;
}
else
{
if (second==10)
{
System.out.println("Page not found :=GE: imagination at work");
selenium.stop();
}
Thread.sleep(4000);
}
}
selenium.click("//ul[@id='ge_secondaryNav']/li[2]/a");
val1=selenium.isElementPresent("//div[@id='secondary_content']/div/h3");
for (int second = 0;second<=10; second++) {
val1=selenium.isElementPresent("//div[@id='secondary_content']/div/h3");
if (val1)
{
System.out.println(val1);
break;
}
else
{
if (second==10)
{
System.out.println("Element not found :=GE: Other Contact Resources");
selenium.stop();
}
Thread.sleep(4000);
}
}
selenium.click("//input[@id='contact_type_press']");
selenium.select("//select[@id='contact_subject']", "label=Other");
selenium.select("//select[@id='contact_country']", "label=Algeria");
selenium.type("//input[@id='contact_email']", "abc@vvv.com");
selenium.type("//textarea[@id='contact_comments']", "testing");
//selenium.click("//form[@id='contact_form']/p/input");
}
}

Thursday, October 28, 2010

Extending Selenium server with custom commands

In this posting I document extending Selenium server with a custom javascript method, getAllRadios(), to return all radio button elements in a page. It’s based on the getAllButtons() that is stock in Selenium.

I will demonstrate two ways to add this custom method. The first strategy is to add the custom method to the native javascript files that ship with Selenium server. This is not the recommended way to extend Selenium but it is an almost guaranteed way to be sure the server picks up my new method and that frees me to focus on getting my client syntax right for calling the new method using the client’s doCommand() method.

Method 1 – selenium-browserbot.js + selenium-api.js

Unpack the server jar into a temporary directory to get access to the selenium-browserbot.js and selenium-api.js files.

[20:54 20090906 crashing@server /selenium-server-1.0.1/temp]
$ jar xf ../selenium-server.jar

The new custom method is added to core/scripts/selenium-browserbot.js. It is a slightly modified version of getAllButtons() which is defined in the same file.

BrowserBot.prototype.getAllRadios = function() {
var elements = this.getDocument().getElementsByTagName('input');
var result = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].type == 'radio') {
result.push(elements[i].id);
}
}
return result;
};

and then the new method is exposed to the Selenium API in core/scripts/selenium-api.js, again modeling after getAllButtons() in selenium-api.js.

Selenium.prototype.getAllRadios = function() {
return this.browserbot.getAllButtons();
};

Now rebundle the updated javascript into a new server jar, replacing the original.

[21:01 20090906 crashing@server /selenium-server-1.0.1/temp]
$ jar cmf META-INF/MANIFEST.MF ../selenium-server.jar .

Method 2 – user-extension.js

The better way to extend Selenium is to add methods to the user-extensions.js file. This avoids screwing around with the native server jar. I initiall had some trouble getting this working, hence the Method 1 approach, but after a bit of futzing I finally got this to work.

Selenium.prototype.getAllRadios = function() {
var elements = this.browserbot.getDocument().getElementsByTagName('input');
var result = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].type == 'radio') {
result.push(elements[i].id);
}
}
return result;
};

This is basically the same method used in selenium-browserbot.js. The important changed bits are (1) the method is attached to the Selenium object instead of the BrowserBot (Selenium.prototype rather than BrowserBot.prototype) and (2) calling getDocument() on the browserbot instance.

I start the Selenium server with the -userExtensions option pointing to the user-extensions.js file.

java -jar \
/selenium-server-1.0.1/selenium-server.jar \
-userExtensions /user-extensions.js

Client Coding

For either of the above methods of extending the Selenium server, I call this new method in my client code with the doCommand().

/** incomplete code **/

proc = new HttpCommandProcessor(seleniumServerHost,
seleniumServerPort, brower, seleniumServerStartUrl);
selenium = new DefaultSelenium(proc);

selenium.open(url);

// radio buttons returned as comma-delimited strings
String allRadios = proc.doCommand("getAllRadios", null);

// alternatively, get radio buttons as a String array
String[] radios = proc.getStringArray("getAllRadios", null);
for (String radio : radios) {
System.out.println(radio);
}

To call custom doCommand()‘s, it’s necessary to instantiate with DefaultSelenium(proc) to inject the HttpCommandProcessor into the DefaultSelenium object.

Tuesday, October 26, 2010

Use regex in Selenium locators

To use regex in Xpath locators.

You need to use the regex matches() function, like this:

xpath=//div[matches(@id,'che.*boxes')]



this will click the div with 'id=checkboxes', or 'id=cheANYTHINGHEREboxes')
To get all the links elements with attribute href that match:
http://[^/]*\d+com

Use
sel.get_attribute( '//a[regx:match(@href,
"http://[^/]*\d+.com")]/@name'
)
which will
return a list of the name attribute of
all the links that match the regex.

Be aware, that the matches function is not supported by all
native browser implementations of Xpath
(most conspicuously, using this in FF3 will throw an error: invalid xpath[2]).
If you have trouble with your particular browser (as I did with FF3), try using Selenium's allowNativeXpath("false") to switch over to the JavaScript Xpath interpreter. It'll be slower,
but it does seem to work with more Xpath functions, including 'matches' and 'ends-with'.