1

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

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

GEB:0.9.0

:2.26.0

时髦的:2.0.5

法郎: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
4

1 回答 1

2

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

您在这里有两个选择,我认为第二个更好:

  • 不要退出驱动程序 - 将打开一个浏览器窗口并在随后的测试执行中重复使用
  • 使用构建系统,例如Gradle (此处提供了使用 Gradle 构建的示例 Geb 项目) - 这将在执行测试后关闭 VM,并且还会自动关闭浏览器
于 2013-04-16T16:32:39.430 回答