Selenium (by Thoughtworks) is on open source tool for automated functional tests. It’s simplicity makes it an excellent candidate for introducing automated functional testing in your project. (Hi! This article is now several years old and updates may have changed how Selenium works)
Setting it up for an ASP.NET application is done in a few simple steps:
- Download Selenium (choose the full install).
- Create a folder “selenium” in your web site root folder.
- Unpack the zip file and move the selenium files to to the selenium folder.
That’s it! Selenium is now installed. You can test the installation by running the Selenium self-test by pointing your browser to http://localhost/selenium/TestRunner.html. Selenium runs in your browser of choice and there is nothing more to install.
Creating your own tests
Getting started with your own test suite is easy. A test suite consists of one or more test cases. A test case consists of a series of commands and verification points which you specify in a table in an HTML file. A sample test case looks like this:
<table> <tbody> <tr> <td colspan="3">Test search query</td> </tr> <tr> <td>open</td> <td>/default.aspx</td> <td></td> </tr> <tr> <td>verifyTextPresent</td> <td>Welcome to the sample search application</td> <td></td> </tr> <tr> <td>type</td> <td>StartpageFramework1_QuickSearch1_txtSearchPhrase</td> <td>ultramarine</td> </tr> <tr> <td>clickAndWait</td> <td>StartpageFramework1_QuickSearch1_btnSearch</td> <td></td> </tr> <tr> <td>verifyTextPresent</td> <td>No match found</td> <td></td> </tr> </tbody> </table>
It should be fairly obvious what this test case does; the page default.aspx is opened in the browser, the text “Welcome to the sample search application” is identified, “ultramarine” is typed into the search box field, the search button is clicked and then we verify that the text “No match found” was displayed.
As you can see, the type command needs a way to locate which field to type the text into. The easiest way is to use the id of the HTML element (there are other ways to locate an element). If you use runat=server controls ASP.NET will have generated the HTML element id for you and depending on it’s position it may look something like StartpageFramework1_QuickSearch1_txtSearchPhrase. Unfortunately the id will change if you move the search field to a different container. This would force us to update the test case every time we did minor changes to the page.
Thus, a better way to identify fields and buttons is to use an XPATH expression:
This will make the test case independent of control hierarchy changes.
After you have saved the test case to a file (name it “searchtest.htm” and put it in the selenium directory for now) you can create a test suite where we point out one or more test cases. A test suite is also described in an HTML table like this:
|My test suite|
|Search function test|
Each test case in the suite is identified with a link in a single table row. Save the test suite (name it “searchsuite.htm” and put it in the selenium directory).
Run your tests
Now it is time to test your application. To open Selenium with your test suite, point your browser to http://localhost/selenium/TestRunner.html?test=searchsuite.htm and start it. You can follow the progress as the test is carried out.
You can also combine Selenium with NUnit to get a complete testing environment. Instead of running test cases in the browser it is possible to install the .NET test driver which gives you control of the browser process via .NET code.