Rod McCutcheon
Rod McCutcheon

Reputation: 241

cucumber jvm close browser after running all features

I'm testing a Spring Boot app using Cucumber-JVM and Selenium. I'm defining a web driver bean and passing a destroy method to quit (shutdown the webdriver and all instances of Chrome) but it is leaving the browser open.

@Configuration
@ComponentScan(basePackages = "com.rodmccutcheon.pensionator.bdd")
public class CucumberConfig {

    @Bean(destroyMethod = "quit")
    public WebDriver webDriver() {
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        return new ChromeDriver();
    }

}

I've basically copied an example from the Cucumber for Java book. The difference is they use xml configuration and I've opted for Java configuration.

<bean class="org.openqa.selenium.support.events.EventFiringWebDriver" destroy-method="quit">
    <constructor-arg>
        <bean class="org.openqa.selenium.firefox.FirefoxDriver" />
    </constructor-arg>
</bean>

How do I get the browser to close at the end of all tests?

Upvotes: 0

Views: 3591

Answers (3)

Rod McCutcheon
Rod McCutcheon

Reputation: 241

I thought the default bean scope was singleton, but the browser is correctly shut down if I add @Scope("singleton"). eg:

@Bean(destroyMethod = "quit")
@Scope("singleton")
public WebDriver webDriver() {
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    return new ChromeDriver();
}

Upvotes: 5

Daniel Baktiar
Daniel Baktiar

Reputation: 1712

It probably just that you picked the wrong attribute of the annotation (or simply a typo). Instead of using destroyMethod you should use destroyMethodName. Refer to this.

@Configuration
@ComponentScan(basePackages = "com.rodmccutcheon.pensionator.bdd")
public class CucumberConfig {

    @Bean(destroyMethodName = "quit")
    public WebDriver webDriver() {
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        return new ChromeDriver();
    }

}

Upvotes: 0

Manmohan_singh
Manmohan_singh

Reputation: 1804

do you have the following code in your java class ?

public void quit()  {
      driver.quit();
    }

In your bean definition, you are initialising the driver. By convention , you should use init-method attribute for setup and destroy-method attribute for teardown.

the bean should look like this.

WebDriver driver;

public void initDriver(){
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
driver = new ChromeDriver();
}

public void quit(){
driver.quit();
}

@Bean(init-method="initDriver" , destroy-Method = "quit")
    public void testUrl() {
    driver.get("https://www.stackoverflow.com");
    driver.getTitle();
    }

Upvotes: 0

Related Questions