8

I am trying to get data driven testing to work in C# with MSTest/Selenium. Here is a sample of some of my code trying to set it up:

[TestClass]
public class NewTest
{
    private ISelenium selenium;
    private StringBuilder verificationErrors;
    [DeploymentItem("GoogleTestData.xls")]
    [DataSource("System.Data.OleDb",
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=GoogleTestData.xls;Persist Security Info=False;Extended Properties='Excel 8.0'",
    "TestSearches$", DataAccessMethod.Sequential)]

    [TestMethod]
    public void GoogleTest()
    { 
        selenium = new DefaultSelenium("localhost", 4444, "*iehta", http://www.google.com);
        selenium.Start();
        verificationErrors = new StringBuilder();
        var searchingTerm = TestContext.DataRow["SearchingString"].ToString();
        var expectedResult = TestContext.DataRow["ExpectedTextResults"].ToString();

    ...

Here's my error: Error 3 An object reference is required for the non-static field, method, or property 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataRow.get' E:\Projects\SeleniumProject\SeleniumProject\MaverickTest.cs 32 33 SeleniumProject

The error is underlining the "TestContext.DataRow" part of both statements. I've really been struggling with this one, thanks!

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
Jacob
  • 93
  • 1
  • 1
  • 4

1 Answers1

14

try:

public TestContext TestContext { get; set; }

and try using it like:

this.TestContext.DataRow["SearchingString"].ToString();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • Thanks for the response! I tried your code additions and it is telling me it cannot apply indexing with [] to an expression of type 'System.Data.DataRow' do you suppose that is an issue with my setup to the spreadsheet? I'm brand new to c# if you couldn't tell! – Jacob Jun 08 '10 at 14:40
  • Nevermind...got it working.. you were dead on with the TestContext method thanks! This is how I had to use it to get it to work: searchTerm = System.Convert.ToString(TestContext.DataRow["SearchingString"]); – Jacob Jun 09 '10 at 19:08
  • This helped me when I was using the same csv for multiple tests in the same class. See http://geekswithblogs.net/Aligned/archive/2013/11/04/selenium-ndash-use-data-driven-tests-to-run-in-multiple.aspx for more details. Thanks! – AlignedDev Nov 05 '13 at 15:46