Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap specflow scenario within try-catch

I have been running into a problem with the C# selenium bindings decribed here: Selenium Error - The HTTP request to the remote WebDriver timed out after 60 seconds

where a timeout exception is thrown occasionally while running my selenium tests. The tests are all written using spec flow, and I would love to have a way to catch that exception and have the test return as Inconclusive (instead of failed) for that particular exception.

Does anyone know of a way to wrap each spec flow scenario in a try catch block? Is that possible to do? Or a way to automatically catch this exception without having to wrap each individual step definition in a try catch block?

Thanks!

like image 512
GKS1 Avatar asked Dec 25 '22 03:12

GKS1


1 Answers

I found a workaround for this issue where I can check for the exception after each step using the [AfterStep] binding. This works for me, and tests that failed are now marked as inconclusive:

[AfterStep]
    public void check()
    {
        var exception = ScenarioContext.Current.TestError;
        if (exception is WebDriverException 
            && exception.Message.Contains("The HTTP request to the remote WebDriver server for URL "))
        {
            Assert.Inconclusive(exception.Message);
        }
    }

Hopefully someone finds this helpful!

like image 192
GKS1 Avatar answered Jan 03 '23 02:01

GKS1