Cucumber jvm 在 运行 所有功能后关闭浏览器
cucumber jvm close browser after running all features
我正在使用 Cucumber-JVM 和 Selenium 测试 Spring 启动应用程序。我正在定义一个网络驱动程序 bean 并传递一个 destroy 方法来退出(关闭网络驱动程序和 Chrome 的所有实例)但它使浏览器保持打开状态。
@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();
}
}
我基本上是从 Cucumber for Java 一书中复制了一个示例。不同之处在于他们使用 xml 配置,而我选择了 Java 配置。
<bean class="org.openqa.selenium.support.events.EventFiringWebDriver" destroy-method="quit">
<constructor-arg>
<bean class="org.openqa.selenium.firefox.FirefoxDriver" />
</constructor-arg>
</bean>
如何让浏览器在所有测试结束时关闭?
您的 java class 中是否有以下代码?
public void quit() {
driver.quit();
}
在您的 bean 定义中,您正在初始化驱动程序。按照惯例,您应该使用 init-method 属性进行设置,使用 destroy-method 属性进行拆卸。
bean 应该是这样的。
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.whosebug.com");
driver.getTitle();
}
可能只是您选择了错误的注释属性(或者只是一个拼写错误)。你应该使用 destroyMethodName 而不是 destroyMethod。参考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();
}
}
我以为默认的 bean 作用域是单例的,但如果我添加 @Scope("singleton"),浏览器会正确关闭。例如:
@Bean(destroyMethod = "quit")
@Scope("singleton")
public WebDriver webDriver() {
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
return new ChromeDriver();
}
我正在使用 Cucumber-JVM 和 Selenium 测试 Spring 启动应用程序。我正在定义一个网络驱动程序 bean 并传递一个 destroy 方法来退出(关闭网络驱动程序和 Chrome 的所有实例)但它使浏览器保持打开状态。
@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();
}
}
我基本上是从 Cucumber for Java 一书中复制了一个示例。不同之处在于他们使用 xml 配置,而我选择了 Java 配置。
<bean class="org.openqa.selenium.support.events.EventFiringWebDriver" destroy-method="quit">
<constructor-arg>
<bean class="org.openqa.selenium.firefox.FirefoxDriver" />
</constructor-arg>
</bean>
如何让浏览器在所有测试结束时关闭?
您的 java class 中是否有以下代码?
public void quit() {
driver.quit();
}
在您的 bean 定义中,您正在初始化驱动程序。按照惯例,您应该使用 init-method 属性进行设置,使用 destroy-method 属性进行拆卸。
bean 应该是这样的。
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.whosebug.com");
driver.getTitle();
}
可能只是您选择了错误的注释属性(或者只是一个拼写错误)。你应该使用 destroyMethodName 而不是 destroyMethod。参考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();
}
}
我以为默认的 bean 作用域是单例的,但如果我添加 @Scope("singleton"),浏览器会正确关闭。例如:
@Bean(destroyMethod = "quit")
@Scope("singleton")
public WebDriver webDriver() {
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
return new ChromeDriver();
}