I'm driving a suite of Selenium tests (actually WebDriver-backed Selenium) using JUnit 4.8.2. I'd like the tests to automatically take a screenshot of the browser as soon as the test fails an assertion. All the tests inherit from SeleniumBaseTestCase
, and the majority then further inherit from from SeleniumBastTestCaseWithCompany
(which uses @Before
and @After
methods to create and then clean up common test data via Selenium).
I've tried adding a subclass of TestWatchman
as a @Rule
in SeleniumBaseTestCase
, overriding TestWatchman
's failed
method to take the screenshot. The trouble is that the @After
methods cleaning up the test data are being run before TestWatchman
's failed
method is called, so the screenshots are all of the final step of the clean-up, not the test that failed.
Looking into it a little, it seems that TestWatchman
's apply
method just calls the passed Statement
's evaluate method (the only exposed method), which calls the @After
methods, leaving TestWatchman
(or any other Rule
) no chance to insert any code between the execution of the test and of the @After
methods, as far as I can tell.
I've also seen approaches that create a custom Runner
to alter the Statement
s created so that methods annotated with the custom @AfterFailure
are run before @After
methods (so the screenshot can be taken in such an @AfterFailure
method), but this relies on overriding BlockJUnit4ClassRunner
's withAfters
method, which is deprecated and due to become private, according to the documentation, which suggests using Rules instead.
I've found another answer on SO about the @Rule lifecycle that makes it sound like this simply might not be possible in JUnit 4.8, but may be possible in JUnit 4.10. If that's correct then fair enough, I'd just like confirmation of that first.
Any thoughts on an elegant and future-proof way in which I can achieve what I want would be much appreciated!