How to perform Selenium navigation before tests run without using JUnit 4.11+'s setUp or at the start of the test method?

We've got both navigation and bookmark based Selenium tests. The test code for the page under test is in a separate method that is called by test methods ending in either Bookmark or Nav. Navigation to the page under test is called from the setUp method, but exceptions that occur when navigating don't get treated as test failures because they are running in setUp. An easy solution that we don't like would be adding a call to the navigation method as the first line of each Nav test. Is there an alterantive way to handle our navigation?

  • 1
    This is really a question about JUnit; there is nothing Selenium-specific about it. What version of JUnit are you using (or willing to use)? I suspect there is a way to do what you want by extending some JUnit classes.
    – user246
    Aug 20, 2013 at 21:33
  • @user246 Updated to include JUnit version, we'd upgrade to any version which gave this behavior.
    – EGHM
    Aug 21, 2013 at 2:29

1 Answer 1


Use a JUnit Rule. Here are two articles I wrote about how I've used Rules:

  • I had to move the rest of my setUp into the @Rule, now when I get a failure during navigation the other test methods are not run.
    – EGHM
    Aug 21, 2013 at 9:58
  • Clearly something is wrong, but I can't tell what. If you want help, contact me via email. See my profile for email address.
    – Dale Emery
    Aug 21, 2013 at 19:07
  • Looks like Rule is executed before Before and the failure is at the class level so the other tests are not run.
    – EGHM
    Aug 21, 2013 at 20:19
  • Is your code in the rule's apply() method? Move it into the statement's evaluate() method. Then you can move your other setup code back where it was.
    – Dale Emery
    Aug 21, 2013 at 22:24
  • The code was in the apply method yes. Moving it to evaluate seemed like it did the trick so I marked your answer as the answer, I hadn't notice that my tearDown was no longer be run and failures not recorded. If I move the setUp call out the tests run and pass instantly (they don't really run).
    – EGHM
    Aug 23, 2013 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.