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:
- 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.
- 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.
- 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.
- 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…
There are three parts to setting up Selenium:
Okay, at this point, we should be all set with Selenium. Verify the following:
- 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.
- 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.
- 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
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…