【发布时间】:2016-04-19 20:13:25
【问题描述】:
我是 Selenium 的新手,我正在尝试通过 CSV 文件执行数据驱动测试。为此,我在包含测试属性的类中定义 DataSource 属性。我正在使用 MStest 框架。
[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 是我用来存储实际 webdriver 的类。 PageElements 是我在其中声明了所有 Web 元素的类。
我正在尝试在单独的类中定义“DDtest_usingCSV”方法中的变量,以便测试不会变得笨拙。但是每当我在另一个类中定义另一个测试上下文时,我都会得到一个 NullReferenceException。我试过在类之间传递属性。但我做不到(我还在学习)。
下面是我试图初始化 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();
}
}
由于我无法初始化该属性,因此我在测试类文件中对该方法进行了参数化。
在声明如下所述的 TestContext 属性时,为什么我们使用“TestContext”作为属性名称而不是实例?
private TestContext instance;
public TestContext TestContext
{
set { instance = value; }
get { return instance; }
}
在从 excel 读取值时,我们使用 "TestContext" 来访问 DataRow 而不是 "instance" 。每当我看到这个问题时,我都会感到烦恼。
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);
}
请不要介意问题的长度。我详细解释了我的问题。任何帮助都将不胜感激。提前致谢。
【问题讨论】: