【发布时间】:2017-07-04 13:11:33
【问题描述】:
我的问题是,当驱动程序在 FetchName 方法中设置名字时,我收到一条错误消息,指出驱动程序为空。我可以通过某种方式传递活动驱动程序实例以便继续获取数据吗?
[TestFixture]
public class TestBase
{
protected IWebDriver driverChrome;
[SetUp]
public void Setup()
{
driverChrome = new ChromeDriver();
}
[TearDown]
public void CleanSite()
{
driverChrome.Quit();
}
}
我在其中创建所有 [测试] 方法的“测试”类。
public void tests: Testbase
{
[Test]
public void testmethods()
{
string blabla = driverChrome.FindElement(By.id("dsd")).Text;
Reuse.FetchName(out string firstname, out string lastname);
Assert.isTrue(firstname.equals(lastname));
}
}
一个类“重用”,其中我有 [test] 方法将多次使用的方法。
public class Reuse: Testbase
{
[Test]
public void FetchName(out string firstname, out string lastname)
{
firstname = driverChrome.FindElement(By.XPath("/html/body/div[2]/table/tbody[last()]/tr/td[2]/div")).Text;
lastname = driverChrome.FindElement(By.XPath("/html/body/div[2]/table/tbody[last()]/tr/td[2]/div")).Text;
}
}
【问题讨论】:
-
你可以让你driverChrome作为一个全局变量或者写一个getter setter来获取驱动实例。
标签: c# google-chrome selenium-webdriver nunit