2

I am developing a testing framework for an application at my work and am running in to an issue. I use Selenium 3.0.1, NUnit 3.5.0, NUnit3TestAdapter 3.6.0, and a Selenium Grid instance to run my tests. In order to parallellize my tests properly I instantiate my webdriver instance at the start of each test (in the test itself, not in a setup method). I do this so that if multiple tests try to execute at the same time they are not trying to talk to the same static webdriver.

I am running in to an issue that when an Assert.IsTrue fails the Driver.Close() method (a wrapper for my driver instance close routine which executes the .Close() method on its own) directly after it does not execute because my code never makes it this far. I understand that this is because the the failing assertion causes the compiler to exit code execution and return a failing test.

My issue with this is that when my Driver.Close() method does not close sessions end up hanging on my grid nodes making them unable to effectively execute the next test in line. If I were using a static driver that I could simply pass around I would just execute the Driver.Close() routine in a teardown method however as I am instantiating my drivers at the start of each test this is not possible.

Does anyone have a way that given the constraints of my environment I can still kill the browser session on my grid node after an assertion fails on a test (perhaps some arcane way that I am unaware of to pass my instantiated RemoteDriver to a teardown method at the end of each test)?

My code is shown below for clarity:

An example of a test:

[Test]

public void GoToSearchPage()
{
    // Create an instance of the RemoteDriver object
    RemoteDriver remoteDriver = new RemoteDriver("firefox", "WINDOWS", "Hub_Location");

    // Go to the search page
    SearchPage.GoTo(remoteDriver);

    // Verify that the browser is at the search page
    Assert.IsTrue(SearchPage.IsAt(remoteDriver), "The web browser is not at the default search page");

    // Close the session
    remoteDriver.Close();
}

An example of my driver.Close method that lives inside the RemoteDriver class:

public void Close()
{
    // Close the currently open browser and dispose of the currently open instance
    Instance.Close();
    Instance.Dispose();
}

Edit

Below is the code implementation of the answer provided by ernie

public void GoToSearchPage()
{
    // Create an instance of the RemoteDriver object
    RemoteDriver remoteDriver = new RemoteDriver("chrome", "WINDOWS", "HUB_LOCATION");

    // Go to the search page
    SearchPage.GoTo(remoteDriver);
    try
    {
        // Verify that the browser is at the search page
        Assert.IsTrue(SearchPage.IsAt(remoteDriver), "The web browser is not at the default search page");
    }
    catch(AssertionException)
    {
        // Close the currently open browser session
        remoteDriver.Close();

        //Fail the test
        Assert.Fail("The web browser is not at the default search page");
    }

    // Close the currently open browser session
    remoteDriver.Close();

}

While this solution does introduce some slowness (approximately 2-3 seconds if the catch is activated) this allows me to avoid refactoring the way I implement my driver in order to use a setup or teardown method.

  • when you call driver.close it will close the session, so rather than asserting use verify or conditional statements like if else.or try and catch block. – sameer joshi Jan 9 '17 at 5:38
1

Simplest solution is a try-finally block .

At a higher level, you could write a decorator/annotation for this. Here's an SO answer in Java, and here's a blog post for C# showing how that might work.

  • This worked for me perfectly however after some testing I tweaked it a little and ended up using a Try-Catch block with an AssertionException inside the catch. When the AssertionException fires I close my browser session and then execute an Assert.Fail("Message Here"). – Isaiah Moran Jan 9 '17 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.