Cucumber hooks do the job for you.
import cucumber.annotation.After;
import cucumber.annotation.Before;
public static WebDriver DRIVER;
@Before
public void setUp() {
// start browser if it does not exist yet
}
@After
public void tearDown() {
// clean cookies
}
Note that I use the cucumber before, not the JUnit before.Do make sure you have a reference to the DRIVER
in your tests. The hooks wil run before and after each scenario (or example if you use scenario outline). If you want a specific setup for certain annotated features, for example:
@slowtest
Feature: F1 feature
Then you can use:
import cucumber.annotation.After;
import cucumber.annotation.Before;
public static WebDriver DRIVER;
@Before("@slowtest")
public void setUp() {
// start browser if it does not exist yet
}
@After("@slowtest")
public void tearDown() {
// close browser or clean cookies, or....
}
Conclusion you can use cucumber hooks in combination with annotations in features for custom setup and teardown.