尝试并行 运行 测试时如何正确传递 webdriver?
How can I properly pass webdriver around when trying to run tests in parallel?
我的工作要求我们编写一个自动化框架,其中包含带有 c#/nunit 的 selenium webdriver。我们目前正在使用 nunit 3.0。我最初的想法是提取足够多的框架内容,以便使用该框架的团队不必担心设置/拆除 webdriver 等问题。
我们决定采用测试助手 class 的方法,它具有设置/拆卸功能,可以根据配置设置(chrome,即等)实例化驱动程序,然后每次测试后将其撕下。我们还添加了一个受保护的 class 驱动程序变量,只要调用安装程序就会设置该变量。这样,每个测试 class 都可以扩展 TestHelper 并且他们可以免费获得驱动程序。
当我们没有运行并行测试时,这非常有效,但一旦我们开始进行并行测试,驱动程序每次都会被覆盖(这是有道理的)。
我的问题是,是否有更好的方法可以让我们这样做,从而保持最初的想法,即在 TestHelper 中提供驱动程序,并将大量驱动程序操作排除在测试本身之外。我真的不想在每个测试中都有驱动程序声明/初始化,只保留测试套件来测试特定代码。
这里有一些测试 class 类似于正在发生的事情(我实际上 post 生产代码因为工作很有趣):
这是我的测试助手class:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace AutomationTest
{
class TestHelper
{
protected IWebDriver driver;
[SetUp]
public void startBrowser()
{
driver = new ChromeDriver("C:\Users\james\Desktop");
}
[TearDown]
public void closeBrowser()
{
driver.Close();
driver.Quit();
}
}
}
这是我的测试 class(8 个测试只是为了测试并行测试):
using NUnit.Framework;
namespace AutomationTest
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
class ParallelTest : TestHelper
{
[Test]
public void test1()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test2()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test3()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test4()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test5()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test6()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test7()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test8()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
}
}
using NUnit.Framework;
namespace AutomationTest
{
class TestHelper
{
//protected IWebDriver driver;
protected void Browse(Action<IWebDriver> action)
{
var driver = new ChromeDriver("C:\Users\james\Desktop");
action(driver);
driver.Close();
driver.Quit();
}
[SetUp]
public void startBrowser()
{
// driver = new ChromeDriver("C:\Users\james\Desktop");
}
[TearDown]
public void closeBrowser()
{
// driver.Close();
// driver.Quit();
}
}
[TestFixture]
[Parallelizable(ParallelScope.All)]
class ParallelTest : TestHelper
{
[Test]
public void test1()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test2()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test3()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test4()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test5()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test6()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test7()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test8()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
}
}
这将为每个测试实例化一个新的驱动程序,这很好,因为您需要单独的浏览器驱动程序和浏览器实例,以便您的测试 运行 并行进行并且不产生干扰。
我的工作要求我们编写一个自动化框架,其中包含带有 c#/nunit 的 selenium webdriver。我们目前正在使用 nunit 3.0。我最初的想法是提取足够多的框架内容,以便使用该框架的团队不必担心设置/拆除 webdriver 等问题。
我们决定采用测试助手 class 的方法,它具有设置/拆卸功能,可以根据配置设置(chrome,即等)实例化驱动程序,然后每次测试后将其撕下。我们还添加了一个受保护的 class 驱动程序变量,只要调用安装程序就会设置该变量。这样,每个测试 class 都可以扩展 TestHelper 并且他们可以免费获得驱动程序。
当我们没有运行并行测试时,这非常有效,但一旦我们开始进行并行测试,驱动程序每次都会被覆盖(这是有道理的)。
我的问题是,是否有更好的方法可以让我们这样做,从而保持最初的想法,即在 TestHelper 中提供驱动程序,并将大量驱动程序操作排除在测试本身之外。我真的不想在每个测试中都有驱动程序声明/初始化,只保留测试套件来测试特定代码。
这里有一些测试 class 类似于正在发生的事情(我实际上 post 生产代码因为工作很有趣):
这是我的测试助手class:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace AutomationTest
{
class TestHelper
{
protected IWebDriver driver;
[SetUp]
public void startBrowser()
{
driver = new ChromeDriver("C:\Users\james\Desktop");
}
[TearDown]
public void closeBrowser()
{
driver.Close();
driver.Quit();
}
}
}
这是我的测试 class(8 个测试只是为了测试并行测试):
using NUnit.Framework;
namespace AutomationTest
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
class ParallelTest : TestHelper
{
[Test]
public void test1()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test2()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test3()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test4()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test5()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test6()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test7()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
[Test]
public void test8()
{
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
}
}
}
using NUnit.Framework;
namespace AutomationTest
{
class TestHelper
{
//protected IWebDriver driver;
protected void Browse(Action<IWebDriver> action)
{
var driver = new ChromeDriver("C:\Users\james\Desktop");
action(driver);
driver.Close();
driver.Quit();
}
[SetUp]
public void startBrowser()
{
// driver = new ChromeDriver("C:\Users\james\Desktop");
}
[TearDown]
public void closeBrowser()
{
// driver.Close();
// driver.Quit();
}
}
[TestFixture]
[Parallelizable(ParallelScope.All)]
class ParallelTest : TestHelper
{
[Test]
public void test1()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test2()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test3()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test4()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test5()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test6()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test7()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
[Test]
public void test8()
{
Browse( (driver) => {
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
});
}
}
}
这将为每个测试实例化一个新的驱动程序,这很好,因为您需要单独的浏览器驱动程序和浏览器实例,以便您的测试 运行 并行进行并且不产生干扰。