【发布时间】:2016-01-07 17:36:22
【问题描述】:
我为我的简单 Spring Boot Web 应用程序编写了以下测试:
@SpringApplicationConfiguration(classes=[PDK])
@WebIntegrationTest
@DirtiesContext
class GebMainpageSpec extends GebSpec {
@Autowired
WebApplicationContext context;
def setup() {
System.setProperty("webdriver.chrome.driver", "chromedriver/win32/chromedriver.exe");
browser.driver = new ChromeDriver();
browser.baseUrl = "http://localhost:8080/";
}
def 'Static page present and works, check without pages'() {
when:
go ""
then:
assert title == "MyApp"
}
def 'Static page present and works, check WITH pages'() {
when:
to Mainpage
then:
LoginWithFormUsername.value() == "root"
}
}
这些测试显然是有效的,即它们通过或失败取决于页面数据。
问题在于它会打开两个 Chrome 浏览器实例来运行(按测试次数)。
如何防止呢?可能是重用浏览器?或者可以在每次测试后关闭它?
更新
如果我添加如下内容
def cleanupSpec() {
browser.driver.quit()
}
然后我所有的测试开始运行两次,而且,他们每次运行都尝试使用HtmlUnit(即使用“内存中”网络浏览器,而不是 Chrome)。
【问题讨论】:
-
我假设您无法更改
setup中的驱动程序。您对浏览器的调用将启动在您的项目中配置的驱动程序。接下来您更改驱动程序,从而启动一个新实例,您将获得 htmlunit(默认)和您的 chrome。
标签: selenium-webdriver spring-boot selenium-chromedriver spock geb