in .NET, General, Testing

Using Selenium for automated functional testing of ASP.NET applications

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:

  1. Download Selenium (choose the full install).
  2. Create a folder “selenium” in your web site root folder.
  3. 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:

<td colspan="3">Test search query</td>
<td>Welcome to the sample search application</td>
<td>No match found</td>

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:

//input[contains(@id, "txtSearchPhrase")]

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.

Next step

All in all Selenium is a quick way to get started with automated functional testing. Apart from opening URL:s and verifying text fragments Selenium is also capable of handling javascript dialogs, variables, continuous testing/reporting and many other features. For more details see the Selenium web site.

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.

More information

Write a Comment here on the real web



  1. Great intro! It convinced me to try this more thoroughly in my current project.

  2. Good tutorial for newbies.Though writen for,it’s also useful for other web techologies.

  3. If only it were as easy as you describe. This example is deceptive. I’ve seen all the little apps that show little things but havent heard of or seen any big achivements with selenium RC and

    See you will run into many problems. Problems that hinder most opensource projects. No Documentation, no code samples and a slew of special rules that will make your project go behind schedule while trying to get it to work.

    I, like most others, was very happy to learn about the IDE, RC and getting it to work with C#. Well, so far its been short lived. Nothing is working.

    Maybe you are smarter than me and can get it to work for you.

    Best of luck!

  4. Hi Peter,
    I know i am very late on this post but i am trying to get code coverage from selenium. I am able to get coverage from unittest ran against an instance of cassini webserver. But it seems that only coverage from aspx files is generated. Have you been able to produce coverage? I would like to hear from you.

    Thanks in advance.

  5. If you are interested in writing, debugging and running web testing with C# or VB.NET and the different flavors of Visual Studio (Including the free express) try is an automation tool designed to implement client-side functional testing of web applications under Internet Explorer 6.x or 7.x, using the C# or VB.NET language.

  6. I tried the selenium tutorial from and I cannot get it to work. I am impressed by the all architecture.
    But both IE and Firefox do not load the page when I run
    – cmd=getNewBrowserSession&1=*iexplore&2=
    – cmd=getNewBrowserSession&1=*firefox&2=

    I do not understand why people would write a tutorial and do not make sure it works or that it is understandable. Could by that it was not updated lately.

    Peter may be you could try for us and report if I am an idiot or there is something wrong.

  7. Selenium RC is working fine for me and I love it. It gets the job done and keeps me coding in C#.

  8. BTW, if you are using Selenium-RC, you will have to do: java -jar selenium-server.jar -multiWindow. It’ll be easier if you call this with a .bat file and use System.Threading;

    proc = System.Diagnostics.Process.Start(“c:\\Selenium\\selenium.bat”);

    selenium = new DefaultSelenium(“localhost”, 4444, “*iexplore”, “http://test”);

    etc..etc.. get the picture?

  9. Great intro, too bad I can’t get Selenium to work on my machine.
    I can run the “dogfood” tests that come with it, but can’t execute any home made or recorded tests.

    When I try, in IE 6 (yes, we are still using it here…) I get a javascript error ‘’ has no properties.
    The error points to the following line of code in selenium-testrunner.js = fnBindAsEventListener(this._onClick, this);

  10. I’ve been using Selenium to test ASP.Net applications for a few days but it has quickly impressed me. Yes, it takes a day or so to really get the hang of it and to see the possibilities. And yes, the documentation is substandard but smart people know how to turn Google into their personal library. There is a wealth of quality Selenium info out there.

    If you’re receiving errors when running Selenium Core then you’ve not set it up correctly, which is fairly typical at first. Some errors have to do with permissions if you copy files from one server to another but that happens with any file that needs to be served up by a web browser.

    I had to deal with 3 files to run my own tests:
    (1) I copied an existing test file to make Test_Login.aspx which contains commands to test my app’s login screen.
    (2) I copied TestSuite.html and modified it so it contains only a reference to my new ./Test_Login.html file.
    (3) I copied TestRunner.hta and modified it to point to MyTestSuite.html instead of TestSuite.html.

    I made a SeleniumTestFramework folder under my web app’s root, and copied Core and Tests folders into it. After that I was able to double-click MyTestRunner.hta in Windows Explorer to launch the test framework. It works beautifully.

    Bottom line: like most any good software tool it takes some elbow grease & smarts to wield properly but once it is working for you, the payoff is golden.

  11. hello…
    I am a manual tester and now i want to learn automation testing.I have some basic knowledge of selenium but i very poor in programming…….so can you tell me can i test website without having knowledge of programming language using selenium.please tell me your answer will helpful for me…if yes then please tell me how can i used and if no then also tell me what should i do.