Selenium

From Genecats
Revision as of 23:10, 12 January 2012 by Greg (talk | contribs) (→‎The Basics: added link to selenium ide reference)
Jump to navigationJump to search


This is mostly a place holder while we develop more content. Here are some useful links...

The Basics

Setting up Selenium IDE

Selenium IDE is a Firefox (only) plugin to help assist in creating basic Selenium test via a GUI.

1. Download/install Selenium IDE for Firefox

2. Open Selenium IDE from Firefox under the Tools pulldown menu

3. Once IDE is open, go to: Options > Options... > Locator Builders and reorder them to (from top down): id, name, link, css, ui, ...then everything else...

Ordering them like this makes for faster running tests, as finding unique elements (id, name, link) is faster (and easier to read) than hunting for them using another method. The fallback is using css selectors, as css selectors have proven to be faster than xPath.

There are some ok introduction to Selenium IDE videos on YouTube.

Setting up Selenium Server

First, install the Selenium Server .jar file on the main "controller" machine and all the remote test machines, the "nodes".

1. Start up the server on the main machine using that machines ip address and port 4448:

java -jar selenium-server-standalone-2.16.1.jar -role hub -hub <static ip address> -port <port number>

2. Start up each of the "nodes" by logging into each machine (can be remotely) and using this command:

java -jar selenium-server-standalone-2.16.1.jar -role node -hub <ip hub>/grid/register

Note, the version of Selenium Server may be different.

Element Locators

Your main locator will be an element's id (they do have id's, right?). There are many others described in the Selenium Docs.

One common way to find elements on a page using Selenium is xPath, however, we should use CSS selectors when possible, as the tests run faster - especially in IE. See: Why CSS Locators are the way to go vs XPath

The best way to find elements is by using their unique ids, in the form of an 'id' or 'name' html attribute, or a specific link. When an element does not have any unique id attached to it, the best fallback is using css selectors. See: Selenium Tips: CSS Selectors in Selenium Demystified.

A really nice tool for creating more complex CSS Selectors (when needed) is Selector Gadget.

Testing dynamic applications (AJAX)

The main things you'll need to test AJAX calls and other dynamic functions are the waitFor... commands. Here are a few of the most useful ones:

waitForCondition(script, timeout)

This is the AJAX command - well almost; also see waitForElementPresent below.

Arguments:

  • script - the JavaScript snippet to run
  • timeout - a timeout in milliseconds, after which this command will return with an error

Runs the specified JavaScript snippet repeatedly until it evaluates to "true". The snippet may have multiple lines, but only the result of the last line will be considered.

Note that, by default, the snippet will be run in the runner's test window, not in the window of your application. To get the window of your application, you can use the JavaScript snippet selenium.browserbot.getCurrentWindow(), and then run your JavaScript in there.

Examples.

waitForElementPresent(locator)

The other AJAX biggie. Similar to waitForCondition. This waits for a certain element to appear (i.e., often an element being loaded via an ajax call).

Generated from isElementPresent(locator)

Arguments:

  • locator - an element locator

Returns: true if the element is present, false otherwise

Verifies that the specified element is somewhere on the page.


waitForVisible(locator)

Also note it's evil twin, waitForNotVisible(locator). These are good when an element is on a page but hidden (i.e., a popup) and your test requires you to wait for it to become visible before clicking on some element inside it.

Arguments:

  • locator - an element locator

Returns: true if the specified element is visible, false otherwise

Determines if the specified element is visible. An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors. This method will fail if the element is not present.

Example:

waitForVisible(id=hgTrackUiDialog)

Wait for the UiDialog (which has an id=hgTrackUiDialog) to appear so the test can continue and click on elements in this dialog.

Selenium Resources

General Automated Testing Resources