Capturing JavaScript errors with Watir
There has been few times topics about capturing JavaScript errors during the test. I’m pretty sure that you have seen this or some similar picture in your browser bottom toolbar:
This is exactly the moment which needs to be captured. Ideally it would be captured by your tests anyway, because you’d test all the functionality. This means that you’d click some link and fire all it’s JavaScript events and then verify (by asserting) that something changed due to the JavaScript and if it didn’t change then your test would fail and you would also know that it failed due to the JavaScript error itself. That would be the ideal world. I think that we all know that it’s quite impossible to test all of the functionality. We’re just trying to give our best. That’s why I and some others in the world have been thinking that why shouldn’t we fail our test whenever JavaScript appears to be broken even if it’s not directly related with the testcase itself. I don’t know if it is the best idea or not, but I thought to give it a try.
First ideas.
At first I thought that if some test fails then I should also check if there was any JavaScript errors and if yes, then add it to the report somehow. My idea was to take a screenshot from the details window when some test fails. I also thought about getting this information somehow programmatically through the OLE object directly, but failed to do so. So, the implementation in our WatirHtmlFormatter class looks kind of kinky because it performs double click on the JavaScript error dialog to open it up and, makes a screenshot and closes the window:
And let’s see the results of running this test:
As you can see from the picture then there’s a possibility to click on the button “Copy error details” (only IE8) which would copy this information to the clipboard and then with the help of gem called win32-clipboard it should be possible to put this text into the report instead of making the picture. Just an idea.
Improving the error catching.
As already mentioned above then the biggest problem is that there will be some JavaScript errors which will be unnoticed because your test just won’t cover some kind of functionality or you won’t check certain things on the page. So, how to catch these? Actually it is relatively easy to achieve because Watir::IE class wait method executes another method called run_error_checks which in turn executes your defined checks. And wait method itself is used by almost (maybe all) actions, which might trigger JavaScript errors - clicking, changing input field’s value, selecting value from select list, changing page url and so on. Maybe this picture created with Diagrammr helps you to understand the flow better:

So we just have to create our checker and add it to the error_checkers list. It’s easy as pie (actually making a pie might be harder):
And if we run our previous test again, it won’t make it to the line where we manually raised an exception. That solution should catch most of your JavaScript errors so you could produce quality product. If you have any better ideas/solutions how to capture the JavaScript errors then don’t hesitate to let me know.




2 years ago