Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/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# 基于扩展方法的Selenium并行测试_C#_Selenium_Visual Studio 2012_Nunit 3.0 - Fatal编程技术网

C# 基于扩展方法的Selenium并行测试

C# 基于扩展方法的Selenium并行测试,c#,selenium,visual-studio-2012,nunit-3.0,C#,Selenium,Visual Studio 2012,Nunit 3.0,我已经将测试配置为在Selenium中与Nunit并行运行,这很好,但我不确定如何在不打开浏览器的第二个实例并破坏测试的情况下将自定义方法添加到混合中 我有一个基础: namespace ParallelTests { public class Base { public IWebDriver Driver { get; set; } } } …还有钩子: public class Hooks : Base { public Hooks() {

我已经将测试配置为在Selenium中与Nunit并行运行,这很好,但我不确定如何在不打开浏览器的第二个实例并破坏测试的情况下将自定义方法添加到混合中

我有一个基础:

namespace ParallelTests
{
   public class Base
   {
       public IWebDriver Driver { get; set; }
   }
}
…还有钩子:

public class Hooks : Base
{
   public Hooks()
   {
      Driver = new ChromeDriver(@"D:\Data\user\Documents\Visual Studio 2012\Projects\ParallelTests\ParallelTests\bin");
   }
}
…和单个测试文件:

[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
    [Test]
    public void ChromegGoogleTest()
    {
        Driver.Navigate().GoToUrl("https://www.google.co.uk");
        Driver.FindElement(By.Id("lst-ib")).SendKeys("Deep Purple");
        Driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
    }
}
运行此方法很好,但如果我添加了自定义方法,请说:

public class ExtensionMethods : Hooks
{
    public void assertDisplayed()
    {
        Assert.IsTrue(Driver.FindElement(By.XPath("//*[contains(text(),'Some Text')]")).Displayed);
    }
}
并在测试中调用
assertDisplayed()
,例如:

[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
  [Test]
  public void ChromegGoogleTest()
  {
    Driver.Navigate().GoToUrl("https://www.google.co.uk");
    Driver.FindElement(By.Id("lst-ib")).SendKeys("Deep Purple");
    Driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
    ExtensionMethods.assertDisplayed();
  }
}
当我在上面显示的测试中调用
assertdisplay()
时,它将启动第二个空白浏览器。非常感谢您的帮助

现在正在根据建议工作,但下面是一个页面对象模型的示例,它再次启动第二个浏览器窗口

页面文件:

namespace ParallelTests
{
class PageObject_LoggedIn : Hooks
{
  public PageObject_LoggedIn()
  {
      PageFactory.InitElements(Driver, this);
  }

  [FindsBy(How = How.XPath, Using = @"//*[contains(text(),'Deep Purple | Official Site')]")]
  public IWebElement SearchText = null;

    [FindsBy(How = How.Id, Using = "lst-ib")]
    public IWebElement SearchBox = null;

    public void Search()
    {
        SearchBox.SendKeys("Deep Purple");
        SearchBox.SendKeys(Keys.Enter);
        Driver.assertDisplayed2();
    }
}
[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
    [Test]
    public void ChromegGoogleTest()
    {
        PageObject_LoggedIn loggedIn = new PageObject_LoggedIn();

        Driver.Navigate().GoToUrl("https://www.google.co.uk");
        loggedIn.Search();
    }
}
}

…并在测试中调用。。。 测试代码:

namespace ParallelTests
{
class PageObject_LoggedIn : Hooks
{
  public PageObject_LoggedIn()
  {
      PageFactory.InitElements(Driver, this);
  }

  [FindsBy(How = How.XPath, Using = @"//*[contains(text(),'Deep Purple | Official Site')]")]
  public IWebElement SearchText = null;

    [FindsBy(How = How.Id, Using = "lst-ib")]
    public IWebElement SearchBox = null;

    public void Search()
    {
        SearchBox.SendKeys("Deep Purple");
        SearchBox.SendKeys(Keys.Enter);
        Driver.assertDisplayed2();
    }
}
[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
    [Test]
    public void ChromegGoogleTest()
    {
        PageObject_LoggedIn loggedIn = new PageObject_LoggedIn();

        Driver.Navigate().GoToUrl("https://www.google.co.uk");
        loggedIn.Search();
    }
}

好的,所以有一些事情你需要改变。扩展方法有一些我们需要遵循的规则。这些规则是:

  • 它必须在非泛型静态类中。因此,该方法必须是静态的,因为在静态类中不能有实例方法
  • 它必须有一个参数,第一个参数必须有
    这个
    关键字。第一个参数不能有
    out
    ref
  • 第一个参数不能是指针类型
  • 记住这些规则,让我们继续创建您需要的扩展方法

    namespace ParallelTests
    {
        public static class ExtensionMethods // I would call it ChromeDriverEntension
        {
            public static void AssertDisplayed(this IWebDriver driver)
            {
                Assert.IsTrue(driver.FindElement(By.XPath("//*[contains(text(),'Some Text')]")).Displayed);
            }
        }
    } 
    
    以上是一个非一般静态类。它有一个参数,第一个参数有这个关键字。第一个参数是IWebDriver,因为这是我们要扩展的。该方法也是静态的

    好的,让我们继续使用它

    namespace ParallelTests
    {
        public class Base
        {
            public IWebDriver Driver { get; set; }
        }
    
        public class Hooks : Base
        {
            public Hooks()
            {
                Driver = new ChromeDriver();
            }
        }
    
        [TestFixture]
        [Parallelizable]
        public class ChromeTesting : Hooks
        {
            [Test]
            public void ChromegGoogleTest()
            {
                Driver.Navigate().GoToUrl("https://www.google.co.uk");
                Driver.FindElement(By.Id("lst-ib")).SendKeys("Deep Purple");
                Driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
                Driver.AssertDisplayed();
            }
        }
    }
    
    编译器如何找到扩展方法?

    当编译器注意到类似实例方法的代码时,
    Driver.assertdisplay(),但没有满足签名的实例方法,然后它会查找扩展方法。它搜索所有名称空间以查找匹配项。由于此方法与上面的名称空间相同,因此它将找到它。如果它位于不同的名称空间中,则需要使用a.B
    导入该名称空间,其中a.B是扩展方法所在名称空间的名称。否则,编译器将生成一个错误,说明它找不到这样的方法


    Jon Skeet的C#indepth深入介绍了扩展方法,如果您想阅读更多内容。

    您如何调用assertDisplayed方法?我们能看到代码吗?很抱歉,是的,我现在添加了这个,谢谢。很抱歉,因为AssertDisplay不是静态方法,所以该代码甚至无法编译。发生这种情况是因为
    ExtensionMethods
    继承了创建新驱动程序的
    Hooks
    。谢谢,我对所有这些都是新手,但是当我这样做的时候,我得到了'ParallelTests.Base.Driver'是一个'property',但是像'type'一样使用,并且扩展方法必须是静态的。但这不起作用,因为“公共类ExtensionMethods:Hooks”需要是静态的,据我所知,它不能是静态的。您需要将ChromeDriver传递给它。谢谢,但正如我提到的,对于“公共类ExtensionMethods:Hooks”,我仍然得到了“扩展方法必须在非泛型静态类中定义”。我想您应该知道这一点。只需将ExtensionMethods设置为静态。我确实尝试过,但测试中显示的资产无法识别。