HTML5 form validation

15 views
Skip to first unread message

SiKing

unread,
Jun 24, 2022, 12:33:51 PM6/24/22
to Selenium Users
Has anyone managed to get at HTML5 form validation.

If you click on "Submit form" (without filling anything out), you will see the "Please fill out this field." popup.
Can you detect the popup with Selenium?

SiKing

unread,
Jun 24, 2022, 4:46:32 PM6/24/22
to Selenium Users
Got it figured out. You can use something like this:

public String getPopupError() {
    JavascriptExecutor executor = (JavascriptExecutor) driver;

    // Note that before the form is submitted, arguments[0].validity.valid == false!
    if (!(Boolean) executor.executeScript("return arguments[0].validity.valid;", username)) {
        log.warn("Username: {}", executor.executeScript("return arguments[0].validationMessage;", username));
        return (String) executor.executeScript("return arguments[0].validationMessage;", username);
    }

    if (!(Boolean) executor.executeScript("return arguments[0].validity.valid;", password)) {
        log.warn("Password: {}", executor.executeScript("return arguments[0].validationMessage;", password));
        return (String) executor.executeScript("return arguments[0].validationMessage;", password);
    }

    return null;
}

In the above case username and password are WebElement.
Reply all
Reply to author
Forward
0 new messages