Monday, September 19, 2011

How to run Selenium WebDriver code with NUnit


In the last post I have shown you how to record and play back with Selenium IDE.

Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at visual studio 2010 with NUnit Framework.

Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and C# in NUnit Framework.

So If we know how to record Selenium IDE (see the post : then we can easily convert it to selenium WebDriver code.

Before starting we just define a Test case.

Test case :

    1. Open Selenium Website ( in Firefox browser.
    2. Click Project tab
    3. Click Selenium IDE link
    4. Verify that Selenium IDE Text is present


Step 1: Record the test case in Selenium IDE

  • Open Firefox Browser
  • Open Selenium IDE window by Clicking Tools->Selenium IDE
  • Make Red colored Record button on.
  • Enter the URL : at the address bar and click Search button
  • Click Project tab
  • Click Selenium IDE link
  • Verify that Selenium IDE Text is present

The recorded script will be like the following figure


Now make the record button off and Click Run button to run the script.

Step 2: Convert the Selenium IDE recorded script to C# WebDriver code

  • Go to Selenium IDE window
  • Click Options menu
  • Click Format –> C#(WebDriver).


Sometimes in Options –> Format menu, C#(WebDriver) options may not be displayed. The menu options like:


In that case, there is some change needed in selenium IDE option window. Please follow the steps:

  1. Click Options menu in Selenium IDE window
  2. Click “Options…” and Selenium IDE options window is displayed.



Click Enable experimental features check box and click OK button. Now go to Format options and driver list.


  • A message dialog is displayed after selecting the appropriate format like (C#(WebDriver))


  • Click OK button
  • Selenium IDE window shows the selenium WebDriver Code in C# format with unit testing Framework NUnit is implemented.



Step 2: Run the Code in visual Studio 2010

  • Open Visual Studio 2010 IDE
  • Create a New Class Library project by Clicking the options  File –>New –>Project and New Project Window is displayed
  • Select Visual C# from the template and project type Class Library


  • Enter Project name, Location and Solution Name and Select .Net Framework 4.0
  • Click OK button
  • New Project is created
  • Now Copy the whole text from Selenium IDE and paste into Class Library project editor. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy. Then remove all sample code from VS editor  and Paste CTRL+V to all code.
  • There are some Reference error show at editor. It could not find Open QA namespace. For this we need some .dll file from Selenium client driver to add as a reference.


  • Now go to selenium download page ( ) and download the latest version of Selenium Client Driver with C#. Here version 2.6.0 is downloaded.


  • Now unzip downloaded zip file and rename the folder as “Selenium”
  • Go to Solution explorer of IDE
  • Right click Reference and click Add Reference option. Add Reference window is displayed


  • Click Browse tab and go to Selenium folder. Inspect that there are 2 sub folder there – net35 and net40. net35 folder is for .Net framework 3.5 project and net40 for .Net framework 4.0 project. If the class library project is created as ,Net framework 3.5 then we select the .dll file of net35 folder. In this project .Net framework 4.0 is selected so we go to net40 folder
  • Select all the dlls  and click OK button and .dll files are added as reference



  • Now NUnit dll’s are needed for NUnit framework
  • Now download the latest NUnit form the site (
  • Download the latest NUnit with windows verison (2.5.10) and Install
  • Now right click on Reference and click Add Reference and go to NUnit folder where it is installed like (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework)
  • Select (nunit.framework.dll) file and click OK


  • Now all the references are added
  • Add the following code at the TheUntitledTest() method
Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
  • TheUntitledTest() method will be
  1. public void TheUntitledTest()
  2.       {
  3.           driver.Navigate().GoToUrl(baseURL);
  4.           driver.FindElement(By.LinkText("Projects")).Click();
  5.           driver.FindElement(By.LinkText("Selenium IDE")).Click();
  6.           Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
  7.           // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
  8.       }


  • Class1.cs code (whole WebDriver code) will be
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Threading;
  5. using NUnit.Framework;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.Firefox;
  9. namespace SeleniumTests
  10. {
  11.     [TestFixture]
  12.     public class Untitled
  13.     {
  14.         private IWebDriver driver;
  15.         private StringBuilder verificationErrors;
  16.         private string baseURL;
  18.         [SetUp]
  19.         public void SetupTest()
  20.         {
  21.             driver = new FirefoxDriver();
  22.             baseURL = "";
  23.             verificationErrors = new StringBuilder();
  24.         }
  26.         [TearDown]
  27.         public void TeardownTest()
  28.         {
  29.             try
  30.             {
  31.                 driver.Quit();
  32.             }
  33.             catch (Exception)
  34.             {
  35.                 // Ignore errors if unable to close the browser
  36.             }
  37.             Assert.AreEqual("", verificationErrors.ToString());
  38.         }
  40.         [Test]
  41.         public void TheUntitledTest()
  42.         {
  43.             driver.Navigate().GoToUrl(baseURL);
  44.             driver.FindElement(By.LinkText("Projects")).Click();
  45.             driver.FindElement(By.LinkText("Selenium IDE")).Click();
  46.             Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
  47.             // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
  48.         }
  49.         private bool IsElementPresent(By by)
  50.         {
  51.             try
  52.             {
  53.                 driver.FindElement(by);
  54.                 return true;
  55.             }
  56.             catch (NoSuchElementException)
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.     }
  62. }
  • Build the solution by right clicking on the solution name and Click build solution
  • Inspect that no Error message is displayed and build is succeeded.
  • Now open NUnit application editor by going Start – > Program – > NUnit 2.5.10 –> NUnit
  • Now go to File – > Open Project
  • Select the Class Library dll file from bin/debug folder of Class Library project
  • Click Run button
  • Firefox Browser is opened and Script runs and Test is successful.


That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand,  please comment me, I must answer. Please comment me about the whole blog.

In my next blog, I will show how we can run our selenium WebDriver code in Visual Studio Test Framework.


  1. Good post Indeed Nadim. Keep up the good work..

  2. "Select the Class Library dll file from bin/debug folder of Class Library project" in my visual c# 2010 express the Class Library dll file is in bin/Release folder, but everything else is ok :), thank you

  3. Hi Nadim,

    Thanks for the good post. I followed the steps that you have mentioned, but I am not able to run the test. It fails in NUnit. I am getting the following error

    SetUp : OpenQA.Selenium.WebDriverException : Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: OpenQA.Selenium.Platform
    TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

    I am using Windows 7 - 64 bit machine.

    Your response will be appreciated.

    A Tester

    1. This should help others in this situation:

      To copy/paste that:
      Either specify the folder (in which your Firefox binary is) in your PATH system variable - here's how.

      WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("path/to/your/firefox.exe")), profile);

      I hope this helps.

    2. Make sure you are using the 32 bit version of nunit. Firefox is a 32 bit browser.

      This issue happened to me, and I resolved it. I have a 64 bit windows OS, but Firefox is a 32 bit browser. I was trying to use the 64 bit version of nunit, which was giving this "Cannot fine firefox binary in PATH" error. I resolved this by using the 32 bit version of nunit. Basically, there are two exe files in the nunit folder, nunit.exe and nunit-x86.exe. If you are getting this "Cannot fine firefox binary in PATH" error, most likely you need to use the 32 bit version of nunit - the Nunit-x86.exe.

  4. Thanks for this post. Exactly what I was looking for.

  5. After installing NUnit and attempting to open your solution, you may find that attempting to run your tests returns an exception:

    Could not load file or assembly 'nunit.framework, Version=, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The system cannot find the file specified**

    If you see this, open Visual Studio Command Prompt and add the nunit.framework.dll file to your GAC manually with a line like the following:

    gacutil -/i "[pathOfYourFile]nunit.framework.dll"

    This will manually install the dll to your GAC and allow you to run Nunit tests. I hope this helps someone else who gets stuck on this problem the way I did!

  6. Hi,

    i tried the above code into the visual studio 2010. Its showing visual studio cannot start debugging error

  7. Hi Nadim,

    Nice post. But I face issue where in the Element is still not displayed to act upon. Hence it throws NoSuchElementException.How do i hadle that scenario? I mean the NUnit result file says-the test Passed even though Exception thrown, this is because all NUnit cares about is Assert Pass/Fail.I saw you mention-
    bool IsElementPresent(By by). Not sure, how & where its called?

    1. Thank you Chandas.

      I think element is not displayed in this case. you can use Wait for this purpose. Please this post :