To achieve full test coverage for our new product sixbee, we chose to try out Selenium. It works perfectly fine for simple stuff like clicking on static links with static texts or IDs:

  1. selenium.clickAndWait("link=Click") // click on a element labeled "Click"
  2. selenium.clickAndWait("btn") // click on element with id "btn"

When it comes to testing components using Ajax, it’s getting trickier, as not all generated elements have an ID to reference it. Fortunately, Selenium offers some more possibilities for locating elements, such as XPath or CSS-selectors:

  1. selenium.clickAndWait("//input[@name='file']") // click on a first input-element with a name attribute "file"
  2. selenium.clickAndWait("input[name='file']:nth-child(1)") // the same

Note that //input[@name='file' does not specify a collection of all elements having their name attribute set to "file" (unlike the XPath specification says), but only the first element. If you would like to refer to the second element, you will have to resort to CSS-selectors.

A great facility to find the XPath to an element is the Firebug plugin. Simply right-click on an element in the HTML view and choose "copy XPath". You will get a rather complicated expression, which can usually be simplified, but it's a good thing to start with. If you want to verify your XPath is correct, switch to the Console view and type something like

  1. $x("//div[@name='content']/p")

and Firebug will show the corresponding element. This safes a lot of time while developing Selenium tests, as you don’t have to run your tests just to see whether your XPath was correct.

Email this Share this on Facebook Share this on LinkedIn Tweet This! RSS feed for comments on this post. TrackBack URL

Leave a comment