【问题标题】:Browser opens many times while Geb testingGeb 测试时浏览器打开多次
【发布时间】: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


【解决方案1】:

在使用GebSpec 时,您不应该自己实例化驱动程序,因为它已经通过浏览器实例处理驱动程序实例的生命周期,它在getBrowser() method 中延迟初始化。

请通过the config script 参阅 The Book of Geb 关于how Geb interacts with WebDriver instances 和关于configuring the driver to be used 的部分以了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多