从 C# NUnit 一个接一个地在多个浏览器中运行 Selenium 测试 [英] Run Selenium tests in multiple browsers one after another from C# NUnit

查看:31
本文介绍了从 C# NUnit 一个接一个地在多个浏览器中运行 Selenium 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for the recommended/nicest way to make Selenium tests execute in several browsers one after another. The website I'm testing isn't big, so I don't need a parallel solution yet.

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

I have the usual test set-up methods with [SetUp], [TearDown], and [Test]. The SetUp one, of course, instantiates a new ISelenium object with whatever browser I want to test with.

所以我想做的就是以编程方式说:这个测试会依次在 Chrome、IE 和 Firefox 上运行.我该怎么做?

So what I want to do is programmatically say: this test will be run on Chrome, IE, and Firefox in sequence. How do I do that?

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

This might help a bit. We're using CruiseControl.NET to start the NUnit tests after a successful build. Is there any way to pass a parameter to the NUnit executable, and then use that parameter in the test? This way we could have NUnit run several times with different browser parameters.

推荐答案

NUnit 2.5+ 现在支持 Generic Test Fixtures,这使得在多个浏览器中的测试变得非常简单.http://www.nunit.org/index.php?p=testFixture&r=2.5

NUnit 2.5+ now supports Generic Test Fixtures which make testing in multiple browsers very straightforward. http://www.nunit.org/index.php?p=testFixture&r=2.5

运行以下示例将执行 GoogleTest 两次,一次在 Firefox 中,一次在 IE 中.

Running the following example will execute the GoogleTest twice, once in Firefox and once in 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();
        }
    }
}

这篇关于从 C# NUnit 一个接一个地在多个浏览器中运行 Selenium 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆