如何使用 Geb 验证错误消息

How to verify error messages using Geb

如何捕获网页上生成的错误消息(例如,在未输入任何用户名或密码的情况下按下登录按钮时生成的错误消息),如何提取此错误消息 "Username is required" 使用盖布。 我正在使用基于 geb groovy 的自动化

所以您首先要为您的页面建模,然后获取您要断言的错误的定位器已显示。请注意,您希望将其设置为 required: false,因为您不希望它在您首次登陆页面时显示。

带有示例页面错误的页面,ID 为 #pageError:

class MyPage extends Page
{
    static url = "/mypageurl"
    static at = {
        title == "My Page"
    }

    static content = {
        pageError (required: false) { $('#pageError') }
        submitButton { $('#mySubmitButton') }
    }

    def submitForm() {
      submitButton.click()
     }
}

然后你有你的测试:

def "If I try click Submit without filling out all form details show me an error message"()
{
    when: "I click Submit without completing all form fields"

        def myPage = to(MyPage)
        myPage.submitForm()

    then: "I am shown an error message on the same page"

        myPage.pageError.displayed
}

编辑:

注意到你提到获取错误的内容,你会调用它来获取文本:

myPage.pageError.text()