Skip to: Site menu | Main content

Selenium Test Tips

Selenium is an open-source test tool for web-based applications and sites. Unlike Canoo WebTest, Selenium actually utilizes the browser itself to perform the tests.

Selenium has a Firefox extension that acts as an IDE for recording and editing test cases however there is a significant difference between running the tests in the IDE and running them as part of a build process — cross-site scripting.

When running tests in the IDE, Selenium is not limited by Firefox cross-site scripting preventative measures and can follow links to external sites (outside your ‘base URL’) and verify that the target has been reached. This is not possible when running it as part of a build process in stand-alone mode.

The reason for this difference is that when running Selenium in stand-alone mode it generates a web-page that contains the test runtime with the actual web-page it is testing residing within an IFRAME. Once you traverse to links outside the domain the Selenium generated web-page is at (usually localhost) then you are subject to cross-site scripting limitations. Put it simply, your top page (Selenium generated) would be in the ‘localhost’ domain, and the external page is at another domain thus triggering the cross-site scripting security restrictions.

I’ll demonstrate what I mean:

In Selenium, a typical link test is done so:

  1. A ‘click’ Selenium command with the ‘link=’ target.
  2. Then a ‘waitForPageToLoad’ command
  3. Followed by an ‘assertLocation’ with the correct expected URL

This test will fail when running in the standalone mode with the link target being external to the domain running the Selenium web-page. Instead of what you expect from the IDE based run you will hit upon permission errors accessing “document.href” and other DOM properties. This is simply because your script is from domain “localhost” (for example) and does not have access to the DOM of the page from the external domain (say “rationalistic.com”)… Note that for the same reason you can’t even call ‘goBack’ after the ‘click’.

An alternative to this method that is useful for external links is simply to verify the existence and ‘href’ attribute of the link. While this is not ideal (as it doesn’t take into account Javascript method links) it is, in my opinion enough verification for off-site links. To use this method of testing a link’s existence and target you must give the <a> element an id, preferably one that starts with “link_” and then using the Selenium ‘assertElementPresent’ and ‘assertAttribute’ commands.

For example, consider a link to my blog site:

<a id=”link_rationalistic” href=”http://www.rationalistic.com”>The Rationalist Manifesto</a>

To test it in Selenium you would do this:

  1. Use the ‘assertElementPresent’ command with a target of ‘link_rationalistic’
  2. Follow with an ‘assertAttribute’ command with a target of ‘link_rationalistic@href’ and a value of ‘http://www.rationalistic.com’.

This testing method provides reasonable confidence that the link exists and leads to the correct location without following it.

Note:
This method is also useful for links which have symbol characters in them which may be difficult to locate using the ‘link=’ targets.

Leave a Reply