连续经文并行Nunit测试

时间:2013-11-06 16:32:51

标签: c# selenium nunit

我的办公室大约有300个网页,应该定期进行测试。我正在使用Visual Studio 2010中的Nunit,Selenium和C#。我使用这个framework作为基础,我确实有一些工作测试。

我遇到的问题是当我运行整个套件时。在每次运行中,随机测试都将失败。如果它们单独运行,它们都会通过。我的猜测是Nunit试图同时运行所有7个测试,并且浏览器由于显而易见的原因无法支持这一点。在视觉上观察浏览器,情况似乎确实如此。

查看下面的屏幕截图,我需要找出一种方法,其中Index_Tests下的测试按顺序运行,而不是并行运行。

enter image description here

错误:

Selenium2.OfficeClass.Tests.Index_Tests.index_4:
OpenQA.Selenium.NoSuchElementException : Unable to locate element: "method":"id","selector":"textSelectorName"}

Selenium2.OfficeClass.Tests.Index_Tests.index_7:
OpenQA.Selenium.NoSuchElementException : Unable to locate element: "method":"id","selector":"textSelectorName"}

一个测试的例子:

using OpenQA.Selenium;
using NUnit.Framework;

namespace Selenium2.OfficeClass.Tests
{
    [TestFixture]
    public class Index_Tests : TestBase
    {
        public IWebDriver driver;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            driver = StartBrowser();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            driver.Quit();
        }

        [Test]
        public void index_1()
        {
            OfficeClass index = new OfficeClass(driver);
            index.Navigate("http://url_goeshere");
            index.SendKeyID("txtFiscalYear", "input");
            index.SendKeyID("txtIndex", "");
            index.SendKeyID("txtActivity", "input");
            index.ClickID("btnDisplay");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

NUnit通常会并行运行测试。然而,对于许多测试使用相同的Web驱动程序,您可能会在测试之间引入不需要的依赖项。您描述的行为看起来像您的浏览器在测试后异步地在后台执行某些操作,这有时会影响以下测试。

我的建议:如果为每个测试设置单独的驱动程序太慢,请尝试在测试之间将Web浏览器(驱动程序)置于定义的初始状态,并确保在每个测试时完成任何异步任务饰面。您可以使用[Setup]和/或[Teardown]方法。也许你必须在每次测试后添加一些等待代码。我对Selenium了解不多,但仔细查看文档here,有一个WebDriverWait示例可能对您有用。