JUnit4 and Selenium

The Selenium website seems to be biased towards TestNG. That’s fine but I’m using JUnit4.

One nice feature of the selenium testcase class is to produce a screendump for failures. But it’s a JUnit 3 class; it seems they have not noticed that JUnit4 exists.

So how to get a screendump with a JUnit4 test case? Wrapping and delegation works but I wanted to do it directly.

In JUnit4 you can install a Listener that is notified at test execution checkpoints. Since I care about only failures I defined it like this:

To make JUnit4 use your listener you need to install it with a Runner. Here is a simple one that gets the job done.

Then in the superclass of my tests I use the RunWith annotation and specify my runner. Since I want to do the selenium setup only once per class I use the JUnit4 BeforeClass and AfterClass annotations. In production I’d probably start the server externally though. Or if I use Selenium userextensions.

etc.

Finally in my listeners testFailure method I get the Selenium instance from the base test class.

It works!

In a future post, let’s see if I can use Spring for dependency injection of that selenium instance.

3 thoughts on “JUnit4 and Selenium”

  1. Pingback: Web 2.0 Announcer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.