Posted on

Selenium IDE Commands

I’ve been using Selenium IDE for a few months now with very simple test scripts.   Mostly scripts I’ve recorded to test my WordPress plugins.   They are not very sophisticated, but as my release schedule has increased so has my need for more robust testing.  Unfortunately the Selenium IDE 2 documentation is sparse at best.   Maybe they have a complete list of commands and how they work, but I can’t find it.

So I decided to create my own Selenium IDE commands list.   This is my cheat sheet and is based on my current knowledge as an end user.  It may not be accurate, so always check what you read here before applying it to a production test suite.

Using Selenium IDE

One of the first lessons I learned is that recording/playing back scripts is a great way to get started.   The second lesson learned seconds later is that the scripts are far from perfect, very inefficient, and always need editing.

Add wait commands, it is key to successful tests.   Always make sure an element is loaded before running full speed through the script.

Edit the first couple of commands to go directly to the test URL.  Often the scripts start elsewhere.

Realize this is not a full programming language.  Out of the box it does NOT support if, goto, or loop constructs.  It is a pure linear command execution.  If you need more you should get an add-on OR use the full Selenium RC line.

Selenium IDE Commands

(that I use)

assertElementPresent

Arguments:

locator – an element locator

Description:

Make sure an element is present, if not fail and abort the remainder of the test.

Notes:

I use this to make sure that a previous test suite or action did what I expected.  If not, something is horribly wrong.   Assert commands tell Selenium IDE “hey, something bad happened… stop testing”.

assertElementPresent

Arguments:

target – a piece of javascript that returns a value

value – what you expect the value to be set to

Description:

Make sure the javascript executes.  The find button will not work here, you must execute the command to test this.

Notes:

This is a recent addition to my Selenium IDE arsenal.  I use it to check custom CSS rules and make sure that when I tweak a CSS rule via my admin panel textarea field that it makes it to the front end.   I check the computed style of a specific element and make sure the attribute is set as I expect.  For example, check that the final style sets a font size to 48px.

Example target:

window.document.defaultView.getComputedStyle(window.document.getElementsByClassName(‘entry-title’)[0]).getPropertyValue(‘font-size’)

Example value:

48px

check

Arguments:

locator – an element locator

Description:

Check a toggle-button (checkbox/radio).

Notes:

This is a great way to force a checkbox on.   The mirror command is uncheck.

  • command: check
  • target: you element finder i.e. name=checkbox_name_property
  • value: n/a (leave blank)

click

Arguments:

locator – an element locator

Description:

Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.

Notes:

A good idea to use waitForElement to make sure your locator is present before doing this.

open

Arguments:

url – the URL to open; may be relative or absolute

Description:

Opens an URL in the test frame. This accepts both relative and absolute URLs. The “open” command waits for the page to load before proceeding, ie. the “AndWait” suffix is implicit. Note: The URL must be on the same domain as the runner HTML due to security restrictions in the browser (Same Origin Policy). If you need to open an URL on another domain, use the Selenium Server to start a new browser session on that domain.

select

Arguments:

selectLocator – an element locator identifying a drop-down menu
optionLocator – an option locator (a label by default)

Description:

Select an option from a drop-down using an option locator.

Notes:

The easiest way to select an element on a pulldown list is:

    • command: select
    • target: id=<input id>
    • value: label=<text string of item to select>

Here are notes on the selectors from Selenium IDE:

Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.

    • label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
    • label=regexp:^[Oo]ther
    • value=valuePattern: matches options based on their values.
    • value=other
    • id=id: matches options based on their ids.
    • id=option1
    • index=index: matches an option based on its index (offset from zero).
    • index=2

If no option locator prefix is provided, the default behaviour is to match on label.

type

Arguments:

locator – an element locator

value – the value to type

Description:

Can also be used to set the value of combo boxes, check boxes, etc. In these cases, value should be the value of the option selected, not the visible text.

Notes:

This is what I use to “type” data into an input field.

uncheck

Arguments:

locator – an element locator

Description:

Uncheck a toggle-button (checkbox/radio).

Notes:

This is a great way to force a checkbox off.   The mirror command is check.

  • command: uncheck
  • target: you element finder i.e. name=checkbox_name_property
  • value: n/a (leave blank)

waitForElementPresent

Arguments:

locator – an element locator

Returns:

true if the element is present, false otherwise

Description:

Verifies that the specified element is somewhere on the page.

Notes:

I try to use this before I click on anything, enter input values, etc.  Often an element is not loaded yet.  This waits until it is (or fails).

Checking if a hyperlink that contains specific text is a good way to manage the AJAX operations of WordPress categories.    You can use the following command set:

command: waitForElementPresent
Target: xpath=//a[text()='Category Name']

My Script(s)

I  have my Store Locator Plus testing scripts on a public GitHub repo for anyone that wants to see them.

Enjoy & please share your Selenium IDE wisdom here.  We can all use help when it comes to using this tool.