Monday, May 17, 2010

Selenium and Visual Studio 2010 C# (CSharp) Express edition

Again, that question - what tool to use for this automation script? It is not a big script so the risk is low even if the selection is not optimal....

Looked at Watir and Selenium. Watir is more command line oriented than Selenium. I was impressed with both as I could execute a single line in interpretive mode and get something to happen in the browser. Amazing!

Selenium had the advantage of having a recorder. It is really handy to get most of the controls without painstakingly going through the source. In couple of sites there was a reference to a recorder for Watir, but never found the tool. This was the main reason to go to Selenium. It didn't take long to realize that the builtin scripting capabilities are not flexible enough to my work. Then, the next stage -went for Selenium RC. It supports many different languages to drive automation. I went for VS C# 2010 Express, which is free from MS.

I was pleasantly surprised of the compatibility of Selenium generated C# script and the VS C#. All I had to do was changing the Target .net framework version in C# project and then copy the class.
  1. After recording, select the 'C# - Selenium RC' from Options->Format. This will generate the C# class for your test.
  2. Note down the class name from 'public class ' statement. This is the name of the test case.
  3. Go to the VS project properties and change the target framework to .Net framework 2.0. This gave me hours of trouble till found!
  4. Remove any references marked with a yellow icon in Solution Explorer, and then any 'using 'statements marked with red underline in program.cs.
  5. From the same dialog, brows and add all the dlls in 'Selenium-dotnet-clientdriver-<ver>' package. This is in Selenium-remote-control <ver>.zip file. I was using version 1.0.3.
  6. Create a new class. Use the name noted in 2. Cut and past the output from Selenium IDE to this class file.
  7. Add the name space of class to program.cs
  8. Simply create the object, and start calling the SetupTest(). Then write your program, you can use/add/edit functions created by Selenium. Call TearDownTest() at the end, which would close everything.
It was a rare situation where two independent software programs agreed with each other so well :-)



No comments:

Post a Comment