Build Functional Test-Suites with Selenium

I am currently writing lots of Selenium tests for a website with lots of form based enquiries. On my way I came accross some questions and obstacles to be overcome. This is a wrap-up of stuff I discovered:

Tools you need:

Links you might check out for background information and examples:

I did it this way:

  • Use Selenium IDE to record my tests roughly.
  • Install/upload Selenium Core onto the target system (this is necessary since Selenium Core is based on JavaScript and thus must be loaded from the same domain – keyword: XXS security in browsers, it would otherwise prevent access of Selenium Core to the page content) .
  • I organized my Tests in a directory structure like Testarea/Tests.html.
  • I manually updated all single tests using xpath-syntax to check/select items or click buttons or links. As default Selenium IDE took id-attributes which are in my case generated at random (dont ask me why) and did not work on repeated tests when pages were reloaded. Doing this the Web Developer-toolbar is very helpful. Click the button ‘Forms’ and select ‘Display form details…’ and you see additional information right on your page under test, which helps you do the xpath stuff. Here are some examples:
    • Push a submit button
      • Command: clickAndWait
      • Target: //input[@type='submit' and @name='send']
      • Value:
    • Make a selection on a dropdown list:
      • Command: select
      • Target: //select[@name='item9']
      • Value: value=1
    • Check a radio button:
      • Command: check
      • Target: //input[@type='radio' and @name='field3' and @value='1']
      • Value:
    • Click a link:
      • Command: clickAndWait
      • Target: //a[@title='Link zur Seite Erholungsstrategie']
      • Value:
    • Enter a value into an inputbox:
      • Command: type
      • Target: //input[@type='text' and @name='age']
      • Value: 32
    • After you opened and closed a popup, return to main window:
      • Command: selectWindow
      • Target: null
      • Value:

Organize all tests in a test-suite and execute them with Selenium Core:

  • Build a file like this one here with relative links to your test files: TestSuite.html
  • To execute a test-suite open Selenium Core in a Browser, enter the path to yourtest-suite in the upper left frame. A list of all your tests in the test-suite should appear. You are ready for executing them now.

My personal tips:

  • Take a look at the tab ‘Reference’ and play around with different commands.
  • When rewriting xpath expressions, occasionally click on the find-button next to it. It will give you an error or mark a found target with a green frame on the page for a second.
  • If you execute tests on a slow server it sometimes happens that an xpath coordinate will not find its target (since the page has not yet been completely loaded). Try to execute your test step by step at a slower pace.
  • In my case of testing enquiry forms I had to test several succeeding, session-based forms with error alerts on false entries etc. I had to delete session cookies very often via the Web Developer-toolbar (Cookies/Clear Session Cookies).

Further things to try:

  • Get Selenium RemoteControl, install and run it on your server and generate PHPUnit test code with Selenium IDE from your existing tests. As far as I know, this way you can even test for compatibility of different browsers and automate these tests for your builds.