从C#NUnit一个接一个地在多个浏览器中运行Selenium测试

Edg*_*gar 30 c# selenium nunit

我正在寻找推荐/最好的方法让Selenium测试一个接一个地在几个浏览器中执行.我正在测试的网站并不大,所以我还不需要并行解决方案.

我有一个通常的测试设置方法[SetUp],[TearDown][Test].当然,SetUp可以ISelenium使用我想要测试的任何浏览器来实例化一个新对象.

所以我想要的是以编程方式说:这个测试将按顺序在Chrome,IE和Firefox上运行.我怎么做?

编辑:

这可能会有所帮助.我们正在使用CruiseControl.NET在成功构建之后启动NUnit测试.有没有办法将参数传递给NUnit可执行文件,然后在测试中使用该参数?这样我们就可以使用不同的浏览器参数多次运行NUnit.

ala*_*ing 50

NUnit 2.5+现在支持通用测试夹具,这使得在多个浏览器中进行测试变得非常简单. http://www.nunit.org/index.php?p=testFixture&r=2.5

运行以下示例将执行两次GoogleTest,一次在Firefox中,一次在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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


pne*_*ook 5

这是一个反复出现的问题,有两种解决方法:

  1. Factory方法生成您的ISelenium对象 - 您有一个带有静态getSelenium方法的帮助器类.该方法读入一些外部配置,该配置具有将您想要的浏览器定义为字符串的属性.在getSelenium中,然后相应地配置浏览器.这里使用与NUnit的配置文件,方便的后http://blog.coryfoy.com/2005/08/nunit-app-config-files-its-all-about-the-nunit-file/

  2. 其他人通过IoC容器注入浏览器取得了成功.我真的很喜欢这个,因为TestNG在Java领域与Guice的合作非常好,但我不确定混合NUnit和Ninject,MEF等是多么容易......


Arv*_*tad 5

这基本上只是对阿兰宁答案的扩展(2011年10月21日20:20).我的情况类似,只是因为我不想使用无参数构造函数运行(因此使用驱动程序可执行文件的默认路径).我有一个单独的文件夹,其中包含我想要测试的驱动程序,这似乎很好用:

[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class BrowserTests<TWebDriver> where TWebDriver : IWebDriver, new()
{
    private IWebDriver _webDriver;

    [SetUp]
    public void SetUp()
    {
        string driversPath = Environment.CurrentDirectory + @"\..\..\..\WebDrivers\";

        _webDriver = Activator.CreateInstance(typeof (TWebDriver), new object[] { driversPath }) as IWebDriver;
    }

    [TearDown]
    public void TearDown()
    {
        _webDriver.Dispose(); // Actively dispose it, doesn't seem to do so itself
    }

    [Test]
    public void Tests()
    {
        //TestCode
    }
}
Run Code Online (Sandbox Code Playgroud)

}