抱歉所有这些代码,但我不知道是什么导致了我的问题,所以就这样吧。
我将 geb 插件配置为使用 JUnit 运行功能测试。所以我在我的 buildConfig.groovy 中有:
def seleniumVersion = "2.29.0"
def gebVersion = "0.7.0"
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.5'
provided('com.oracle:oracle:11.1.0.7.0')
provided('com.oracle:i18n:10.2.0.5')
test ("org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion") {
export = false
}
test("org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"){
excludes "commons-io"
export = false
}
test ("org.seleniumhq.selenium:selenium-ie-driver:$seleniumVersion") {
export = false
}
test ("org.seleniumhq.selenium:selenium-support:$seleniumVersion") {
export = false
}
test ("org.seleniumhq.selenium:selenium-remote-driver:$seleniumVersion") {
export = false
}
test ("org.codehaus.geb:geb-junit4:$gebVersion") {
export = false
}
}
plugins {
build(":tomcat:$grailsVersion") {
export = false
excludes 'svn'
}
compile (":hibernate:$grailsVersion") {
export = false
excludes 'svn'
}
build (":release:2.0.0") {
excludes 'commons-io','http-builder'
export = false
}
compile(":spring-security-core:1.2.7.3") { excludes 'svn' }
compile(":spring-security-ldap:1.0.6")
compile (":remote-control:1.3") {
export = false
}
test(":geb:$gebVersion") {
export = false
}
}
我的 conf 文件夹中有一个 GebConfig.groovy:
driver = {
//def driver = new HtmlUnitDriver()
//driver.javascriptEnabled = true
//driver
def driver = new FirefoxDriver()
driver
}
environments {
// run as “grails -Dgeb.env=chrome test-app”
// See: http://code.google.com/p/selenium/wiki/ChromeDriver
chrome {
driver = { new ChromeDriver() }
}
// run as “grails -Dgeb.env=firefox test-app”
// See: http://code.google.com/p/selenium/wiki/FirefoxDriver
firefox {
driver = { new FirefoxDriver() }
}
}
我有一个登录功能测试:
class LoginTests extends GebReportingTest {
@Test
void login() {
to LoginPage
at LoginPage
username = "SERGIO"
password = "SERGIO"
loginButton.click()
assert at(IndexPage)
link.click()
}
}
这是我的两个页面:
class LoginPage extends Page {
static url = "login/auth"
static at = {
title ==~ /Efetuar Login/
}
static content = {
loginForm { $("form", id: "loginForm") }
username { $("input", type:"text", id:"username") }
password { $("input", type:"password", id:"password") }
loginButton{ $("input", type:"submit", id:"submit") }
}
}
class IndexPage extends Page {
static at = {
title ==~ /Security Service Index View/
}
static content = {
description { $('h1') }
link { $('a') }
}
}
出于某种原因,我的功能测试运行了两次,无论我如何开始:
grails test-app :functional
grails test-app -functional
最佳答案
看起来 Geb 插件与 Grails 2.3.x 不完全兼容。由于某种原因,升级到 Geb 插件 0.9.2 后,测试执行了两次。
我相信这个问题与 https://jira.grails.org/browse/GRAILS-10552 有关以及作为 https://jira.grails.org/browse/GRAILS-6352 的一部分所做的更改.
在 Grails 2.3.x+ 中,GrailsSpecTestType 负责 Junit 和 Spock 测试: https://github.com/grails/grails-core/blob/bce298f0/grails-test/src/main/groovy/org/codehaus/groovy/grails/test/spock/GrailsSpecTestType.groovy#L33
看起来 Geb 插件正在将已弃用的 JUnit4GrailsTestType 添加到执行中: https://github.com/geb/geb/blob/584738cb/integration/geb-grails/scripts/_Events.groovy#L60-L67
这就是功能测试执行两次的原因。
这就是我在 Geb 0.9.2/0.9.3 版本中解决问题的方法。
grails test-app functional:spock
看起来 Geb 版本 0.9.1 没有执行两次测试。
差异似乎是由这个提交引起的: https://github.com/geb/geb/commit/9c71e820
您还应该知道,您不应该在 Grails 2.3.x/2.4.x 中安装 Spock 插件。
关于firefox - Geb Firefox 驱动程序 : why my test runs twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404892/