【发布时间】: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)] 似乎是唯一真正的解决方案,这会减慢测试速度。
提前谢谢你!
【问题讨论】:
-
哈哈,重复... 转了一圈! stackoverflow.com/questions/43988524/…
标签: selenium selenium-webdriver nunit