Click here to Skip to main content
15,886,091 members
Articles / Web Development / HTML

Writing Integration Tests for ASP.NET with Selenium – Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Apr 2012CPOL5 min read 16.2K   6   2
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-through 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 I 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 QAs. 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 plug in 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:

Okay, at this point, we should be all set with Selenium. Verify the following:

  1. Installing the Selenium IDE: A Firefox plug in that allows you to manually record test scenarios. Find and download the Selenium IDE plug in 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 supposed 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.
    REM start-selenium-server.bat
    cd .
     
    java -jar selenium-server-standalone-2.20.0.jar
    • 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 plug in, I guess. :)
    • You are able to start the Selenium server: Moveover, the Selenium JAR file that you download in step 2 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:
    • 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…
This article was originally posted at http://nizarnoorani.com?p=519

License

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


Written By
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 Pin
Dennis_Nerush12-Apr-12 9:49
Dennis_Nerush12-Apr-12 9:49 
AnswerRe: Why not to use Visual studio cided ui tests? They do the same Pin
stooboo12-Apr-12 12:40
stooboo12-Apr-12 12:40 
Don't you need Ultimate Or Premium for coded UI Tests?

So Selenium etc. are available to more developers Smile | :)

Also http://blogs.msdn.com/b/gautamg/archive/2010/01/07/platform-support-for-coded-ui-test-and-fast-forward-feature-of-test-runner.aspx[^] indicates no support for Chrome.. not sure if support has been added now but I couldn't find anything more up-to-date saying it 'has'

cheers
Stu

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

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