Driver Instance - With hooks
The best place to implement this code is the @Before and @After scenario hooks given that we want to start a new driver instace before the scenario starts and kill the browser after the scenario is completed.
Consider the following code for StartingSteps class to demonstrate this concept.
public class StartingSteps {
private WebDriver driver;
@Before
public void beforeScenario() {
driver = new DriverFactory().getDriver();
System.out.println("this will run before the actual scenario");
}
@After
public void afterScenario() {
new DriverFactory().destroyDriver();
System.out.println("this will run after scneario is finished, even if it failed");
}
@Given("^the user is on landing page$")
public void setup() throws Throwable {
driver.get("http://accountsdemo.herokuapp.com");
driver.manage().window().maximize();
}
@Before("@Signup-DataDriven")
public void signupSetup() {
System.out.println("This should run everytime before any of the @Signup-DataDriven tagged scenario is going to run");
}
@After("@Signup-DataDriven")
public void signupTeardown() {
System.out.println("This should run everytime after any of the @Signup-DataDriven tagged scenario has run");
}
}