第一次测试运行并发生TestFixtureTearDown后,Selenium Web驱动程序失败

时间:2012-07-30 15:58:01

标签: .net selenium nunit selenium-webdriver

我有多个功能测试编写为NUnit测试,这些测试彼此独立,并且当我一次运行一个时可以正常工作。但是,如果我选择所有测试并立即运行它们,我的Web驱动程序变量在执行第一次测试后崩溃。如果我采用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驱动程序变量。每次创建页面对象时都会实例化它。

1 个答案:

答案 0 :(得分:8)

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