1

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?

asked Aug 20, 2013 at 3:09
2
  • 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. Commented Aug 20, 2013 at 21:33
  • @user246 Updated to include JUnit version, we'd upgrade to any version which gave this behavior. Commented Aug 21, 2013 at 2:29

1 Answer 1

2

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

answered Aug 21, 2013 at 7:00
5
  • 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. Commented 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. Commented 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. Commented 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. Commented 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). Commented Aug 23, 2013 at 17:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.