Home > Agile, C#, Selenium, Test Automation > Data Driven Testing (DDT) using C#, Selenium WebDriver and NUnit

Data Driven Testing (DDT) using C#, Selenium WebDriver and NUnit

Data Driven Testing (DDT) is to verify many different test cases by driving the test from an external data source instead of using the same hard-coded values each time the test runs. This way, you can test how the application handles various input without having a lot of similar tests that only have different data sets.


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


Refer my previous blog on set-up instructions.


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


  • Create a sample spreadsheet using MS Excel and add some search text that needs to be searched.
  • Copy and paste the code snippet below into your Visual Studio 2010.
  • Add the reference to Microsoft.Office.Interop.Excel using .Net tab in the ‘Add Reference’ window.
  • Update the location of the spreadsheet in the code (i.e., in my case @”C:\Users\anoops\Desktop\GoogleTestData.xls”)
  • Run the tests using NUnit.
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;
using Excel = Microsoft.Office.Interop.Excel;

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()
   Excel.Application xlApp;
   Excel.Workbook xlWorkBook;
   Excel.Worksheet xlWorkSheet;
   Excel.Range range;

   string str;
   int rCnt = 0;
   int cCnt = 0;

   xlApp = new Excel.Application();
   //Opening Excel file
   xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\anoops\Desktop\GoogleTestData.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
   xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

   //Gives the used cells in the sheet
   range = xlWorkSheet.UsedRange;

   for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
      //Get the string from the sheet
      str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
      // Find the text input element by its name
      IWebElement query = driver.FindElement(By.Name("q"));
      // Convert the search string to lower case
      string lowerstr = str.ToLower();
      // Input the search string
      // 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));
      IWebElement title = wait.Until<IWebElement>((d) =>
        return d.FindElement(By.ClassName("ab_button"));

      //Check that the Title is what we are expecting
      //Console.WriteLine("Page Title is: " + driver.Title);
   xlWorkBook.Close(true, null, null);

  1. Ram
    November 7, 2012 at 5:46 pm

    Getting the following generic error when trying to use extract values from multiple columns in sheet, however script runs till end without problem with below error displayed in n unit instead of pass. I goggled it but could not get much info around.
    Any help will be appreciated.

    System.Runtime.InteropServices.COMException : Exception from HRESULT: 0x800A01A8


    • November 8, 2012 at 5:13 am

      I think this error is associated with Excel. Can you try your luck by killing the Excel Workbook and Excel app at the end of the test execution? [i.e., using xlWorkBook.Close(true, null, null) and xlApp.Quit() as in my example]

  2. Syed Faizul Hasan
    December 21, 2012 at 4:40 am

    Good Example. I would separate the reading of excel from the test and use the NUnit TestCaseSource attribute to provide data to the test. This will provide the benefits of using different data source at a later point of time, and will add individual NUnit tests for each test case data row.

    TestCaseSource attribute: http://www.nunit.org/index.php?p=testCaseSource&r=2.5.5

    • December 21, 2012 at 5:03 am

      Thanks for pointing on this. I will definitely have a look.

  3. Mahesh Upadhyay
    June 5, 2013 at 12:11 pm

    IWebElement title = wait.Until((d) =>
    In this scenario what do you mean by “d” here?

  4. Suresh
    July 13, 2014 at 1:41 pm

    Could you please mention the method to write the data to an excel sheet in the same format? It would be very helpful.

  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: