Best practices for exception handling with Selenium

43 views
Skip to first unread message

el.gato...@gmail.com

unread,
May 4, 2018, 3:33:05 AM5/4/18
to Selenium Users

I've been searching on google for best practices regarding exception handling, however I did not have much success besides using try-catch.


My framework is set with a EventFiringWebDriverwhich, according to Selenium documentation has a ExceptionThrown event.


I was thinking in implementing it so we can quickly debug the method that threw the exception, as some colleagues are not programmers and don't know a lot about coding.

The other idea I had is adding try-catch to each method but this seems like a really tedious and worthless endeavour as I would've to keep adding try-catchs to each new method.

So my question is, do you have any best practices regarding exception handling?


Thank you very much.

Jonathan Herbaut

unread,
May 4, 2018, 4:09:42 AM5/4/18
to seleniu...@googlegroups.com
I'm interested about this discussion.
On my side, I wrote my tests like that :

[Test]
public void NavigateToQueueStatus()
{
using (var pageTest = new PageTest<TConfig, Queue, TScreenSize>())
{
pageTest.ExecuteTest(
(page) =>
{
page.NavigateToTab(Queue.QueueView.Status, "ET0021B7BAA4CA");
});
}
}

The PageTest is my main class with common methods to all my app. The object page returned is an instance of the class of my PageTest Queue (for the example).
Here is the content of my ExecuteTest method :

public void ExecuteTest(Action<TPage> handler)
{
try
{
//Exécution de la méthode donnée en paramètre
handler((TPage) Activator.CreateInstance(typeof(TPage), this));
}
catch (Exception e)
{
ManageError(e);
throw;
}
finally
{
CleanPageTest();
}
}

This is my ExecuteTest that takes care of my exceptions. ManageError makes a screenshot of my page with a problem, write the exception and stacktrace in a log file.

With that, I never use again a try/catch structure to get my errors. All I have to do is to call once the ExecuteTest method in my NUnit test with all the process inside the Action parameter.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/310d433b-0984-4459-a165-9d594dffe09b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages