如何使用Geb验证错误消息

时间:2018-07-19 10:00:06

标签: selenium groovy geb

如何捕获在网页上生成的错误消息(例如,在不插入任何用户名或密码的情况下按下登录按钮时生成的错误消息),如何使用geb提取此错误消息“需要用户名”。 我使用基于geb groovy的自动化系统

1 个答案:

答案 0 :(得分:2)

因此,您将首先为页面建模,并获取要断言的错误的定位器。请注意,您希望将此设置为必填: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()