【问题标题】:Functional testing with Spring Security plugin in a Grails app在 Grails 应用程序中使用 Spring Security 插件进行功能测试
【发布时间】:2012-10-01 00:22:52
【问题描述】:

我有一个带有 spring 安全插件的安全 grails 应用程序,现在我正在尝试对登录过程进行一些测试,但到目前为止还没有成功。有人知道问题出在哪里吗?

这是我的 LoginPage.groovy

package pages.login

import geb.Page


class LoginPage extends Page {
    static url = "login/auth"

    static at = {
        title ==~ /Login/
    }

    static content = {
        loginForm { $("form") }
        username { $("input", type:"text", name:"j_username") }
        password { $("input", type:"password", name:"j_password") }
        loginButton{ $("input", type:"submit", value:"Login") }
    }
}

这是使用junit4的测试:

import geb.junit4.GebReportingTest

import pages.copyright.*
import pages.login.LoginPage
import org.junit.Test

class CopyrightCRUDTests extends GebReportingTest {

    @Test
    void doSomeCrud() {

        to LoginPage
        at LoginPage
        $("form").j_username() << "admin"
        $("form").j_password() << "XXXXX"
        loginButton.click()

        to AuthenticatedPage
        at AuthenticatedPage

    }
}

AuthenticatedPage 是一个需要认证的页面,但目前无法使用 geb 进行认证。有人知道这个问题吗?

提前致谢!

【问题讨论】:

  • 您遇到的错误是什么?使用 firefox 驱动,看看发生了什么...
  • 你不应该需要这个:到 AuthenticatedPage
  • 也不需要at LoginPage

标签: grails spring-security junit4 functional-testing geb


【解决方案1】:

我不知道这是否会对你有所帮助,因为你没有使用 Spock,但我在登录时也遇到了一些问题(虽然不记得具体是什么)

我最终找到了我放入扩展 GebSpec 的规范中的代码。

在每个需要登录的测试之前调用它:

  def setupSpec() {
    Browser.drive {
      go baseUrl
        $("form.login").with {
          username = System.getenv("APP_USERNAME")
          password = System.getenv("APP_PASSWORD")
        }
        $("input[name='submit']").click()
      }
  }

看起来不多,但在表单上使用“with”对我有用。

【讨论】:

    【解决方案2】:

    尝试改写doSomeCrud()如下:

    to LoginPage
    loginForm.j_username = 'admin'
    loginForm.j_password = 'XXXXX'
    loginButton.click()
    waitFor { at AuthenticatedPage }
    

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2018-09-09
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      相关资源
      最近更新 更多