How to check if an HTML5 validation was triggered using selenium?

前端 未结 4 1823
执笔经年
执笔经年 2020-12-14 19:43

If testing my webapp using Selenium. All the form validation in the webapp is done using HTML5 form validations. Is there any way to assert if a form/input validation was tr

4条回答
  •  孤城傲影
    2020-12-14 20:11

    A search like this does the trick in HTML5-aware browsers (using Java and WebDriver, but should be usable anywhere else, too):

    // looks for an element that has been marked as required on a submit attempt
    WebElement elem1 = driver.findElement(By.cssSelector("input:required"));
    // looks for an element that has been marked as invalid on a submit attempt
    WebElement elem2 = driver.findElement(By.cssSelector("input:invalid"));
    

    The CSS pseudo classes available are:

    • :required
    • :optional
    • :valid
    • :invalid

提交回复
热议问题