Testing with Selenium Webdriver, Visual Studio and NUnit

Update 20/06/12 - Updated NareshScaler to work with the new IEDriverServer that ships with the latest version of Selenium Webdriver

We have a multi-tenant solution at work.  As we added tenants we were happy to discover that our app scaled nicely, but we were sad to discover that Naresh, our lone QA superstar, did not.  We discovered (unsurprisingly) it wasn’t possible to stick to a fixed release schedule and do regression/integration testing manually.  So some additional automation was required.

Integration Testing Automation Requirements

Our requirements were

  1. Naresh could create integration tests with minimum oversight from the team
  2. Tests needed to be created quickly without a lot of additional coding
  3. Ideally tools would run on Visual Studio

Ruling out Specflow

I’d been keen to investigate Specflow and the BDD style of integration testing.  However, it quickly became apparent that this would require a significant effort in time to successfully wire up the tests, and would require a reasonable amount of dev to ensure each test passed.  Thus failing requirements 1 and 2.

Ruling in Selenium Webdriver

We already had a bit of experience with Selenium and we found this excellent blog post from Stephen Walther.  Reading this we realised that  Selenium Webdriver met our requirements perfectly.  We could install Selenium Webdriver into Firefox and export the scripts as C# Webdriver classes.  We could then add the classes into a simple test-runner and we’d be able to scale Naresh :-)

Some Selenium Pitfalls

However, it’s not all good news.  One thing to point out is that there can be a certain amount of flakiness when the various selenium drivers are trying to locate elements on your page.  Ie a test will fail once, then pass again later.  This is obviously far from ideal, but overall I think the benefits out-weigh the drawbacks.   Test that fail consistently can be investigated.

One way to minimise these failures would be to run the tests in only one browser, the Firefox driver seems the most reliable.  If you’re not doing loads of Javascript this is probably safe enough.

Introducing NareshScaler

I wrote NareshScaler to allow Naresh to quickly add each Selenium macro into the test-runner.  I’d also been wanting to try out creating a nuget package for a while, so this seemed like a perfect chance to give it a go.

You can install NareshScaler into your Integration Test project using the nuget package manager.  Once successfully installed, you should add your Selenium Webdriver class file(s).

Then simply mark each class as inheriting from NareshScalerTest.

You will now have to override the RunSeleniumTests method.  You simply need to wire up the driver and list any test methods in your selenium Webdriver class, ie:

[TestFixture]
public class NugetOrgTest : NareshScalerTest
{
private IWebDriver driver;
private string baseURL;

[SetUp]
public void SetupTest()
{
baseURL = "http://nuget.org/";
}

public void Test_That_NareshScaler_Exists_On_NugetOrg()
{
driver.Navigate().GoToUrl(baseURL + "/");
driver.FindElement(By.Id("searchBoxInput")).Clear();
driver.FindElement(By.Id("searchBoxInput")).SendKeys("NareshScaler");
driver.FindElement(By.Id("searchBoxSubmit")).Click();
driver.FindElement(By.LinkText("Naresh Scaler")).Click();
}

public override void RunSeleniumTests(IWebDriver webDriver)
{
driver = webDriver;
Test_That_NareshScaler_Exists_On_NugetOrg();
}
}

We use NUnit here, so once everything is wired up, you should be able to point NUnit at your Integration Tests dll and see all your tests running in IE, Firefox and Chrome  Which I have to admit is pretty cool when you see everything running automatically.

Additionally NareshScaler includes an Nant build file, to allow you to wire up your integration tests into CruiseControl etc.  I’ve added a sample, so you can hopefully see how it works.  Hope you find it useful.

About these ads
This entry was posted in c#, testing, web development and tagged , , , , , , , . Bookmark the permalink.

5 Responses to Testing with Selenium Webdriver, Visual Studio and NUnit

  1. LMGriese says:

    This is awesome. I, just like Naresh, am the resident lone QA superstar and have been looking for a nice, straightforward way to run my Selenium C#/NUnit scripts in all three browsers. I recently started using your NuGet package.and am really liking it. Thank you!!! I am wondering though if you have seen a similar problem that I am getting… When I use the ChromeDriver and have the ImplicitlyWait command in use the browser launches just fine but then immediately blows up and I see error in NUnit: OpenQA.Selenium.WebDriverException : Unexpected error. System.Object[].
    If I exclude the ImplicitlyWait code then all runs fine. I am just curious if you have seen this before and might be able to shed some light for me. Thanks again for a great, helpful package! Linda

  2. Iain Hunter says:

    Hi Linda

    Glad you like the package, I just updated it to use the latest versions of ChromeDriver and Selenium.Webdriver. Run the following command and let me know if it improves anything

    update-package NareshScaler

    Cheers
    Iain

    • LMGriese says:

      Hi Iain –

      Thanks so much for the reply. Just wanted to follow-up to say I finally had a chance to get my environment updated with the latest package and all is well. I am now running scripts in Chrome, FF and IE. WooHoo!! :)

      Thanks again,
      Linda

  3. Vaishnavi says:

    Hi,
    Thanks for the great installer.
    I tried to set it up to run on multiple browsers, but when i run it asks for ‘main method needed for entry point’ and i added a main function and called this method ‘Test_That_NareshScaler_Exists_On_NugetOrg();’.
    But it is throwing exception, ‘NullReferenceException’ on this line:
    driver.Navigate().GoToUrl(baseURL + “/”);

    Could you please provide your input and what I might be missing or doing wrong?
    Any help is greatly appreciated.
    -Vaishnavi

  4. Yvetteseb@gmail.com says:

    Hi There,
    Do you mind explaining me what the advantage of using NareshScaler package. I am trying to automate testing process just by myself. SO you mind sharing your knoweldge please?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s