5

我有多个功能测试编写为 NUnit 测试,它们彼此独立,并且在我一次运行它们时工作正常。但是,如果我选择所有测试并立即运行它们,我的网络驱动程序变量在执行第一个测试后就会崩溃。如果我采用 TestFixtureTearDown 方法,所有测试都会运行,但我最终会得到很多打开的浏览器。我已经尝试在 TearDown 中使用 Quit() 和 Close() 方法。如何编写一个 TearDown 方法,在每次测试运行后关闭浏览器但不会使整个测试崩溃?我迫切需要你的帮助,所以请提出任何可能有用的建议,我愿意尝试。这是我在测试运行后得到的错误。

AFT.AministratorPageTest("firefox").SuperAdminAssignsPermissionsOfAdmin-catalyst:
  OpenQA.Selenium.WebDriverException : Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:7055
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Firefox.Internal.ExtensionConnection.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
TearDown : System.InvalidOperationException : No process is associated with this object.

这是我的抽象类,我的所有其他测试都继承自

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support;


namespace BusinessLayer
{
    [TestFixture("ie")]
    [TestFixture("firefox")]
     public abstract class BaseTest 
    {
        public IWebDriver browser { get; set; }
        public String driverName;

        /// <summary>
        /// Required No Argument Constructor
        /// </summary>
        public BaseTest()
        { }

        /// <summary>
        /// Constructor to allow for TestFixture parameterization
        /// </summary>
        /// <param name="name"></param>
        public BaseTest(string name)
        { 
            this.driverName = name; 
        }

        /// <summary>
        /// Loads Browser into the TestFixture
        /// </summary>
        [TestFixtureSetUp]
        public void CreateDriver()
        {
            if (driverName != null)
            {
                this.browser = (IWebDriver)Browser.GetBrowser(driverName);
            }
            else
            {
                throw new Exception("DriverName cannot be null");
            }
        }

        /// <summary>
        /// Insures browser is destroyed at conclusion of test
        /// </summary>
        [TestFixtureTearDown]
        public void FlushBrowser()
        {
            browser.Quit();
            browser = null;
        }
    }
}

这是我的测试之一

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenQA.Selenium;
using NUnit.Framework;
using BusinessLayer;
using BusinessLayer.Pages;
using System.Threading;


namespace Pegged_AFT
{
    class ScreeningProcessTests : BaseTest
    {
        public ScreeningProcessTests()
            : base()
        { }

        public ScreeningProcessTests(string name)
            : base(name)
        { }

       [Test]
        public void TestHappyPathToRegistration()
        {
            User user = new User().GetCandidate();

            Components components = new Components(
                    browser: Browser.GetBrowser(driverName),
                    client: new Client("test"),
                    user: user,
                    credentials: new Credentials(user.emailAddress, user.password)
                    );

            AddUserPage addUser = new AddUserPage(components);
            addUser.AddUser(user);

            Screening screening = new Screening(components);
            screening.Registration();

            screening.InitPage(new TestPage(components));
            Assert.AreEqual(screening.testPage.TryToFindElement(By.Id("ctl00_ContentPlaceHolder1_lblSectionName")).Text, "Candidate Registration");

        }
}

如果有人想知道哪些组件只是我创建的一个类,用于处理我的 Web 应用程序运行所需的所有用户和 Web 驱动程序变量。每次创建页面对象时都会实例化它。

4

1 回答 1

8

我终于弄清楚了我的问题。我设置浏览器驱动程序的方法“Browser.GetBrowser(driverName)”(我没有使用过)没有创建浏览器的新实例。相反,它重用了最初创建的浏览器。因此,使用浏览器后的崩溃并在第一次测试中被撕毁。让 Browser.GetBrowser(driverName) 方法在 NUnit 测试的 SetUp 方法中创建 IWebDriver 的新实例将解决问题。

于 2012-08-02T18:24:34.153 回答