从GroovyConsole后续调用Geb Spock测试失败,出现UnreachableBrowserException

时间:2013-04-15 22:41:29

标签: groovy spock geb

我正在GebReportingSpec内运行以下脚本(GroovyConsole.exe测试用例)。当GroovyConsole启动时,它第一次正常运行。它打开FF,运行场景,然后在退出浏览器时关闭FF。

但是,当我从同一个GroovyConsole再次运行相同的脚本时,我得到一个UnreachableBrowserException。它首先调用setupSpec()方法,然后在测试用例中调用go方法时调用cleanupSpec() ...

GEB :     0.9.0

:     2.26.0

Groovy的: 2.0.5

FF : 14.0.1

JDK : 1.6.0_37 64位

脚本

@Grapes([
    @Grab("org.gebish:geb-core:0.9.0"),
    @Grab("org.gebish:geb-spock:0.9.0"),
    @Grab("org.seleniumhq.selenium:selenium-firefox-driver:2.26.0"),
    //@Grab("org.seleniumhq.selenium:selenium-chrome-driver:2.26.0"),
    @Grab("org.seleniumhq.selenium:selenium-support:2.26.0")
])
import geb.Browser
import geb.spock.GebReportingSpec
import org.openqa.selenium.firefox.*

class Google_Search_Test extends GebReportingSpec {
def setupSpec() {
    println "Inside setupSpec()..."
    browser.config.autoClearCookies = true
}

def cleanupSpec() {
    println "Inside cleanupSpec()..."
    println "Quitting browser..."
    browser.quit()
}

def "google_search_wikipedia"() {
    println "Inside google_search_wikipedia..."
    when:
        println "Going to google.com..."
        go "http://google.com/ncr"

        // make sure we actually got to the page
        assert title == "Google"

        // enter wikipedia into the search field
        $("input", name: "q").value("wikipedia")

        // wait for the change to results page to happen
        // (google updates the page dynamically without a new request)
        waitFor { title.endsWith("Google Search") }

        // is the first link to wikipedia?
        def firstLink = $("li.g", 0).find("a.l")
    then:
        firstLink.text() == "Wikipedia"
        println "Finished test execution..."

}
}

GebConfig.groovy

import org.openqa.selenium.firefox.*
import java.util.concurrent.*

driver = { 
    FirefoxProfile firefoxProfile = new FirefoxProfile()
    firefoxProfile.setPreference("capability.policy.default.Window.frameElement", "allAccess")

    def driver = new FirefoxDriver(firefoxProfile)
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS)
    driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS)

    driver
}

waiting {
    // all values are in seconds
    timeout = 60
    retryInterval = 0.5
}

reportsDir = "SeleniumReports"

成功运行

Inside setupSpec()...
Inside google_search_wikipedia...
Going to google.com...
Finished test execution...
Inside cleanupSpec()...
Quitting browser...
JUnit 4 Runner, Tests: 1, Failures: 0, Time: 15739

后续运行(例外)

Inside setupSpec()...
Inside google_search_wikipedia...
Going to google.com...
Inside cleanupSpec()...
Quitting browser...
JUnit 4 Runner, Tests: 1, Failures: 2, Time: 687
Test Failure: google_search_wikipedia(Google_Search_Test)
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.26.0', revision: '18040', time: '2012-11-02 09:44:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:526)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:275)
    at geb.Browser.go(Browser.groovy:371)
    at geb.Browser.go(Browser.groovy:363)
    at geb.spock.GebSpec.methodMissing(GebSpec.groovy:51)
    at Google_Search_Test.google_search_wikipedia(Google_Search.groovy:27)
Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot be used after quit() was called.
Build info: version: '2.26.0', revision: '18040', time: '2012-11-02 09:44:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:351)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:505)
    ... 5 more

Test Failure: google_search_wikipedia(Google_Search_Test)
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.26.0', revision: '18040', time: '2012-11-02 09:44:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:526)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:535)
    at org.openqa.selenium.remote.RemoteWebDriver.getPageSource(RemoteWebDriver.java:396)
    at geb.report.PageSourceReporter.getPageSource(PageSourceReporter.groovy:42)
    at geb.report.PageSourceReporter.writePageSource(PageSourceReporter.groovy:38)
    at geb.report.PageSourceReporter.writeReport(PageSourceReporter.groovy:29)
    at geb.report.CompositeReporter.writeReport(CompositeReporter.groovy:31)
    at geb.Browser.report(Browser.groovy:698)
    at geb.spock.GebReportingSpec.report(GebReportingSpec.groovy:43)
    at geb.spock.GebReportingSpec.cleanup(GebReportingSpec.groovy:39)
Caused by: org.openqa.selenium.WebDriverException: The FirefoxDriver cannot be used after quit() was called.
Build info: version: '2.26.0', revision: '18040', time: '2012-11-02 09:44:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_37'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:351)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:505)
    ... 9 more

1 个答案:

答案 0 :(得分:2)

Geb的概念为driver caching,因此只打开并重复使用一个浏览器窗口,并且您在groovy控制台中使用它时遇到了问题。 Geb为您管理驱动程序/浏览器实例,缓存在VM的整个生命周期中都处于活动状态。您收到错误是因为您手动退出驱动程序(如果启用了驱动程序缓存,则不应该执行此操作)并且VM尚未关闭(groovy控制台仍在两次执行之间运行)这意味着浏览器管理从缓存中检索到的驱动程序已经关闭。

你有两个选择,我认为第二个更好:

  • 不要退出驱动程序 - 将打开一个浏览器窗口,并在后续的测试执行中重复使用
  • 使用构建系统,例如Gradle(使用Gradle构建的示例Geb项目是可用的here) - 这将在执行测试后关闭VM,它也会自动关闭浏览器