【问题标题】:Maintain context of Selenium WebDriver while running parallel tests in NUnit?在 NUnit 中运行并行测试时维护 Selenium WebDriver 的上下文?
【发布时间】:2018-03-27 23:03:47
【问题描述】:

使用:
C#
NUnit 3.9
Selenium WebDriver 3.11.0
Chrome WebDriver 2.35.0

在 NUnit 中运行并行测试时如何维护 WebDriver 的上下文?

  • 当我使用 ParallelScope.All 属性运行测试时,我的测试会重用驱动程序并失败

  • 我的测试中的 Test 属性不会在 [Setup] - [Test] - [TearDown] 中持续存在,除非 Test 被赋予更高的范围。

Test.cs

public class Test{
    public IWebDriver Driver;
    //public Pages pages;
    //anything else I need in a test

    public Test(){
        Driver = new ChromeDriver();
    }

    //helper functions and reusable functions
}

SimpleTest.cs

[TestFixture]
[Parallelizable(ParallelScope.All)]
class MyTests{

    Test Test;

    [SetUp]
    public void Setup()
    {
        Test = new Test();
    }

    [Test]
    public void Test_001(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_002(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_003(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_004(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }

    [TearDown]
    public void TearDown()
    {

        string outcome = TestContext.CurrentContext.Result.Outcome.ToString();
        TestContext.Out.WriteLine("@RESULT: " + outcome);
        if (outcome.ToLower().Contains("fail"))
        {
            //Do something like take a screenshot which requires the WebDriver
        }
        Test.Driver.Quit();
        Test.Driver.Dispose();
    }
}



  • The docs state:“SetUpAttribute 现在专门用于每次测试设置。”

  • 在 [Setup] 中设置 Test 属性似乎不起作用。

  • 如果这是一个时间问题,因为我正在重新使用 Test 属性。如何安排我的夹具,以便每次测试的驱动程序都是独一无二的?

  • 一种解决方案是将驱动程序放在 [Test] 中。但是,我不能使用 TearDown 方法,这是保持我的测试组织和清理所必需的。

  • 我已经阅读了很多帖子/网站,但都没有解决问题。 [Parallelizable(ParallelScope.Self)] 似乎是唯一真正的解决方案,这会减慢测试速度。

提前谢谢你!

【问题讨论】:

标签: selenium selenium-webdriver nunit


【解决方案1】:

ParallelizableAttribute 向 NUnit 承诺并行运行某些测试是安全的,但实际上它并没有做任何事情来使其安全。这取决于你,程序员。

您的测试(测试方法)具有共享状态,即字段Test。不仅如此,每个测试都会更改共享状态,因为每个测试都会调用SetUp 方法。这意味着您的测试可能无法安全地并行运行,因此您不应该告诉 NUnit 以这种方式运行它们。

您有两种方法...要么使用较低程度的并行性,要么使测试安全地并行运行。

使用较小程度的并行是最简单的。尝试在程序集上使用 ParallelScope.Fixtures 或在每个夹具上使用 ParallelScope.Self(默认)。如果您有大量独立的固定装置,这可能会为您提供与处理更复杂的事情一样好的吞吐量。

或者,要并行运行测试,每个测试都必须有一个单独的驱动程序。您必须创建它并在测试方法本身中处理它。

未来,NUnit 可能会添加一项功能,通过将每个测试方法隔离在一个单独的对象中来简化此操作。但是对于当前的软件,以上是你能做到的最好的。

【讨论】:

  • 感谢查理的快速和出色的回应!我认为我的选择有限。我只需要将测试分解为不同的夹具。那可行!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2017-05-25
  • 1970-01-01
相关资源
最近更新 更多