2

I'm using Selenium using both Java and Cucumber. And I'm curious when I'm using driver.quit() or driver.close() under @AfterTest method, browser won't closed and when I'm using it inside my test step i.e @Test annotated method. It's called perfectly within steps.

Refer below code for the same :

@Then("^I see Dashboard$")
public void iSeeDashboard() throws Throwable {
    LoginPage lp = new LoginPage(driver);
    Assert.assertEquals(lp.TitleId().getText(),"Mengelola User Customer");
    log.info("Successfully Login");
    driver.quit();
}

And here the whole code and I want to put driver.quit under @AfterTest

public class LoginStep extends steps.BaseStep {
public static Logger log = 
LogManager.getLogger(steps.BaseStep.class.getName());


@BeforeTest
public void before() throws IOException
{
   driver = initializeDriver();
   log.info("Driver is Initialized");
}

@Given("^I navigate to Website$")
public void iNavigateToWebsite() throws IOException {
    driver = initializeDriver();
    driver.get(prop.getProperty("URL"));
    log.info("Navigate to Website");
}

@Then("^I enter username and password")
public void iEnterUsernameAndPassword() throws IOException {
    LoginPage lp = new LoginPage(driver);
    lp.UsernameId().sendKeys("xxx");
    lp.PasswordId().sendKeys("xxx");
    log.info("Successfully entered valid credentials");
}

@When("^I click Login$")
public void iClickLogin() throws Throwable {
    LoginPage lp = new LoginPage(driver);
    lp.LoginId().click();
}

@Then("^I see Dashboard$")
public void iSeeDashboard() throws Throwable {
    LoginPage lp = new LoginPage(driver);
    Assert.assertEquals(lp.TitleId().getText(),"Lorem ipsum");
    log.info("Successfully Login");
    //driver.quit();
}

@AfterTest
public void tearDown()
{
    driver.quit();
}

Any explanation would be helpful.

5

@BeforeTest, @AfterTest annotations are not supported since Cucumber doesn't has the concept of test. It has features and you are executing the steps in Given, When & And, Then format. So in your case Cucumber only understands your steps not Tests.

Use @BeforeFeature & @AfterFeature which was Cucumber understands the hooks.

See the list of cucumber supported hook here.

Also not framework related hooks work with Cucumber like- onPrepare, beforeSession, afterSession and onComplete.

  • 2
    +1 Perfect, I was trying the same things in few months back and caught the same issue but its get resolved after using latest Cucumber Hooks like AfterFeature & BeforeFeature. So, go ahead with AfterFeature annotation man. – Michael Roy Dec 26 '17 at 10:51
  • @bharatmane I do not think @BeforeFeature and @AfterFeature work with cucumber framework. These work with specflow. Cucumber still to implement these annotations – RKM Jan 10 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.