Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在类之间传递对活动驱动程序的引用_C#_Google Chrome_Selenium Webdriver_Nunit - Fatal编程技术网

C# 如何在类之间传递对活动驱动程序的引用

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

我的问题是,当firstname假设由FetchName方法中的驱动程序设置时,我会收到一条错误消息,表明驱动程序为null。我能否以某种方式传递活动驱动程序实例,以便继续获取数据

[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
  • 如果您想要一个包含多个动作的类,它应该是静态类
  • WebDriver是一个独立的进程。您可以使用多个类或多个进程来访问它。他们都将获得相同的WebDriver
  • 下面的示例显示了如何更改
    重用
    类以及如何使用它

    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; 
        }
    }