【问题标题】:Geb click executed before input is completed在输入完成之前执行 Geb click
【发布时间】:2014-04-24 05:44:39
【问题描述】:

我正在运行一个简单的 Geb 测试,如下所示:

class IndexPage extends Page {
  //static url = "http://localhost:8080/sampleGrailsApp"
  static at = { title == "sampleGrailsApp" }
  static content = {
    signInButton { $("div", 0, class: "container-fluid").find("button", 0) }
    modalFooterSignInButton(wait: true, to: MyPage) { $("footer", 0, class: "modal-footer").find("input", 0, class: "btn-primary") }
    modalFooterUsernameInput { $("form").username = it }
    modalFooterPasswordInput { $("form").password = it }
    signIn { username, password ->
      signInButton.click()
      waitFor { modalFooterSignInButton.present }
      modalFooterUsernameInput username
      modalFooterPasswordInput password
      modalFooterSignInButton.click()
    }
  }    
}

在我的测试中,页面调用如下:

def "sigin"() {
    given: "User is at index page"
    at IndexPage

    when: "User signs in"
    signIn "username","password"

    then: "Goes to MyPage"
    at MyPage
  }

在某些情况下,modalFooterSignInButton.click() 发生在用户名完全输入之前,因此测试失败。有没有人遇到过这个?如何在点击激活之前等待输入完成?我正在使用 Geb 0.9.2。

提前致谢。

【问题讨论】:

    标签: grails geb


    【解决方案1】:

    尝试使用 isDisplayed() 而不是 present,因为 present 表示代码中存在,但 isDisplayed() 表示它出现在 UI 中。希望以下内容能解决您的问题!

    waitFor { modalFooterSignInButton.isDisplayed()}
    

    另外,在紧急情况下使用waitFor{},因为我对您的应用一无所知。

    【讨论】:

    • 谢谢。 isDisplayed() 似乎已经解决了这个问题。我目前无法确定,因为问题是间歇性的。但是我已经运行了几次测试并没有遇到这个问题。再次感谢。
    • @user3240644:别担心,伙计!感觉真的很棒,它对你有用!让我们发展 GEB 社区,让它变得更大!
    【解决方案2】:

    你可以这样做:

    waitFor { $(<username-input>).text().contains("username") } // after username is entered.
    waitFor { $(<password-input>).text().contains("password") } // after password is entered.
    

    在调用 modalFooterSignInButton.click() 之前

    此外,这种行为是特定于任何浏览器还是发生在所有浏览器中?

    【讨论】:

    • 谢谢,我试过了,还是不行。我认为它不可能读取文本输入,因为它没有出现在 html 中。我目前只在 Chrome 中测试它,所以我不确定它是否会在其他浏览器中发生。
    • 不是一个理想的解决方案,但如果你在 modalFooterSignInButton.click() 之前添加一个 sleep(500) 或 sleep(1000) 那么它应该可以工作。
    • 我从未见过有人必须等待文本输入完成,所以这种行为让我感到困惑。 :(
    • waitFor 应该足以处理这种情况!即使是 geb 的核心开发者 Erdi 或 Peter,他们总是不鼓励使用 sleep 或相关的东西!我自己从来没有在 geb 中使用过 sleep!
    • @user3240644 即使我没有遇到这种行为,但我一直使用 FireFox 进行测试。 Mamun:同意你的观点,但总是有例外:)
    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 2019-02-22
    • 2017-04-30
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多