Selenium RC In C# as a Console App: Another End-To-End Example
The purpose of this post is to show people how to wrap a Selenium RC test up into a console app (*.exe) so can been executed from the command line. This is a solution makes it where you don’t have to have NUnit involved.
Déjà vu all over again! Another basic Selenium example. What the heck!? Can’t this guy do anything else?
Well, I agree this is somewhat repetitive, but I hope that this sequel will be in the league of Empire Strikes Back or Star Trek: The Wrath of Khan. Both movies, though they were sequels, were arguably greater than the originals. These sequels introduce us to the likes of Ricardo Montalban playing “Khan” and Billy Dee Williams playing “Lando Calrissian”. And of course, a muppet playing “Yoda”.
Of course, many sequels start off with the desire to be Empire, and instead they end up being:
- Caddyshack II
- Beastmaster 2: Through the Portal of Time
- Speed 2: Cruise Control
- Batman and Robin (No, we don’t need nipples on the Batman suit, Joel Schumacher. But thanks for asking.)
- Mannequin: On the Move.
- Or the biggest sacrileges of all time Godfather III
- And Episode I (Empire, it is not!).
My hope is that this post doesn’t suck like those movies. At the very least it’s nipple and Clooney free.
First off, you need to get all these things (if you don’t have them):
- Selenium RC
- Firefox
- Selenium IDE
- Java Runtime Environment (You should already have this)
- Visual C# 2008 Express Edition (Only get this if you don’t have a full version of Visual Studio)
Install instructions for the downloads:
Selenium RC
-No installation necessary. Just find a permanent location and unzip it there. I unzipped mine at C:\ just to make things simple
Firefox, Visual Studio Express, and Java
-Just run the respective installs with all defaults selected.
Selenium IDE
-To install, right click on the file and select “Open With…”
-Select Firefox in the programs list. If it’s not in the immediate menu, browse to it. (default location should be “C:\Program Files\Mozilla Firefox\firefox.exe“
-When Firefox launches, click button in the dialog box that says “Install Now”.
-After the install of the plug-in, hit the “Restart Firefox” button when you see it.
Phase One: Record a Test using Selenium IDE
Okay, so, now that you have the tools, we’re going to record a simple test in Selenium IDE. Even though we want to eventually run the test in Selenium RC, Selenium IDE is a great tool for getting the basic test recorded since it’s all UI point and click.
- Start up Firefox.
- Click on the “Tools” menu and click on “Selenium IDE”
- (Take your time to explore the Selenium IDE form)
- Notice that it’s already recording. The red circle button on the right side is pressed in:
- Type “www.google.com” the main Firefox browser address bar and navigate to it.
- In the search box, type in “thetestingblog.com” and hit the “I’m Feeling Lucky” button.
- Notice in your Selenium IDE window that steps are being recorded.
- With any luck, you hopefully landed on this blog’s home page.
- Highlight the text “Daniel Brown” on the page, right click. You should select the option that says “VerifyTextPresent Daniel Brown”. See below:

- Okay, we got our test recorded. Now let’s run it to make sure it passes.
- Unclick the red circle button to turn off recording.
- Click on the green play triangle with the single bar to run the test

- All green, it passed. Good.
- Now we want to convert this test to C#.
- Select “Options ->Format -> C# – Selenium RC” from the Selenium IDE window.

- Next select all the C# code and copy it.
Phase Two: Setup the C# Console App
- Open up Visual Studio
- Select “File ->New ->Project”
- In the New Project window under project types, select “Visual C# ->Windows”
- Select the “Console Application” Template and name it something like “TestingBlogSeleniumTest” and click “Ok”.
- It will create the project along with a file called “Program.cs”. Paste your SeleniumIDE test into Notepad. You’re going to take pieces out of the test and put it in “Program.cs”.
- Add the using references from the SeleniumIDE test. Replace any that are there.
- Add the “ISelenium” and “StringBuilder” object references inside the Main() method. As you see, I removed the “private” access control prefixes. We don’t need them.
- Add the body of the SetupTest() method after the object references. (I put a comment to designate that it was part of the setup.)
- Change “*chrome” to “*iexplore” for Internet Explorer. Use “*firefox” for Firefox. (There are other browser options, but those work best.)
- Change the URL to the site you start your test on. In this case, change that URL to “http://www.google.com/” since we’re starting from Google.
6. Paste body of the TheUntitledTest() method into the Main() method.
7. Paste body of the TeardownTest() method into the Main() method.
8. Get rid of the Try/Catch blocks on Teardown.
9. Get rid of the StringBuilder references.
10. Get rid of the Try/Catch block in the test body, replacing it with an if statement that writes to the console window if true (See my code below). Notice that I have to cast the Selenium.IsTextPresent statement as a boolean. I don’t understand why it doesn’t work without it because the return type for that method should be boolean. But whatever. I’m just dummy tester; what do I know?
- Next we need to add all the references so this thing will run.
- Go to the solution explorer, and right click on the “References” and select “Add Reference…”
- Hit the “Browse” tab and navigate to where you unzipped your SeleniumRC files. Look in the dotnet client driver folder. Here’s the path to mine:
C:\selenium-remote-control-1.0.1\selenium-dotnet-client-driver-1.0.1\
- Once you navigate to where the proper *.dlls are, select all of them and click “OK” to add them to the project. (I know you can probably get away with adding less, but let’s keep it simple.)

- Build the project to see if it works. (Mine worked. Hopefully yours did too.)
Phase Three: Running the Selenium RC test Console App
- In order to run the Selenium RC test, you first have to have the Selenium RC server running. The Selenium RC server folder should be in the files you unzipped. Normally, I have a batch file on my desktop created to run the Selenium Server, but for our purposes, we’ll just run this from the “Start -> Run” commandline, using this command:
java -jar C:\selenium-remote-control-1.0.1\selenium-server-1.0.1\selenium-server.jar
- You should see some a dos window popup, and you will see something like the screenshot below. DON’T CLOSE IT WHILE TESTING; IT NEEDS TO STAY RUNNING. If you don’t have Java installed, this won’t work. But most people should already have Java.

Next you need to select Debug from your Visual Studio menu and select “Start Without Debugging”. (I’m choosing this option because it doesn’t close the console window after the end of execution. The debug option does.)
You should see the Selenium window(s) pop-up. And it should do it’s thing. When it’s finished, you should see something like this:
So, where is my test executable? Well, it’s in the bin\debug folder of the project. For me, the path to it looks like this:
C:\Documents and Settings\dbrown\My Documents\Visual Studio 2008\Projects\TestingBlogSeleniumTest\TestingBlogSeleniumTest\bin\Debug\
So, what do I need to run the program? Just take the executable file and the dll’s. Make sure the dll’s are in the same folder as the test when you execute it, otherwise you will get a big fat error. If you send the executable to someone, make sure you send the dll’s too.
Remember if you port that executable to another computer, it needs to be a Windows box with the .NET framework installed and Selenium server needs to be running. I hope this actually helps someone. I’m going to post some tricks to “sex up” your console app. Be looking for them shortly.
Sorry, I still haven’t figured out Selenium GRID good enough to post on it. Maybe Marisa could post on it? She’s awesome like that.
Read Full Post | Make a Comment ( 4 so far )







