Search code examples
c#seleniummstest

How can I initialize TestContext property in another class?


I am new to Selenium and I am trying to perform a data-driven test through CSV file. For this I am defining DataSource attribute in a class which contains test attributes. I am using MStest framework.

[TestClass]
public class UnitTest1:BaseDriver
{

    ExcelTest sd;

    private TestContext instance;

    public TestContext TestContext
    {
        set { instance = value; }
        get { return instance; }
    }

    public UnitTest1()
    {

        sd = new ExcelTest(_driver);              
    }

    [TestInitialize]
    public void Testinitialize()
    {

    }

    [TestMethod]
    [DeploymentItem("TestData.csv")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"C:\Users\nidumukv\Documents\Visual Studio 2012\Projects\BMICalculator\BMICalculator\DataFiles\TestData.csv", "TestData#csv", DataAccessMethod.Sequential)]
    public void DDtest_usingCSV()
    {
        string feet = TestContext.DataRow["feet"].ToString();
        string inches = TestContext.DataRow["inches"].ToString();
        string weight = TestContext.DataRow["weight in pounds"].ToString();
        string BMI = TestContext.DataRow["BMI"].ToString();


        sd.TestUsingCSV(feet,inches,weight,BMI);
    }

    [TestCleanup]
    public void cleanup()
    { _driver.Quit(); }


}

BaseDriver is a class I am using to store the actual webdriver. PageElements is a class in which I have declared all the web elements.

I am trying to define the variables that are in 'DDtest_usingCSV' method in a seperate class so that the test does not become clumsy. But whenever I am defining another testcontext in another class I am getting a NullReferenceException. I have tried passing the property between classes. But I could not do it (I am still learning).

Below is the class I am trying to initialize the TestContext

 public class ExcelTest:PageElements
{
    public IWebDriver _driver;

    public ExcelTest(IWebDriver driver):base(driver)
    {
        _driver = driver;

    }


    public void TestUsingCSV(string _feet,string _inches,string _weight,string _BMI)
    {
        feet.SendKeys(_feet);
        inches.SendKeys(_inches);
        weight.SendKeys(_weight);
        compute_btn.Click();
    }
}

As I could not initialize the property, I am parameterizing that method in the test class file.

And while declaring the TestContext property as mentioned below, why are we using "TestContext" as property name instead of instance??

private TestContext instance;
public TestContext TestContext
    {
        set { instance = value; }
        get { return instance; }
    }

At the time of reading values from excel, we are taking "TestContext" for accessing DataRow instead of "instance" . This question is bugging me whenever I look at it.

public void DDtest_usingCSV()
    {
        string feet = TestContext.DataRow["feet"].ToString();
        string inches = TestContext.DataRow["inches"].ToString();
        string weight = TestContext.DataRow["weight in pounds"].ToString();
        string BMI = TestContext.DataRow["BMI"].ToString();


        sd.TestUsingCSV(feet,inches,weight,BMI);
    }

Please don't mind the length of the question. I gave a detailed explanation on my problem. Any help can be appreciated. Thanks in advance.


Solution

  • TestContext is set automatically by the MSTest framework but only in the class attributed with [TestClass] and when it executes a test from this class.

    In you case, just pass TestContext as a parameter in the TestUsingCSV() method of the ExcelTest class.