C# 如何在类之间传递对活动驱动程序的引用
我的问题是,当firstname假设由FetchName方法中的驱动程序设置时,我会收到一条错误消息,表明驱动程序为null。我能否以某种方式传递活动驱动程序实例,以便继续获取数据C# 如何在类之间传递对活动驱动程序的引用,c#,google-chrome,selenium-webdriver,nunit,C#,Google Chrome,Selenium Webdriver,Nunit,我的问题是,当firstname假设由FetchName方法中的驱动程序设置时,我会收到一条错误消息,表明驱动程序为null。我能否以某种方式传递活动驱动程序实例,以便继续获取数据 [TestFixture] public class TestBase { protected IWebDriver driverChrome; [SetUp] public void Setup() { driverChrome = new ChromeDriver
[TestFixture]
public class TestBase
{
protected IWebDriver driverChrome;
[SetUp]
public void Setup()
{
driverChrome = new ChromeDriver();
}
[TearDown]
public void CleanSite()
{
driverChrome.Quit();
}
}
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;
}
}
类“Tests”,我在其中创建所有[test]方法
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));
}
}
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;
}
}
一个类“重用”,其中我有[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;
}
}
重用
类不是测试用例。将属性[Test]
添加到方法时,该方法应包含断言。因此,重用
不应该继承自Testbase
重用
类以及如何使用它
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;
}
}
public static class Reuse
{
public static IWebDriver driverChrome;
public static 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;
}
}
你可以这样称呼它
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;
}
}
public void tests: Testbase
{
[Test]
public void testmethods()
{
string blabla = driverChrome.FindElement(By.id("dsd")).Text;
Reuse.driverChrome = driverChrome;
Reuse.FetchName(out string firstname, out string lastname);
Assert.isTrue(firstname.equals(lastname));
}
}可以将driverChrome设置为全局变量,也可以编写getter setter来获取驱动程序实例。
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;
}
}