使用扩展方法进行 Selenium 并行测试 [英] Selenium Parallel testing with extension methods

查看:25
本文介绍了使用扩展方法进行 Selenium 并行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have configured tests to run in parallel in Selenium with Nunit which works fine but I'm not sure how to add a custom method into the mix without a second instance of the browser opening and breaking the test.

我有基地:

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

...和一个测试文件:

...and a single test file:

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

运行这个工作正常,但如果我添加自定义方法,请说:

Running this works fine, but if I add a custom method, say:

public class ExtensionMethods : Hooks
{
    public void assertDisplayed()
    {
        Assert.IsTrue(Driver.FindElement(By.XPath("//*[contains(text(),'Some Text')]")).Displayed);
    }
}

并在测试中调用 assertDisplayed() 如:

and call assertDisplayed() in the test such as:

[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();
  }
}

当我在上面显示的测试中调用 assertDisplayed() 时,它将启动第二个空白浏览器.非常感谢任何帮助.

It will launch a second blank browser when I call assertDisplayed() in the test shown above. Any help much appreciated.

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

Now working based on suggestions but below is an example with page object model which again launches a second browser window...

页面文件:

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

}

...并调用测试...测试代码:

...and calling in the test... Test code:

[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();
    }
}

推荐答案

好的,您需要更改一些内容.扩展方法有一些我们需要遵循的规则.规则是:

Ok so there are a few things you need to change. Extension methods have a few rules which we need to follow. The rules are:

  1. 它必须在非通用静态类中.因此该方法必须是静态的,因为静态类中不能有实例方法.
  2. 必须有一个参数,第一个参数必须有this关键字.第一个参数不能有 outref.
  3. 第一个参数不能是指针类型.
  1. It must be in a non generic static class. So the method must be static since you cannot have instance methods in a static class.
  2. It must have one parameter and the first parameter must have this keyword. The first parameter cannot have out or ref.
  3. The first parameter cannot be a pointer type.

记住这些规则,让我们继续创建您需要的扩展方法.

So keeping those rules in mind, let's go ahead and create the extension method you need.

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

以上是一个非泛型的静态类.它有一个参数,第一个参数有 this 关键字.第一个参数是 IWebDriver 因为这是我们正在扩展的.该方法也是静态的.

The above is a nongeneric static class. It has one parameter and the first parameter has this keyword. The first parameter is IWebDriver since this is what we are extending. The method is also static.

好的,让我们继续使用它.

Ok let's go ahead and use it.

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.AssertDisplayed();,但没有满足签名的实例方法时,它会寻找扩展方法.它搜索所有名称空间以找到匹配项.由于这个方法在上面相同的命名空间中,它会找到它.如果它在不同的命名空间中,则需要使用 using A.B 导入该命名空间,其中 A.B 是扩展方法所在的命名空间的名称.否则,编译器会产生一个错误,说它找不到这样的方法.

When the compiler notices code which looks like instance method, Driver.AssertDisplayed();, but there is no instance method that satisfies the signature, then it looks for an extension method. It searches all the namespaces to find a match. Since this method is in the same namespace above, it will find it. If it was in a different namespace, you would need to import that namespace with using A.B where A.B is the name of the namespace where the extension method is. Otherwise, the compiler will generate an error saying it cannot find such a method.

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

C# in Depth by Jon Skeet covers extension methods in depth if you want to read more.

这篇关于使用扩展方法进行 Selenium 并行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆