Home > Agile, C#, Selenium, Test Automation > Test Automation using C#, Selenium WebDriver and NUnit

Test Automation using C#, Selenium WebDriver and NUnit


  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool


To install the Selenium WebDriver project:
  • Create a new Project with C# Class Library and save it (‘TestAutomation’ in my example).
  • Rename the class ‘Class1.cs’ to ‘Driver.cs’.
  • Download and install NuGet Package Manager using Tools -> Extension Manager. Restart MS Visual Studio in order for the changes to take effect.
  • Go to Package Manager Console install the latest version of Selenium WebDriver by running the command Install-Package Selenium.WebDriver -Version 2.20.0
  • Install the latest version of Selenium WebDriver Support Classes by running the command Install-Package Selenium.Support
Referencing the NUnit Library:
  • Download the latest NUnit framework from the site: http://www.nunit.org
  • Install the NUnit software on your machine.
  • In Visual Studio, Go to the Project -> Add Reference menu item.
  • When the Add Reference dialog appears, click on ‘Browse’ and navigate to C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework and select nunit.framework.dll.
  • Finally, the solution Explorer should look like this:

Consider a sample test scenario:

As an end user,
I would like to visit the google search page 
And then I would like to search an item so that
I can view the search results


  • Define the browser on which the tests needs to be executed in [SetUp] attribute.
  • In the [Test] attributenavigate to the google search page.
  • In the [Test] attributefind the text input element by its name. Here, the element name for the search field is ‘q’, which is identified using the Firebug (Add-ons for Firefox).
  • In the [Test] attribute, input the search text and submit the form.
  • In the [Test] attribute, validate the result using Assert method.


  • Copy and paste the code snippet below into your Visual Studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace TestAutomation
 public class Driver
 IWebDriver driver;

 public void Setup()
 // Create a new instance of the Firefox driver
 driver = new FirefoxDriver();

 public void Teardown()

 public void GoogleSearch()
 //Navigate to the site
 // Find the text input element by its name
 IWebElement query = driver.FindElement(By.Name("q"));
 // Enter something to search for
 // Now submit the form
 // Google's search is rendered dynamically with JavaScript.
 // Wait for the page to load, timeout after 5 seconds
 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
 wait.Until((d) => { return d.Title.StartsWith("selenium"); });
 //Check that the Title is what we are expecting
 Assert.AreEqual("selenium - Google Search", driver.Title);
Running the tests using NUnit:
  • In Visual Studio, Go to Projects -> Project Properties (‘TestAutomation Properties’ in my example).
  • Click on the ‘Debug’ tab.
  • Set the ‘Start external program’ to the location of NUnit exe file (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe) as shown in the snapshot below.
  • Build the solution, Go to ‘Build -> Build Solution’ (hit the F6 key) in Visual Studio.
  • Execute the test, Go to ‘Debug -> Start Debugging’ (hit the F5 key) in Visual Studio. Visual Studio invokes the NUnit application.
  • In NUnit, click on ‘File -> Open Project’ and choose the location of the TestAutomation.dll file as shown in the snapshot below.
  • In NUnit, click on the ‘Run’ button to run the tests. Test is executed using NUnit and the result will be displayed in NUnit GUI window as shown in the snapshot below.
  1. Gopi
    March 19, 2012 at 10:28 pm

    Nice introduction to Selenium test automation using C# !

    • March 13, 2014 at 6:11 am

      Yes .. ! Nice Testing Tool ..!

  2. Amarjeet Chavhan
    April 18, 2012 at 7:04 am

    Struggling from last couple of days for any sample C# code for testing the implementation of VS2010 & Nunit…This is something G8…. Every new bee must visit this post…..

  3. rv8881
    July 11, 2012 at 8:37 pm

    Very detailed and simple step by step instructions. This will be handy for any newbee using Selenium n C#. Too gud 🙂

  4. VIRAL
    July 19, 2012 at 12:47 am

    Nice introduction…thanks for this.

    But when I try to install Install the latest version of Selenium WebDriver Support Classes from console of package manager it gives me the following error. Could you please let me know what’s wrong? I tried to install different version as well but i am not able to. However, i have NUnit 2.6, does it matter and interrupting me to download or what?

    Install-Package : The current environment doesn’t have a solution open.
    At line:1 char:16
    + Install-Package <<<< Selenium.Support -Version 2.23.0
    + CategoryInfo : InvalidOperation: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetNoActiveSolution,NuGet.PowerShell.Commands.InstallPackageCommand

    • July 19, 2012 at 1:04 am

      You are getting this error because you have not created a solution or may be Solution Explorer pane is empty in the right-hand side of Visual Studio. Make sure there is a solution created or create a new Project with C# Class Library before you run the command ‘Install-Package Selenium.WebDriver’. Hope this should fix your problem 😉

  5. Beena
    July 27, 2012 at 5:28 pm

    Reply | Quote

    Very detailed and simple step by step instructions.This is great

    • July 27, 2012 at 7:38 pm

      Thanks Beena! Hope you have a happy time with Selenium….

  6. September 14, 2012 at 7:51 am

    How to avoid using XPath completely? I want scripts which use Text or id. In case id is dynamically generated then it should use only InnerText. I don’t want to use XPath to record as well as replay. Is it possible using Selenium?
    Thanks in advance!

  7. Bogdan
    October 3, 2012 at 2:18 pm

    renderinglife :
    How to avoid using XPath completely? I want scripts which use Text or id. In case id is dynamically generated then it should use only InnerText. I don’t want to use XPath to record as well as replay. Is it possible using Selenium?
    Thanks in advance!

    i recommend (as do a lot of other people) to use CSS selectors as they are the most accurate and to my knowledge the fastest. xpath would be the second best option so no need to avoid it.

    • October 25, 2012 at 6:16 am

      Thanks Bogdan,
      I agree the CSS Selectors are better. Can anyone provide me a link on how to create custom CSS Selectors esp. which can select span with text as input. How to use Selenium Builder or Selenium IDE to record CSS selector method while replying a scenario?
      I tried to create a custom CSS selector / locator using Sameer Badle articles but I dont know how to implement it in correct way.
      Help needed on below things:
      1. Adding Custom CSS locator script to Selenium IDE or Selenium builder.
      2. Customizing Selenium IDE or Selenium Builder to record using the above Custom CSS Locator and checking how to replay the same.
      Thanks in advance!

  8. October 23, 2012 at 6:41 pm

    It’s an remarkable piece of writing in favor of all the web visitors; they will obtain benefit from it I am sure.

  9. Chinkoo
    October 31, 2012 at 6:33 pm

    Thanks for posting this, very good intro on Selenium. Is there any book/ link that I can use to automate UI testing ? I am new to selenium and automation, I tried the above steps and changed implementation for the web page I’m currently working on. But is there any advanced tutorial which provides details on automating a web page ?

    Thanks in advance.

  10. November 6, 2012 at 4:12 am

    Remarkable! Its really remarkable paragraph, I have got
    much clear idea concerning from this post.

  11. November 26, 2012 at 7:31 am

    Hmm it appears like your site ate my first comment
    (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to everything. Do you have any helpful hints for inexperienced blog writers? I’d definitely appreciate it.

    • November 26, 2012 at 9:04 pm

      Thanks! While writing the blog, just think yourself as an end user and present your ideas such that it would be easy for anyone to understand and implement it.. That’s it 🙂

  12. Arjun
    November 29, 2012 at 2:19 pm

    Thank you very much…you make me fly……please post how to test application using keyword driven framework….

  13. Vitaly
    February 1, 2013 at 12:04 pm

    Got an error:
    в OpenQA.Selenium.Support.UI.DefaultWait`1.ThrowTimeoutException(String exceptionMessage, Exception lastException) в c:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\DefaultWait.cs:строка 209
    в OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition) в c:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\DefaultWait.cs:строка 193
    в TestAutomation.Driver.GoogleSearch() в D:\xkav_VS\xkav_work\Selenium\Projects for VS 9.0\LibrarySeleniumTests\LibrarySeleniumTests\Class2.cs:строка 44″

    Appreciate any suggestions.

    • February 7, 2013 at 2:05 am

      It is hard to troubleshoot the problem remotely. However, the error report says that there is a timeout exception as the WebDriverWait function did not work as expected. Can you give a try with increasing the timeout period (say around 10)?

  14. QA Tester
    February 6, 2013 at 7:02 am

    Can you please tell me where the results will be logged for these test cases after run?
    Is there any way to generate logs of what we have run so far or what are the results? Our test cases have been passed or failed etc.

    Thanks by the way, your tutorial is amazing and quite easy for those who started very first time.

  15. Karthikeyan
    February 6, 2013 at 10:31 am


    I need to assert the text which is under header tag My dashboard. I am using C#, Nunit + Webdriver. Please help me in getting this.

    • February 7, 2013 at 2:44 pm

      Use Firebug and identify the text under the header tag using Xpath or CSS Selector and then add it in the Assert statement.

  16. QA Tester
    February 7, 2013 at 2:16 pm

    I did not find any difference in the other tutorial. As both have the same results. NUnit also gives the same results. What’s new Specflow did?

  17. dipali zinjad
    March 11, 2013 at 11:12 am

    Nice Article!!!!!

  18. May 2, 2013 at 9:18 pm

    I got this web page from my buddy who informed me regarding this
    website and at the moment this time I am browsing this website and reading very informative articles

  19. May 5, 2013 at 4:39 am

    My partner and I stumbled over here from a different web page and thought I might as well check
    things out. I like what I see so now i am following you.
    Look forward to going over your web page repeatedly.

  20. May 7, 2013 at 2:08 am

    i actually do agree with you!

  21. Shun
    May 23, 2013 at 1:25 pm

    Very Good post for beginners

  22. Mahesh Upadhyay
    June 6, 2013 at 6:00 am

    Great Post Anoop, you explain it in very detail step.

  23. Rajesh
    August 8, 2013 at 6:12 pm

    Excellent post Anoop.. It helped a lot.. I’m all set with running these tests in my local machine. Can you please help me with the steps of integrating it with the continuous build. We’re planning to move everything to bitbucket (https://bitbucket.org/) and I’ve been asked to provide integration test (selenium) wrote in C# – VSTS 2010 which should run after the build

    • August 13, 2013 at 10:15 pm

      I have 2 posts on running the selenium automation tests using NAnt and integrating with Jenkins. Please look into it.

  24. Nazia Hasan
    August 13, 2013 at 4:11 pm

    Hope you are doing fine.
    My visual studio is of 2012 express edition.
    So I am finding no ways to change the Start Option in Debug tab of the TestAutomation Project Property.
    I went to msdn.microsoft.com site to look for a solution. But it was mentioned there that the Start option could only be change with the full Visual Studio Product.
    [ref: http://msdn.microsoft.com/en-us/library/vstudio/68c8335t(v=vs.100).aspx]

    So is there any other way to run the automated test according to your given instructions via Visual Studio 2012 Express Edition?

    I am just to reach my goal, but the express product simply put off all my works. 😦

  25. Nazia Hasan
    August 13, 2013 at 5:38 pm

    Hope you are doing fine.
    I fixed the problem of not setting StartAction properties in debug tab under project properties.
    I manually selected the project dll file from NUnit and ran the test.
    everything worked well, except for the last line in the code:

    Assert.AreEqual(“Selenium – Google Search”, driver.Title);

    for this the test is failing and I am getting the following failure reason in NUnit:

    Expected string length 24 but was 6. Strings differ at index 0.
    Expected: “Selenium – Google Search”
    But was: “Google”

    so if the change the text into google, the test passes.
    But it is not the desired result I am looking for

    So what should be done in this place?

    What I tried to do is to get the value through firebug, but the term “google” couldn’t be found

    so a bit help will be appreciable

    • August 13, 2013 at 10:12 pm

      Looks like selenium web driver is picking up the title of Google home page but not the search results page. You need to tweak the code for delay/wait such that assert statement picks up the title of google search results page.

      • Nazia Hasan
        August 17, 2013 at 8:53 am

        Hi Anoop,
        Thanks for your reply.
        I manually tried to do the testing to see what actually google displays as search result when I type selenium.
        So what is returned is this:


        So the term “Selenium – Google Search” term doesn’t appear anywhere.
        I am sorry I can’t attach snapshot in here, else it would be better to understand.

        So should it needs to be done something like this? like getting the textbox element and obtaining its value?

        Let me know

  26. steveculshaw
    October 9, 2013 at 7:20 am

    Excellent post, which I tried to follow step-by-step (apart from using IE, rather than Firefox), but the test fails with …

    SetUp : OpenQA.Selenium.DriverServiceNotFoundException : The IEDriverServer.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://code.google.com/p/selenium/downloads/list.
    TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

    • October 11, 2013 at 9:46 am

      Looks like you need to add the path of IEDriverServer.exe in the PATH system variable. Can you give a try?

      • steveculshaw
        October 11, 2013 at 11:54 am

        I added the IEDriverServer.exe to the project with “Copy to Output Directory” set to “Copy Always” and that fixed it.

        However I did eventually have to put a copy in my PATH as NCrunch failed to find it when it ran the unit tests … unlike the R# test runner

  27. Vinod
    November 23, 2013 at 5:39 pm

    Hi Nazia Hasan,

    Could you please explain, how exactly you fixed the problem in debug tab.

    Please provide exact steps and file path as well as file name with extn which you set there.

    Thanks in advance


  28. Pritam M Maske
    February 19, 2014 at 6:26 am

    nice one

  29. Pierre
    February 24, 2014 at 5:02 am

    Thanks a bunch for this post, Shetty.
    When I execute the script (using Firefox) it worked until the wait.Until(d) line. I changed the time to 10 seconds, however the same error occurred.
    Below is copied from “Errors and Failures” in the NUnit window:
    at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
    at SeleniumProject.Demo.GoogleSearch() in c:\Users\Pierre_Morel\Documents\Visual Studio 2012\Projects\SeleniumProject\SeleniumProject\Demo.cs:line 49
    Any help will be greatly appreciated! Thanks

  30. March 14, 2014 at 6:43 pm

    Greetings I am so excited I found your blog, I really found
    you by error, while I was searching on Askjeeve for something else, Anyhow I am here now and would just like to say thanks for a remarkable post and
    a all round enjoyable blog (I also love the theme/design), I don’t
    have time to read through it all at the minute but I have bookmarked it and
    also included your RSS feeds, so when I have time
    I will be back to read more, Please do keep
    up the superb work.

  31. April 18, 2014 at 12:53 am

    I really like what you guys are up too. Such clever work and
    reporting! Keep up the fantastic works guys I’ve included
    you guys to my personal blogroll.

  32. May 14, 2014 at 8:32 am

    Very great post. I simply stumbled upon your weblog and wanted to
    say that I have really loved surfing around your weblog posts.

    In any case I’ll be subscribing for your feed and I hope you write
    once more soon!

  33. May 17, 2014 at 6:35 pm

    Love your step by step instructions. Very clear and helpful. I have a question – I want to setup a nightly build and regression environment for the team. Any pointers on how to configure the setup on server, clients?

  34. June 2, 2014 at 11:19 pm

    Hi my loved one! I wish to say that thi post is awesome, great
    wrirten and include almost all vital infos. I’d lijke to look
    more posts like this .

  35. July 7, 2014 at 2:23 am

    I’m very happy to discover this web site. I need to to thank you for your time
    just for this wonderful read!! I definitely liked every part of it
    and i aloso have you book-marked to see new information on your website.

  36. Kiran
    July 10, 2014 at 5:40 pm

    Hi Can some one please suggest me if selenium , c# works in Visual studio 2012 and with Nunit

  37. reddys
    July 17, 2014 at 9:56 am

    I am try to run same code but TimedOut error at wait command, i am keep on changing timings but getting same error every time .
    wait.Until((d) => { return d.Title.StartsWith(“selenium”); }); could explain me why i am getting this error ?

    • July 17, 2014 at 10:15 am

      Looks like page title doesn’t start with ‘selenium’. Try by entering the exact title.

  38. Sergy
    August 4, 2014 at 6:53 pm

    Anoop, the reason getting TimeOut during “wait” statement, that Word “Selenium” in the title starting from capital “S”, but in statement from lowercase “s” ..

  39. August 15, 2014 at 3:06 am

    Hello to all, how is the whole thing, I think every one is getting more from
    this website, and your views are pleasant in support of new visitors.

  40. August 20, 2014 at 10:26 am

    Right here is the perfect webpage for anybody who wishes
    to find out about this topic. You know a whole lot
    its almost hard to argue with you (not that I really will need to…HaHa).
    You certainly put a brand new spin on a topic that’s been discussed for ages.
    Great stuff, just great!

  41. Mark Griffith
    August 20, 2014 at 8:05 pm

    The title bar will reflect the same case as the search word. To resolve change:




    and this should fix it

  42. September 22, 2014 at 7:08 am

    Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for
    your next write ups thanks once again.

  43. Ravi
    October 3, 2014 at 4:01 pm

    In project properties under debug tab i cann’t see start project,start external program and start browser with url. I am using visual studio 2013 express.

  44. Ravi
    October 3, 2014 at 4:14 pm

    I can’t my Project Automation.dll file to run manually

  45. October 10, 2014 at 11:16 am

    I read this article completely concerning the resemblance of most recent and
    previous technologies, it’s amazing article.

  46. October 16, 2014 at 4:44 am

    This is really attention-grabbing, You are a very skilled blogger.

    I have joined your rss feed and stay up for in the hunt for more of your wonderful post.
    Also, I’ve shared your website in my social networks

  47. November 12, 2014 at 11:38 pm

    That was a nice post for the beginners Anoop. i could not find the WebDriverWait in my solution . am i missing any reference to be added. please suggest.

    • November 13, 2014 at 1:42 am

      Looks like you haven’t added the references – ‘using OpenQA.Selenium;’ and ‘using OpenQA.Selenium.Support.UI;’

  48. November 13, 2014 at 1:29 am

    Also please let me know how i can use/get the IE/Chrome webdriver rather than firefox.

    • November 13, 2014 at 1:40 am

      Current example is for Firefox browser where I am referencing using – “using OpenQA.Selenium.Firefox;”. You need to add the reference for IE/Chrome to run tests on those browsers.

  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: