Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Writing integration tests for ASP.NET with Selenium – Part 1

, 12 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This is the first in a series of posts on writing integration tests for ASP.NET using the Selenium web application testing system.

This is the first in a series of posts on writing integration tests for ASP.NET using the Selenium web application testing system.

In this post, I explain what it is, why use it and how to setup and configure it. In the next subsequent posts, I’ll do a code walk-thru and also share some advanced scenarios.

What is Selenium?

The best description of Selenium is probably the one mentioned on their website:

“Selenium automates browsers. That’s it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.”

Why use Selenium?

There are other integration testing web frameworks out there such as WaitN or the Lightweight Test Automation Framework for testing ASP .NET web apps. So, why use Selenium? Here are the main reasons why picked it:

  1. Selenium allows you to record test scenarios manually (by browsing to specific pages and clicking buttons, etc.) as well as programmatically (by coding up a test case). Thus the tool can be used by both developers and QA’s. You can even use a combination of recorded test scenarios and C# test cases to integration test your application.
  2. One feature that I really liked is that you can export a recorded test scenario as a C# test-case! This technique can be used to drastically speed up the Selenium learning curve since it spits out code that you can use inside C# test-case. For instance, when you’re unsure how to code up a specific scenario you can manually record it and then export it to see what the code needs to look like.
  3. The recorded scripts can be used as a tool to speed up manual testing. For example, I never manually login into the web application that I am currently implementing anymore. Instead I run a test scenario that I had previously recorded, conveniently named Login, that logs me in. No more manually typing in username/password! You can use this technique to directly land on specific pages and/or fill-in and submit forms as well. This adds up to save you boat loads of time during your manual testing.
  4. Finally, the test recorder is a small Firefox plugin and integrates real nice with Firefox.

Convinced? Awesome! Let’s move on to setting up Selenium…

Initial Setup

There are three parts to setting up Selenium:

  1. Installing the Selenium IDE: A Firefox plugin that allows you to manually record test scenarios. Find and download the Selenium IDE plugin from the Selenium Downloads section. After it downloads, fire up the Firefox browser -> Go to Add Ons from the Main Menu -> Click the Settings icon -> Select the Install Add-On From File option -> Click Install Add-On.
  2. Installing the Selenium stand-alone server: Accepts commands from your C# test cases and automates the browser. Find and download the Selenium Server from the Selenium Downloads section. It’s a JAR file so make sure you have the JDK (Java Development Toolkit) installed. If not, then download the JDK @ Java Downloads.
  3. Grabbing the ThoughtWorks Selenium DLL: The missing link between Selenium and C#. It enables C# test-cases to talk to Selenium. Find and download the C# Selenium Client Driver from the Selenium Downloads section. Upon extracting the zip file, you’ll find a bunch of the DLLs in there. Ignore everything and just copy/paste the ThoughtWorks.Selenium.Core.dll file into wherever your project references external DLLs from. That’s all we need! The rest of the DLLs provide a slightly different API that’s suppose to be bit easier to work with. But I didn’t find them very useful and more importantly I had trouble running my tests with them. Kept getting errors. Anyways, I’d recommend just sticking with the ThoughtWorks DLL. The subsequent posts on this topic that I will be writing will use the ThoughtWorks API.
  4. Okay, at this point, we should be all set with Selenium. Verify the following:

    • You are able to record a test scenario: Fire up Firefox. On the Main Menu, under Web Developer, you should now see a Selenium IDE option. Or you can use the Ctrl+Alt+S shortcut key to bring it up. Click the record button on the top right, browse to goggle.com, and search for something. Click the record button again to stop the recording. Now try running the script by clicking the play button and see what happens. Make sure you are already on the google.com page when you click the Play button. Hopefully, it worked. If not, call me. I’ll help you out and I only charge a $150/hr. Just kidding. If it doesn’t work then try re-installing the plugin, I guess. :)
    • You are able to start the Selenium server: Move over the Selenium JAR file that you download in step two to a folder of your choosing. Open up the command prompt, navigate to the folder where you have placed the JAR file and run the following: java -jar selenium-server-standalone-2.20.0.jar. That should start the Selenium stand-alone server. Press Ctrl+C to quit. For feasibility, I placed the JAR file in the same directory as my unit-tests project. I then created a BAT file that I just double-click to start the server. Here is what the BAT file looks like:
    • REM start-selenium-server.bat
      cd .
       
      java -jar selenium-server-standalone-2.20.0.jar
    • You are able to write and run C# test-cases using the ThoughtWorks DLL for Selenium – This is what I’ll go over in my next post on Selenium. Check back soon…

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nizar Noorani
Software Developer (Senior) NCube, Inc.
United States United States
Nizar Noorani is an independent software consultant. He provides services in the areas of web applications and systems architecture, analysis, design and development. Although proficient in a variety of technologies, he specializes in the .NET technology. He can be reached at nizar.noorani@gmail.com.

Comments and Discussions

 
QuestionWhy not to use Visual studio cided ui tests? They do the same PinmemberDennis_Nerush12-Apr-12 9:49 
AnswerRe: Why not to use Visual studio cided ui tests? They do the same Pinmemberstooboo12-Apr-12 12:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 12 Apr 2012
Article Copyright 2012 by Nizar Noorani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid