简体   繁体   English

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

[英]How can I check for an input's constraint validity within Geb?

I'm rewriting some tests to check constraint validation on email inputs. 我正在重写一些测试,以检查对电子邮件输入的约束验证。 I'm trying to check the following JavaScript: 我正在尝试检查以下JavaScript:

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

When I test this with plain old JavaScript, everything is fine. 当我使用普通的旧JavaScript进行测试时,一切都很好。 I get "true", which is the expected answer when the email address is invalid. 我得到“ true”,这是电子邮件地址无效时的预期答案。 When I try to do this within Geb, I get all sorts of trouble. 当我尝试在Geb中执行此操作时,会遇到各种各样的麻烦。 When I try 当我尝试

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

with the assertion being that isValid == true 断言isValid == true

I get the spock comparison error with the following output: 我收到以下输出的spock比较错误:

Condition not satisfied:

isValid == true
|       |
null    false

When I try 当我尝试

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

I get a java.lang.IllegalArgumentException error with the following report: 我收到带有以下报告的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)

I'm at a loss. 我很茫然。 I've tried browser.driver.executeScript, js.exec, js., everything returns null or an error. 我尝试过browser.driver.executeScript,js.exec,js。,所有内容都返回null或错误。 Any pointers on what I could possibly do to get this to work? 关于我可以做些什么的任何指示?

Solved it! 解决了! Here's what ended up working: 最终的工作如下:

    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        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM