我的工作要求我们编写一个自动化框架,其中包含带有 c#/nunit 的 selenium webdriver。我们目前使用的是 nunit 3.0。我最初的想法是提取足够的框架内容,以便使用该框架的团队不必担心诸如设置/拆除 webdriver 之类的事情。
我们决定采用具有设置/拆卸功能的测试助手类的路线,该类将根据配置设置(chrome,ie 等)实例化驱动程序,然后在每次测试后将其拆卸。我们还添加了一个受保护的类驱动程序变量,每当调用 setup 时都会设置该变量。这样,每个测试类都可以扩展TestHelper,并且可以免费获得驱动程序。
当我们不并行运行测试时,这非常有效,但是一旦我们开始进行并行测试,驱动程序每次都会被覆盖(这是有道理的)。
我的问题是,是否有更好的方法可以做到这一点,以便它保持在 TestHelper 中提供驱动程序的原始想法,并将大量驱动程序操作排除在测试本身之外。我真的不想在每个测试中都有驱动程序声明/初始化,而只是保留测试套件来测试特定的代码。
以下是一些类似于正在发生的事情的测试类(我实际上无法发布生产代码,因为工作很有趣):
这是我的测试助手类:
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();
}
}
}
这是我的测试类(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");
});
}
}
}
这将为每个测试实例化一个新驱动程序,这很好,因为您需要单独的浏览器驱动程序和浏览器实例,以便您的测试并行运行而不会干扰。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句