如何用selenium和一个NUnit套件测试多个浏览器并保持DRY? [英] How do I test multiple browsers with selenium and a single NUnit suite and keep it DRY?
问题描述
我正在寻找一种方法来重用一个NUnit测试套件,而不需要为每个浏览器复制整个套件。看起来我需要一个新的夹具为每个浏览器。我可以从NUnit gui发送某种环境变量或配置设置来切换浏览器吗?见下面:
I'm looking for a way to reuse one NUnit test suite without duplicating the entire suite for each browser. It seems like I would need a new fixture for each browser. Can I send some sort of environment variable or configuration setting from the NUnit gui to switch the browser? see below:
[TestFixture]
public class User
{
private ISelenium selenium;
private StringBuilder verificationErrors;
[SetUp]
public void SetupTest()
{
// TheBrowser = How do I populate this variable from the NUnit gui?
selenium = new DefaultSelenium("localhost", 4444, **TheBrowser**, "http://localhost:52251/");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
...
}
[Test]
public void SearchUser()
{
...
}
}
推荐答案
NUnit 2.5+支持通用测试工具,使得测试在多个浏览器中非常简单。
http://www.nunit.org/index.php?p=testFixture& r = 2.5
NUnit 2.5+ supports Generic Test Fixtures which make testing in multiple browsers very straightforward. http://www.nunit.org/index.php?p=testFixture&r=2.5
构建以下将创建两个GoogleTestNUnit测试,一个用于Firefox,一个用于IE。
Building the following will create two "GoogleTest" NUnit tests, one for Firefox and one for IE.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System.Threading;
namespace SeleniumTests
{
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()
{
private IWebDriver driver;
[SetUp]
public void CreateDriver () {
this.driver = new TWebDriver();
}
[Test]
public void GoogleTest() {
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Bread" + Keys.Enter);
Thread.Sleep(2000);
Assert.AreEqual("bread - Google Search", driver.Title);
driver.Quit();
}
}
}
这篇关于如何用selenium和一个NUnit套件测试多个浏览器并保持DRY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!