Gbru
Gbru

Reputation: 1087

Cucumber 'After' Hook not working?

Cucumber 'After' Hook not working?

I have a DriverFactory class which performs the setup etc as listed below however once all steps have been executed the 'Cucumber After' method dosnt seem to work which is housed in the DriverFactory?

I want a master hooks class 'Before' 'After' etc etc which will stop code duplication within Step files

public class DriverFactory  {
protected WebDriver driver;
protected BasePage basePage;
protected LoginPage loginPage;

public WebDriver getDriver() {
    if(driver == null) {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\java\\resources\\other\\chromedriver.exe");
        this.driver = new ChromeDriver();
        this.driver.manage().window().maximize();
        this.driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
    }
    return this.driver;
}

public WebDriver returnDriver() {
    return this.driver;
}


@After
public void test() throws Throwable {
    this.driver.close();
    this.driver.quit();
}

}

public class LoginSteps {
DriverFactory driverFactory = new DriverFactory();
WebDriver driver = driverFactory.getDriver();

@Given("^User navigates to the \"([^\"]*)\" website$")
public void user_navigates_to_the_website(String url) throws Throwable {
    BasePage basePage = new BasePage(driver);
    basePage.loadUrl(url);
}

@And("^User entered the \"([^\"]*)\" username$")
public void user_entered_the_username(String username) throws Throwable {
    LoginPage loginPage = new LoginPage(driver);
    loginPage.setUsername(username);
}

Upvotes: 1

Views: 6414

Answers (1)

John J Smith
John J Smith

Reputation: 11941

I had the same problem - my @Before hook was working but my @After hook wasn't. This turned out to be due to a simple import problem - probably caused by choosing the wrong option when Intellij offered me the list of imports. I had chosen the junit.After class rather than the cucumber.api.Java.After class:

import cucumber.api.java.Before;
import cucumber.api.java8.En;
import org.junit.After;
import org.openqa.selenium.By;

Upvotes: 2

Related Questions