【问题标题】:How to pass Grails test with Geb and Spock? (using grails-functional-test-development)如何通过 Geb 和 Spock 的 Grails 测试? (使用 grails-functional-test-development)
【发布时间】:2011-07-12 07:09:22
【问题描述】:

请帮忙。

我有页面 login/auth.gsp

在正文中使用以下代码

<div id="page-wrap">
  <div class="login-block">
    <g:if test="${flash.message}">
      <h3  class='login_message'>${flash.message}</h3>
    </g:if>
    <g:else>
      <h3>IMMS Login</h3>
    </g:else>
    <form action='${postUrl}' method='POST' id='loginForm' class='cssform' autocomplete='off'>
      <p>
        <label for='username'><g:message code="auth.username.label" default="User Name"/></label>
        <input type='text' class='text_' name='j_username' id='username'/>
      </p>
      <p>
        <label for='password'><g:message code="auth.password.label" default="Password"/></label>
        <input type='password' class='text_' name='j_password' id='password'/>
      </p>
      <p class="submit-wrap">
        <input type='submit' value="${message(code: 'default.button.Login.label', default: 'Login')}"/>
      </p>
    </form>
  </div>
  <div class="image-block"></div>
</div>

在 test/functional/pages 目录下,我有 LoginPage

package pages

import geb.Page

class LoginPage extends Page {
  static url = "login/auth/"
  static at = {
    userName.text() == ""
  }
  static content = {
    userName { $("input", name : "j_username") }
    password { $("input", name : "j_password") }
    loginButton(to: IndexPage){ $(".submit-wrap").find("input").first() }
  }
}

这是我的测试代码

import geb.spock.GebReportingSpec
import pages.*
import spock.lang.Stepwise
@Stepwise
class BaseSpec extends GebReportingSpec {
  String getBaseUrl() {
    return "http://localhost:8080/IMMS/"
  }

  def "open URL"() {
    when:
    to LoginPage
    then:
    at LoginPage
  }

}

我运行测试但失败了。这是报告

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="1" hostname="SGCDMSSVR" name="BaseSpec" tests="1" time="24.715" timestamp="2011-07-12T07:00:04">
  <properties />
  <testcase classname="BaseSpec" name="open URL" time="24.693">
    <failure message="Condition not satisfied:

at LoginPage
|
false
" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Condition not satisfied:

at LoginPage
|
false

    at BaseSpec.open URL(BaseSpec.groovy:18)
</failure>
  </testcase>
  <system-out><![CDATA[--Output from open URL--
]]></system-out>
  <system-err><![CDATA[--Output from open URL--
]]></system-err>
</testsuite>

有什么想法可以帮助我吗?是我遗漏了一些配置还是我的类似 jQuery 的导航不正确?

对于测试,我使用的是“功能测试开发”插件。


更新: 最初我使用的 GebConfig 正是来自 The sample. 我刚刚注意到默认驱动程序是 HTMLUnit。

当我使用命令控制台中的Functional Test Development 功能运行功能测试时。

grails develop-functional-tests

我选择了运行所有功能测试的选项。控制台显示测试失败。

当我将默认驱动程序更改为 Firefox 时。 它仍然失败,但我可以看到它自动打开 Firefox 浏览器并打开 URL:

http://localhost:8080/IMMS/login/auth.gsp

并且它无法打开 404 的 URL。我认为这就是测试失败的原因。

我尝试从 IDE 运行以下命令。

grails test-app -functional

它可以工作,并且打开了 firefox 浏览器,它执行了编写的测试脚本并通过了测试。

所以,我在这里修改标题。现在的重点是 grails 的功能测试开发插件。 也许你们中的任何人曾经尝试过这个插件并有答案? 谢谢。

PS:我可以修改问题吗?还是我应该在 stackoverflow 中创建新问题?

【问题讨论】:

  • 这里在黑暗中拍摄,但可以尝试删除LoginPageurl 属性中的尾部斜杠(login/auth 而不是login/auth/)。我不明白为什么 Geb 会在末尾添加 .gsp

标签: grails spock geb


【解决方案1】:

LoginPageat 条件失败(您不在预期的页面中,或者您的标记中有一些问题导致条件失败)。您可以通过在其中输入assert 来获取更多信息。

喜欢:

package pages

import geb.Page

class LoginPage extends Page {
  static url = "login/auth/"
  static at = {
    assert userName.text() == ""
    true
  }
  static content = {
    userName { $("input", name : "j_username") }
    password { $("input", name : "j_password") }
    loginButton(to: IndexPage){ $(".submit-wrap").find("input").first() }
  }
}

这应该告诉你选择器的实际值是多少。

无论如何,我认为这种情况很脆弱。对于页面识别,您最好检查文档的标题或某些特定字符串,例如页眉。在您的情况下,我会匹配 &lt;h3&gt; 标头。即:

static at = {
  assert $('h3').text() =~ /IMMS Login/
  true
}

(最后的 true 是这样,at-checking 不会失败,因为您已经在断言条件,但断言具有 null 值。)。

另外,请查看 this 博客文章以获得更一致的替代方案。

【讨论】:

  • 感谢您的回答。我认为这更多地取决于我用来运行的插件。我会试着把断言和看看
  • 在这方面确实不错。添加assert 将允许您“提前失败”并获得有关 at 条件失败原因的更多信息,仅此而已。有关检查的更详尽讨论,请参见此处:gebish.org/manual/current/browser.html#at_checkinggebish.org/manual/current/pages.html#at_verification。不过,我必须在最后添加true,否则在使用assert 时,我的at 条件总是会失败。
  • 第二个链接建议使用assert 只是为了获得其详细输出的好处。
  • 是的,对不起,我第一次读错了。顺便说一句,我已经用新的发现更新了这个问题。谢谢。
【解决方案2】:

我已经询问了 grails 插件的创建者 here。 所以基本上,这与测试代码问题无关。我只需要使用不同的端口进行测试。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多