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?
-
1This 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– user2462013年08月20日 21:33:57 +00:00Commented Aug 20, 2013 at 21:33
-
@user246 Updated to include JUnit version, we'd upgrade to any version which gave this behavior.EGHM– EGHM2013年08月21日 02:29:35 +00:00Commented 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– EGHM2013年08月21日 09:58:21 +00:00Commented 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– Dale Emery2013年08月21日 19:07:13 +00:00Commented 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– EGHM2013年08月21日 20:19:02 +00:00Commented 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– Dale Emery2013年08月21日 22:24:51 +00:00Commented 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– EGHM2013年08月23日 17:00:08 +00:00Commented Aug 23, 2013 at 17:00