I am using cucumber-jvm.
I have an init method to initialize all the necessary stuff, such as the browser dimensions, application url etc. I have put this init method under a @Before (cucumber.api) tag.
@Before
public void initLoginPage() throws Exception {
getBrowserDimension();
setBrowserCapabilities();
init(getApplicationUrl());
}
My life was fine with this running smoothly. Now, I also wanted to use @Before for some tags at scenario levels. Say my scenario looks like:
@myTag
When I do blah
Then I should get blah-blah
And I wanted to use something like:
@Before(@myTag)
public void beforeScenario(){
blah = true;
}
But the moment I give it another @Before, it starts giving a NullPointerException. I tracked it back to the runBeforeHooks and runHookIfTagsMatch methods in Cucumber's Runtime class. They are throwing the exception for the @Before (for initLoginPage()) itself. Is there a conflict getting created with multiple @Before's? How can I resolve this?
I found the solution to get this working. The problem was that any of the @Before codes were getting picked up in a random order. It wasn't based on the assumption that a @Before without parameters will be executed before @Before("myTag").
So the trick is to assign order parameter (in @Before) some value. The default order that gets assigned to @Before is 10000. So, if we define the order value explicitly, it should work.
So basically, my code for initializer could look like:
@Before(order=1)
public void initLoginPage() throws Exception {
getBrowserDimension();
setBrowserCapabilities();
init(getApplicationUrl());
}
That solved my problem