Selenium Web驱动程序在第一次测试运行并发生TestFixtureTearDown后失败 [英] Selenium web driver fails after the first test run and TestFixtureTearDown happens

查看:65
本文介绍了Selenium Web驱动程序在第一次测试运行并发生TestFixtureTearDown后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个写为NUnit测试的功能测试,它们相互独立,并且一次运行一个就可以正常工作.但是,如果我选择所有测试并立即运行它们,则我的Web驱动程序变量将在执行第一个测试后崩溃.如果我使用TestFixtureTearDown方法,则所有测试都将运行,但最终会导致许多打开的浏览器.我已经尝试在TearDown中使用Quit()和Close()方法.如何编写一个TearDown方法,该方法在每次测试运行后关闭浏览器,但不会使整个测试崩溃?我非常需要您的帮助,因此请提出任何可行的建议,我愿意尝试.这是我在测试运行后收到的错误.

I have multiple functional tests written as NUnit test which are independent from each other and work fine when I run them one at a time. But if I select all the tests and run them at once, my web driver variable crashes after it executes the very first test. If I take the TestFixtureTearDown method all the tests run but I will end up with a lot of open browsers. I have already tried using Quit() and Close() methods inside the TearDown. How can I write a TearDown method which closes the browser after each test run but doesn't crash the whole test? I am in a desperate need of your help so please suggest anything that might work I am open to trying it. This is the error I get after the test run.

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.

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

This is my abstract class where all my other tests inherit from

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驱动程序变量.每次我创建页面对象时都会实例化它.

If anyone is wondering about what components are its just a class I created to handle all the user and web driver variables needed for my web app to run. It is instantiated every time I create a page object.

推荐答案

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

I finally figured out my problem. My method "Browser.GetBrowser(driverName)" (which I haven't worked on) where I set up the browser driver was not creating a new instance of the browser. Instead, it was reusing the initially created browser. Hence, the crash after the browser is used and Tore down in the first test. Having the Browser.GetBrowser(driverName) method create a new instance of the IWebDriver inside the SetUp method of the NUnit test shall solve the problem.

这篇关于Selenium Web驱动程序在第一次测试运行并发生TestFixtureTearDown后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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