如何在Geb中检查输入的约束有效性?

时间:2016-05-25 21:43:03

标签: javascript testing automated-tests geb

我正在重写一些测试以检查电子邮件输入上的约束验证。我试图检查以下JavaScript:

document.getElementById('theEmailId').validity.typeMismatch

当我用普通的旧JavaScript测试时,一切都很好。我得到" true",这是电子邮件地址无效时的预期答案。当我在Geb中尝试这样做时,我会遇到各种各样的麻烦。当我尝试

    def isValid = js.exec("document.getElementById('theEmailAddress')")

断言是isValid == true

我通过以下输出得到了spock比较错误:

Condition not satisfied:

isValid == true
|       |
null    false

当我尝试

         def isValid = js.exec(theEmailAddress,
            """
                return \$(this).validity;
            """
    )

我使用以下报告得到了java.lang.IllegalArgumentException错误:

java.lang.IllegalArgumentException: Argument is of an illegal type: geb.content.TemplateDerivedPageContent
at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:81)
at com.google.common.collect.Iterators$8.transform(Iterators.java:817)
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at com.google.common.collect.Iterators.addAll(Iterators.java:365)
at com.google.common.collect.Lists.newArrayList(Lists.java:162)
at com.google.common.collect.Lists.newArrayList(Lists.java:146)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:572)
at geb.js.JavascriptInterface.execjs(JavascriptInterface.groovy:37)
at geb.js.JavascriptInterface.exec(JavascriptInterface.groovy:67)
at com.ag.functionaltest.crs.specs.LoginGebSpec.The customer can not register using an email without @(LoginGebSpec.groovy:33)

我不知所措。我尝试过browser.driver.executeScript,js.exec,js。,一切都返回null或错误。关于我可以做些什么才能让它发挥作用的任何指示?

1 个答案:

答案 0 :(得分:0)

解决了!以下是最终工作的内容:

    given: "The customer goes to login page"
    to LoginPage
    waitFor { LoginPage }

    when: "The customer tries to register with an email without domain"
    theEmailId.value("anemail@.com")

    and: "The customer clicks the continue button"
    continueButton.click()
    def isValid = js."window.document.getElementById('theEmailAddress').validity['typeMismatch']"

    println isValid

    then: "The customer should see this error"

    assert isValid == true