Posts Tagged ‘ C#

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:

Design pattern for an MSTest automation suite in Visual Studio C#

In this test automation project I will build a Visual Studio Solution for the purpose of maintaining multiple C# projects. The solution will contain all of my QA tests for all application types within a project. For example, you may have API’s, Windows services, console applications, or in the case of this example; websites. Creating individual projects reduces the clutter of loading lots of references because each of these application types have their own set of developer tools necessary for testing. I will also create a tools project for generic, non-test specific functionality; such as random name and address generating methods. To make things easier to manage, the versioning, application configuration file (App.config), and the configuration base class is going to be placed at the solution level and subsequently linked in every project. Use the “Add as Link” feature in Visual Studio to add the Configuration.cs and AssemblyInfo.cs to each project.

To add one more layer of abstraction move the TestInitialize and TestCleanup to a base class for each test project. In this example, WebsitesTestBase is the abstract class from which every TestClass can inherit.

Visual Studio solution structure for an application test automation suite.

Basic Test Automation Visual Studio Structure Design

App.config

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

Configuration.cs

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

WebsitesTestBase.cs

using Jef.Qa;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Jef.QA
{   
  [TestClass]
  public abstract class WebsitesTestBase : Configuration
  {
    [TestInitialize]
    public void SetUp()
    {     }     
    [TestCleanup]
    public void TearDown()
    {     }
  }
} 

JefWordpress.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Jef.QA
{
 [TestClass]
 public class JefWordpress : WebsitesTestBase
 {
   [TestMethod]
   public void TestMethod0()
   {
     Assert.IsTrue(true);
   }
   [TestMethod]
   public void TestMethod1()
   {
     Assert.IsFalse(false);
   }
 }
}

Test automation suite using C#, MSTest and Selenium – Introduction

With my most recent project I have been given the chance to build a test automation suite with no ties to existing test automation design or xUnit tools. I wanted to take advantage to as much of the Microsoft Visual Studio Test tools because having the test cases, defects, and developer work items all in one place with a TFS server was the direction the whole development team agreed upon. Previously I had done some prototyping with test unit frameworks but was still stuck to NUnit and WatiN because of a department policy. WatiN and NUnit ran very successfully for IE but with the browser usage trends changing there is a lot of pressure for multi-browser support.

Creating a test suite that would run on every browser consistently can prove to be a difficult task when you are at the mercy of the browser automation tools. There are a lot of tools out there like Galileo, WatiN, WebAii, etc.. that all claim to have multi-browser support. Much improvement has been made recently among all these tools but at the time Selenium was the only one I could get working on all browser and operating systems quickly and consistently. Having the Selenium IDE test recorder was also a big help.

There are a lot more factors in creating a full suite for all types of applications (Windows Services, API’s, websites, etc..) that also proved to be very successfully introduced using Visual Studio Test Professional and MSTest. For this article series I will focus on the website test automation portion as the goal. In later articles I will show how to add C# projects to the solution to cover other application test suites and use a common configuration, versioning, and tool set.