How to get ValidityState Attribute in Katalon

I’ve used the getAttribute to successfully get other attributes such as validationMessage however when I try to use the WebUI.getAttribute method for the validity attribute. It will return null as I tried to print it out. I can’t access anything within the validityState object such as tooShort. It will still return null.

So far as I can tell from your screenshot, validityState is a property, not an attribute. Mostly, Katalon/Selenium will try to smooth over the difference and give you what you want. However, sometimes, it fails.

In your case, you have the added problem that validity is an object containing other properties with their own values. I don’t believe getAttribute can deal with that. You’ll need to retrieve the property/values using JavaScript.

Question: do you want the whole object returned to Groovy? Or a specific value property?

HI @Russ_Thomas thank you for your response. So yes, I am trying to access the object’s properties such as “tooShort”. Apologies if this a stupid question but I thought you could only use Java/Groovy in scripting katalon test cases, so how would I use JavaScript?

How would I return the object to Groovy and access a specific property like “tooShort”?

You can use JavaScript, too. But it looks a little strange at first - you need to pass the code as a string to the WebUI.executeJavaScript API. Katalon will then inject it into the browser for execution and return the result to Groovy. Like this:

String js = "return document.querySelector(your_element).validity.tooShort;"
def tooShort = WebUI.executeJavaScript(js, null)

You will need to provide a suitable replacement for your_element. I couldn’t see enough of the HTML to figure out what that argument should be.