【问题标题】:Geb content elements unreachable in spock test: groovy.lang.MissingPropertyException在 spock 测试中无法访问 Geb 内容元素:groovy.lang.MissingPropertyException
【发布时间】:2018-11-30 02:57:13
【问题描述】:

我一直在使用 Geb 和 Spock 以及 Spring Boot,这一切都很好,直到我决定使用 Geb Page 类。以下是我的规范测试类详细信息

@SpringBootTest(webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@Category(IntegrationTest.class)
class LoginSpec extends GebSpec{

  @LocalServerPort
  private int port
//....
}

以下测试有效

def "User should with valid username and password "() {
    def username
    def password
    def loginUrl

    given: "A valid username and password"
    username = "user"
    password = "password"
    loginUrl = "http://localhost:" +port+"/hello"

    when:
    go loginUrl

    then:
    browser.page.title == "Login"

    when:
    $("#loginForm input[name=username]").value(username)
    $("#loginForm input[name=password]").value(password)
    $("#loginForm input[type=submit]").click()


    then:
    $("h1", text: iContains("Hello $username"))

}

我已将 LoginPage 类定义如下

import geb.Page

class ModeledLoginPage extends Page{

  static url = '/login'

  static at = { title == "Login"}

  static content = {

    loginForm { $("#loginForm")}
    formUsername { $("#loginForm input[name=username]") }
    formPassword { $("#loginForm input[name=password]") }
    formSubmitButton {
        loginForm.find("input", type: "submit")
    }
    errors {
        $(".alert-danger")
    }

    invalidUsernameOrPasswordError {
        errors.filter(text:contains("Invalid username and/or password"))
    }
  }


}

当我对上面使用以下内容的相同测试进行悲伤路径时:

 def "User should not be able to login with wrong Username or Wrong Password " (){
    def uname
    def passwd

    given: "A wrong username and a correct password"
    uname = "wronguser"
    passwd = "password"
    baseUrl = "http://localhost:"+port

    when: "User navigates to the login page"
    to ModeledLoginPage

    then:
    at ModeledLoginPage

    when: "User enters the wrong username and correct password and clicked on submbit button"
    ModeledLoginPage.formUsername.value(uname)
    ModeledLoginPage.formPassword.value(passwd)
    ModeledLoginPage.formSubmitButton.click()


    then: "We are redirected back to login page and there is an error message wrong: Invalid username and/or password "
    at ModeledLoginPage
    ModeledLoginPage.invalidUsernameOrPasswordError.present
}

我有以下错误:groovy.lang.MissingPropertyException: No such property: formUsername for class: net.myname.tutorials.springbootsecurity.ModeledLoginPage at net.myname.tutorials.springbootsecurity.LoginSpec.User should not be able to login with wrong Username or Wrong Password (LoginSpec.groovy:80)

我有点迷路了,无法确定问题出在哪里。我查看了 StackOverflow 和 Geb Book 上的各种帖子。

以下是我的依赖项:

dependencies {
 compile('org.springframework.boot:spring-boot-starter-security')
 compile('org.springframework.boot:spring-boot-starter-web')
 compile("org.springframework.boot:spring-boot-starter-thymeleaf")
 compile("org.springframework.boot:spring-boot-starter-data-jpa")
 compile("com.h2database:h2")
 testCompile("junit:junit")
 testCompile('org.springframework.boot:spring-boot-starter-test')
 testCompile('org.springframework.security:spring-security-test')
 testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'
 testCompile 'org.codehaus.groovy:groovy-all:2.4.11'
 testCompile "org.gebish:geb-core:2.1"
 testCompile "org.gebish:geb-spock:2.1"
 testCompile "org.seleniumhq.selenium:selenium-firefox-driver:3.6.0"
 testCompile "org.seleniumhq.selenium:selenium-support:3.6.0"

}

如果有人能对此有所了解,将不胜感激。谢谢

【问题讨论】:

    标签: bdd spock functional-testing spring-test geb


    【解决方案1】:

    您应该将代码更改为:

    when: "User enters the wrong username and correct password and clicked on submbit button"
    formUsername.value(uname)
    formPassword.value(passwd)
    formSubmitButton.click()
    
    
    then: "We are redirected back to login page and there is an error message wrong: Invalid username and/or password "
    at ModeledLoginPage
    invalidUsernameOrPasswordError.present
    

    有关说明,请参阅 http://gebish.org/manual/current/#spock-junit-testnghttp://gebish.org/manual/current/#the-page

    如果您希望跟踪当前页面类型以便从您的 IDE(主要是 IntelliJ,请参阅 http://gebish.org/manual/current/#authoring-assistance-autocomplete-and-navigation)获得更好的创作支持,您可能需要将其重写为:

    then:
    def modeledLoginPage = at ModeledLoginPage
    
    when: "User enters the wrong username and correct password and clicked on submbit button"
    modeledLoginPage.formUsername.value(uname)
    modeledLoginPage.formPassword.value(passwd)
    modeledLoginPage.formSubmitButton.click()
    
    
    then: "We are redirected back to login page and there is an error message wrong: Invalid username and/or password "
    at(ModeledLoginPage).invalidUsernameOrPasswordError.present
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多