Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium web driver fails after the first test run and TestFixtureTearDown happens

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

And this is one of my tests

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

        }
}

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.

like image 209
Sophonias Avatar asked Jul 30 '12 15:07

Sophonias


1 Answers

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.

like image 67
Sophonias Avatar answered Oct 04 '22 04:10

Sophonias