【问题标题】:Geb with spock and closing the browser windowGeb 与 spock 并关闭浏览器窗口
【发布时间】:2013-03-27 12:53:50
【问题描述】:

所以我编写了我的第一个 Geb 脚本,它扩展了 GebReportingSpec。我已经指定了一个 def 清理,我在其中执行了 driver.quit()。现在浏览器确实关闭了,但我收到一个错误,因为屏幕截图实用程序无法运行,因为我猜它在浏览器关闭后运行。我试着睡觉看看是否是问题所在,但没有任何作用


JUnit 4 Runner, Tests: 1, Failures: 1, Time: 25944Test Failure: Validate that the default time zone for store is set to America/New_York(groovy.manager.ReferenceStoreDefaultTimeZoneTests)org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.3', java.
version: '1.6.0_43'
Driver info: driver.version: RemoteWebDriver
        at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute
(FirefoxDriver.java:352)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.ja
va:527)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.ja
va:569)
        at org.openqa.selenium.remote.RemoteWebDriver.getPageSource(RemoteWebDri
ver.java:414)
        at geb.report.PageSourceReporter.getPageSource(PageSourceReporter.groovy
:39)
        at geb.report.PageSourceReporter.writePageSource(PageSourceReporter.groo
vy:35)
        at geb.report.PageSourceReporter.writeReport(PageSourceReporter.groovy:2
7)
        at geb.report.ScreenshotAndPageSourceReporter.writeReport(ScreenshotAndP
ageSourceReporter.groovy:31)
        at geb.Browser.report(Browser.groovy:731)
        at geb.spock.GebReportingSpec.report(GebReportingSpec.groovy:43)
        at geb.spock.GebReportingSpec.cleanup(GebReportingSpec.groovy:39)

想法?

编辑:

驱动程序正在通过 GebConfig 文件创建

@Grab(group='org.seleniumhq.selenium', module='selenium-firefox-driver', 
    version='2.31.0')
@Grab(group='org.seleniumhq.selenium', module='selenium-chrome-driver', 
    version='2.31.0')
@Grab(group='org.seleniumhq.selenium', module='selenium-htmlunit-driver', 
    version='2.31.0')
@Grab("org.seleniumhq.selenium:selenium-support:2.28.0")

import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver

reportsDir = "target/geb-reports"

// default is to use firefox
driver = {
    def driver = new FirefoxDriver()
    driver.manage().window().maximize()
    return driver
}

environments {
    'chrome' {
        def chromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
        System.setProperty("webdriver.chrome.driver", chromePath)

        driver = {
            def driver = new ChromeDriver()
            return driver
        }
    }

    'firefox' {
        driver = {
            def driver = new FirefoxDriver()
            driver.manage().window().maximize()
            return driver
        }   
    }

    'htmlunit' {
        driver = {
            def driver = new HtmlUnitDriver()
            driver.javascriptEnabled = true
            return driver
        }   
    }
}

这是正在执行的脚本

package groovy.manager

@Grab( 'org.spockframework:spock-core:0.7-groovy-2.0' )
@Grab(group='org.gebish', module='geb-core', version='0.9.0-RC-1')
@Grab(group='org.gebish', module='geb-spock', version='0.9.0-RC-1')
@Grab(group='org.seleniumhq.selenium', module='selenium-firefox-driver', 
    version='2.31.0')

import geb.spock.GebReportingSpec
import org.openqa.selenium.firefox.FirefoxDriver
import groovy.manager.pages.LoginPage
import groovy.manager.pages.org.OrganizationHomePage
import groovy.manager.pages.org.OrganizationProfilePage

class ReferenceStoreDefaultTimeZoneSpec extends GebReportingSpec {

    def "Validate that the default time zone is set to America/New_York"() {
        given: "You have the admin username and password"
            def username = "admin"
            def password = "test"

        when: "Navigate to Manager"
            to LoginPage

        and: "Log intoManager"
            usernameInput().value username
            passwordInput().value password
            submitBtn().click()

        then:
            assert at (OrganizationHomePage)

        and:
            assert ( {$("a[value='America/New York']")} )

        when: "Navigate to the organization profile"
            timeZoneLink().click()

        then:
            assert at (OrganizationProfilePage)

        and: "Verify America New York is selected"
            assert ( $("span", text: contains("America/New York")) )
    }
}

【问题讨论】:

  • 你是如何运行那个测试类的?
  • 从 xterm2 运行 groovy groovy/manager/ReferenceStoreDefaultTimeZoneSpec.groovy

标签: webdriver geb


【解决方案1】:

您如何配置驱动程序?您只需要quit the driver if you are explicitly creating it。否则驱动程序将自行关闭。

【讨论】:

  • 我添加了我的 GebConfig,这是处理驱动程序创建的地方。
  • 那你应该让Geb处理驱动的关闭,不需要你调用quit。
  • 当我通过命令行运行脚本时,测试将完成,它坐在那里,浏览器保持打开状态。命令提示符位于“JUnit 4 Runner, Tests: 1, Failures: 0, Time: 27463”
  • 版本控制?还有你的脚本是什么样的。我设置了一些失败的测试,我们的驱动程序都按预期关闭。
  • 脚本中的一切似乎都很好。我想看看您是否可以将其分解为尽可能小的可重现案例并发布到邮件列表或作为 github 上的错误,但我无法重现(尽管我们使用 Gradle 而不是 Grape/Ivy构建)。
【解决方案2】:

如果我在命令行上使用groovy 命令运行一个非常简单的geb 测试规范,我可以重现您的问题。我不知道这是否与使用 @Grab 注释或 groovy JUnit runner 的工作方式有关,但某些地方不对劲,浏览器确实没有关闭。

我学习了相同的课程并使用Gradle 构建运行它,运行正常结束并关闭浏览器。

如果您还没有安装 Gradle,最简单的方法是使用 gvm

然后将以下内容放入build.gradle 文件中:

apply plugin: 'groovy'                       

repositories {                               
    mavenCentral()                             
}                                            

dependencies {                               
   groovy 'org.codehaus.groovy:groovy-all:2.1.2'
   testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
   testCompile 'org.gebish:geb-spock:0.9.0-RC-1'
   testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:2.31.0'
} 

最后将GebConfig.groovy 和规范类复制到src/test/groovy 并运行gradle test。您的测试将被执行,之后浏览器将被关闭。

【讨论】:

  • 这可能有点跑题了,但我按照你的要求做了,我收到以下错误:“转换过程中的一般错误:模块版本冲突。模块 [groovy-all 在 2.1 版中加载。 1 并且您正在尝试加载版本 2.0.5"。我已经完成了我的整个项目,我根本不想加载 2.0.5。想法?
  • groovy-all:2.0.5 是 Spock 的依赖项(运行 gradle 依赖项以查看依赖项树)但应该没问题,因为 Gradle 应该采用最新版本的 groovy-all - 这是默认的 Gradle行为。你在使用一些旧版本的 Gradle 吗?它适用于我的最新版本 1.5。另外,请记住从您的测试类中删除所有 @Grab 注释,我一开始忘了这样做,但得到的错误与您的完全不同。如果一切都失败了,请尝试将 build.gradle 中的 groovy 依赖项更改为 2.0.5。
【解决方案3】:

这是我用的

dependencies {
def gebVersion = "0.9.3"
def seleniumVersion = "2.42.2"
// If using Spock, need to depend on geb-spock
testCompile "org.gebish:geb-spock:$gebVersion"
testCompile("org.spockframework:spock-core:0.7-groovy-2.0") {
    exclude group: "org.codehaus.groovy"
}
testCompile "org.codehaus.groovy:groovy-all:2.3.4"

testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
testRuntime "org.seleniumhq.selenium:selenium-support:$seleniumVersion"

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2012-07-21
    • 2017-01-16
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多