Writing and debugging Selenium tests with MSTest and Visual Studio

Multi-browser compatibility is now an essential part of website testing with browser usage trends starting to even out among the browser applications. To utilize Selenium’s ability to run on multiple operating systems and browsers, you need to obtain the Selenium .Net driver. For this example, the Selenium dot Net client driver version 1.0.1 will be used and can be obtained here. Download the C# (Selenium RC) 1.0.3 package and extract selenium-dotnet-client-driver-1.0.1 and selenium-server-1.0.3 to a local toolset folder.

The Selenium HQ instructions will tell you to include multiple files but the only one that needs to be referenced is ThoughtWorks.Selenium.Core.dll. Once the Selenium reference is added to the test project, add the associated settings for the Selenium RC server agent and browser string.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
 <add key="BrowserAgent" value="localhost"/>
 <add key="BrowserString" value="*firefox"/>
 <add key="JefWordpressUrl" value="http://www.jeffmsmith.com/wordpress/"/>
 </appSettings>
</configuration>

Configuration.cs:

using System.Configuration;

namespace Jef.Qa
{
 public class Configuration
 {
   public string BrowserAgent
   { get { return ConfigurationManager.AppSettings["BrowserAgent"]; } }

   public string BrowserString
   { get { return ConfigurationManager.AppSettings["BrowserString"]; } }

   public string JefWordpressUrl
   { get { return ConfigurationManager.AppSettings["JefWordpressUrl"]; } }
 }
}

WebsitesTestBase.cs:

using System;
using Jef.Qa;
using Selenium;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Jef.QA
{
[TestClass]
public class WebsitesTestBase : Configuration
{
protected ISelenium Browser;

[TestInitialize]
public void SetUp()
{
  Browser = new DefaultSelenium(BrowserAgent, 4444, BrowserString, "http://www.jeffmsmith.com/");
  Browser.Start();
  Browser.WindowMaximize();
}

[TestCleanup]
public void TearDown()
{
   try
   {
     Browser.Close();
     Browser.Stop();
   }
   catch(Exception e)
   {
    Console.Out.WriteLine("Error during tear down: " + e.Message);
   }
  }
 }
}

Now that Selenium is referenced in the base file and the setup and teardown are prepared, you can start writing tests with the Selenium browser object.

JefWordpress.cs:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Jef.QA
{
 [TestClass]
 public class JefWordpress : WebsitesTestBase
 {
  [TestMethod]
  public void ValidateJefWordpressHome()
  {
   Browser.Open(JefWordpressUrl);
   Assert.IsTrue(Browser.IsTextPresent("jef"));
  }
 }
}

Once the project compiles you will need a Selenium RC server to run and debug the tests. The Selenium RC server can be installed to any client and the debugging can be done from your current Visual Studio session as long as you update the BrowserAgent setting in the App.config. All that is needed to run the Selenium RC server is access to java from a command prompt. For example, from windows:

1. Click Start, select Run.

2. Type ‘cmd’ and press Enter.

3. Type ‘cd “C:\Program Files (x86)\Java\jre6\bin”‘ and press Enter.

4. Type ‘java.exe -jar c:\selenium-server-1.0.3\selenium-server.jar’ and press Enter.

To avoid having to run Selenium RC from a separate application, use the Visual Studio External Tools functionality to run the command through the Visual Studio output.

1. From Visual Studio, click Tools and select External Tools.

2. Enter the following values on the External Tools window:

Title: &Selenium RC

Command: C:\Program Files (x86)\Java\jre6\bin\java.exe

Argument: -jar C:\selenium-server-1.0.3\selenium-server.jar

Initial Directory: C:\Program Files (x86)\Java\jre6\bin\

3. Check the box next to Use output window.

4. Click Ok.

5. Click the Tools menu and select the Selenium RC option that was just created.

Notice in this example the Argument field includes the relative solution directory, $(SolutionDir). This is because I like to keep my selenium-server directory within my source controlled toolset folder.

When Selenium RC is running from within Visual Studio, the Output window should look like this:

    • Troy
    • May 25th, 2011

    Cool stuff, very clean and easy to read. How do you like the Selenium library, compared to WATIN?

      • mrjeffms
      • May 25th, 2011

      Selenium 1 has almost all the same commands of WatiN, but it doesn’t allow you access to the browser DOM object like WatiN does. For example, in WatiN you can do something like this:

      public IE Browser = new IE(“http://www.jeffmsmith.com”);

      public SubmitButton()
      { get
      {
      if(Browser.Frames.Length > 0)
      return Browser.Frames[1].Button.Find.ById(“myButtonId”);
      else
      return Browser.Button.Find.ById(“myButtonId”);
      }
      }

      [TestMethod]
      public void WatiNTest()
      {
      SubmitButtion().Click();
      }

      (FYI, I don’t know if this will compile.)

      This comes in really handy if your page controls are always changing Id’s or if the browser objects are sometimes within frames but not always because it provides a layer of abstraction in which you can fix the browser controls in one place only.
      In Selenium 1 the page element identification is passed through the method or action. For example browser.Click(“myPageId”). This means that you will have to provide the element locate string for every command. I work around this issue by making each string a property and an associated Resource.resx file. This only solves part of the problem because 1.) You still have to type out the string variable for every Selenium command 2.) You cannot use the browser object to conditionally locate a page element.
      Selenium 2 however, corrects this problem as well as provides a lot more support for IntelliSense and many other features. I am not very familiar with Selenium 2 and Webdriver myself because it is still in beta so I don’t use it professionally. It will be a great project so I’ll make sure to post a walk through for Selenium 2 in the near future. For now you can checkout the Selenium HQ site’s article here.

    • Troy
    • May 26th, 2011

    Good stuff, thanks! :-)

  1. No trackbacks yet.


9 + four =