Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 '13 at 21:33
@user246 Updated to include JUnit version, we'd upgrade to any version which gave this behavior. – EGHM Aug 21 '13 at 2:29
up vote 1 down vote accepted

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

share|improve this answer
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 '13 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 '13 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 '13 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 '13 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 '13 at 17:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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