简体   繁体   中英

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:

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

When I test this with plain old JavaScript, everything is fine. I get "true", which is the expected answer when the email address is invalid. When I try to do this within Geb, I get all sorts of trouble. When I try

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

with the assertion being that isValid == true

I get the spock comparison error with the following output:

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: 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. 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        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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