简体   繁体   中英

Consecutive verse Parallel Nunit Testing

My office has roughly ~300 webpages that should be tested on a fairly regular basis. I'm working with Nunit, Selenium, and C# in Visual Studio 2010. I used this framework as a basis, and I do have a few working tests.

The problem I'm running into is is that when I run the entire suite. In each run, a random test(s) will fail. If they're run individually, they will all pass. My guess is that Nunit is trying to run all 7 tests at the same time and the browser can't support this for obvious reasons. Watching the browser visually, this does seem to be the case.

Looking at the screenshot below, I need to figure out a way in which the tests under Index_Tests are run sequentially, not in parallel.

在此输入图像描述

errors:

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"}

example with one test:

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");
        }
    }
}

NUnit typically does not run tests in parallel. Nevertheless using the same web driver for many tests, you risk to introduce unwanted dependencies between tests. The behaviour you describe looks like your browser executes some things in the background asynchronously after a test, which may sometimes affect the following test.

My suggestion: If setting up an individual driver for each test is too slow, try to bring your web browser (driver) in a defined initial state between the tests, and make sure any asynchronous tasks are done when each of the tests finishes. You can utilize [Setup] and/or [Teardown] methods for this. Maybe you have to add some waiting code after each test. I don't know much about Selenium, but having a short look into the documentation here , there is a WebDriverWait example which may be useful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM