Search code examples
javaseleniumexceptionassertthucydides

Best practice on exception and error handling with Selenium and SerenityBDD (thucydides)


Actually I'm writing a bunch of tests but I'm not sure how I should handle exceptions/errors best.

There a different types of exceptions, e.g. AssertException if a result was not as expected using assertThat(..). This is O.K. and understandable.

But what if I have a FileNotFound / SOAPException / DOMException and so on...?

For example in my @BeforeStory method I create some testdata by reading testfiles and sending them to a webservice and there I could possibly get the above mentioned exceptions. I would like to present these errors by using an own error message also in the living documentation. But how should I manage this? Actually I'm thinking about two approaches:

1.) I catch the exception and throw my own new exception with an individual error message. The testexecution is aborted for the scenario and the exception is presented in the living documentation.

2.) I catch the exception, implement a string based return statement with the error message and use an assertThat(...) in my low-level specifications so I only should get AssertException in the end.

3.) ..?

Question: And advice or common best practices how to handle exceptions or errors with selenium/serenity ?


Solution

  • First of all, there is a good source of information for you theme - xUnit test patterns book.

    Answering your question, the good approach is using 2 major groups of errors. The first one is AssertionException indicating a problem (bug) in application under test. The second one are all other exceptions indicating problems in test code itself, test execution env or application env, but not in application. Building your tests this way will help you finding and eliminating problems fast.

    So generally you are on the right way with your first option. It's a good idea to collect some additional data (e.g. application/execution env) when exception occurs as well.