Using Selenium to test XForms and formsPlayer

Selenium is an automated test tool from OpenQA that provides control over web applications running in a browser. Although it's normally used to test HTML or Ajax sites, we've built a number of hooks into formsPlayer so that Selenium can also be used to test XForms applications. This section explains how to make use of these extensions.

Seeing an example

Before you install Selenium and start creating your own test suite for your XForms application, you might want to try it out. If you have formsPlayer installed already, direct Internet Explorer to the formsPlayer test suite. Next select TestXForms10SubmissionGetReplaceInstance in the left frame, and click Selected in the very right-hand frame. Selenium will start running the test, using the bottom frame to hold the actual XForms document to be tested. After some activity, the right-hand frame should show that 1 test has run, and that 2 commands have passed.

Now that you've seen Selenium in use on the formsPlayer server, we'll look at how you can use it for your own projects.

Installing Selenium

If you're not already using Selenium for other parts of your project, then the first step will be to obtain a copy of Selenium Core. The standard configuration is to place the Selenium files on the same server that is delivering your application, so that there are no cross-domain security problems. Once you have the download, it would be worth testing your set-up by running some of the core Selenium tests.

Obtaining the formsPlayer user extensions

When Selenium runs it looks for a script called user-extensions.js which contains any custom extensions that are needed. This allows you to upgrade Selenium without losing any modifications that might have been made.

You can find the formsPlayer extensions in our Subversion repository:

<http://svn.x-port.net/svn/public/testsuite/selenium/core/scripts/user-extensions.js>

Copy this file to the selenium/core/scripts directory.

Writing tests

Now you should be ready to create some test-cases. A test in Selenium involves a normal HTML document and a 'driver' that describes some steps to take on that document. For example, we might want the test to act as if something was typed into a control, or the mouse clicked on a button.

The files that drive the tests are themselves HTML, and use a simple table layout to define them. We'll leave a full description of how this works to the Selenium documentation, but as a way of getting started quickly, take a look at the 'driver files' that are available from our Subversion repository:

<http://svn.x-port.net/svn/public/testsuite/selenium/tests>

For example, we have:

The naming convention is probably obvious, but these tests are for:

Remember that these files are 'driver' files--i.e., they are the instructions on how to control the test and what to check for, but they do not contain the actual functionality. In the case of these test files, we've kept them separate, and the tests being controlled come from the samples area. For example, samples that show how to use XForms 1.0 submission are here:

<http://svn.x-port.net/svn/public/samples/specifications/xforms/1.0/submission>

The file used in the example at the beginning, which tested submission with replace="instance", is in this directory, and is called submission-get-replace-instance.html.

Creating a test suite

To control a group of tests, a further HTML file is created that contains references to all of the necessary driver files. TestFormsPlayer.html is the file that we saw above, that runs all of the formsPlayer tests (available in the same Subversion directory mentioned above), but other test suites could be created to test narrower ranges of functionality, such as only XForms 1.0 features, or only submission.

Running the tests

To execute the tests we load the core test runner, and provide it with the location of a test suite. For example, to run the formsPlayer test suite directly from the Subversion server (as we saw earlier), we can use the following URL:

<http://svn.x-port.net/svn/public/testsuite/selenium/core/TestRunner.html?test=../tests/TestFormsPlayer.html>

Once Selenium has loaded, a frameset is displayed that contains:

To run all of the tests in the test suite, in one go, select 'All' from the test results panel, otherwise click on 'Selected' if you want to run tests one at a time.