1

所以我编写了我的第一个 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")) )
    }
}
4

3 回答 3

0

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

我学习了相同的课程并使用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. 您的测试将被执行,之后浏览器将关闭。

于 2013-03-29T01:08:54.537 回答
0

这就是我用的

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"

}

于 2014-08-15T10:27:49.983 回答
0

你是如何配置驱动的?如果您明确地创建它,您只需要退出驱动程序。否则驱动程序将自行关闭。

于 2013-03-27T13:27:32.333 回答