简体   繁体   中英

how to validate the UI error with no error message in selenium

my page contains ui fields which has the typical validations on it. but the thing is, instead of displaying a message explicitly, it highlights the field border with red color. might be some css changes might happen behind based on the validations. while in scripting, how can i validate/assert the filed shows me a validation error here. lets say 'Email' field is a mandatory. When i save the form with empty value, it highlights the Email filed with Red color border. Any help here on how to validate.

When it has value the html looks like,

<input aria-label="email" aria-atomic="true" aria-hidden="false" aria-invalid="false" aria-readonly="false" autocomplete="off" class="Input__field Input__field--with-trailing-icon" data-has-errors="false" id="email" inputmode="email" maxlength="100" name="email" novalidate="" placeholder="" required="" tabindex="-1" type="email" value="test@gmail.com">

When it is empty,

<input aria-label="email" aria-atomic="true" aria-hidden="false" aria-invalid="true" aria-readonly="false" autocomplete="off" class="Input__field Input__field--empty-warning Input__field--with-trailing-icon" data-has-errors="true" id="email" inputmode="email" maxlength="100" name="email" novalidate="" placeholder="" required="" tabindex="-1" type="email" value="">

(as i am into client server, i cannot post a screen-print of the UI filed)

When highlighted the element has class name Input__field--empty-warning also data-has-errors="true" .
So you can pass that element to method like this:

public void validateClassInElement(WebElement element, String className){
    String classes = element.getAttribute("class");
    Assert.assertTrue(classes.contains(className));
}

Or

public void validateAttributeValueInElement(WebElement element, String att, String expectedValue){
    String actualValue = element.getAttribute(att);
    Assert.assertTrue(actualValue.contains(expectedValue));
}

So it can be:

WebElement emailInput = driver.findElements(By.xpath("//input[@id='email']"));
validateClassInElement(emailInput,"Input__field--empty-warning");

Alternatively this can be done with WebDriverWait ExpectedConditions

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//input[@id='email']"), "class", "Input__field--empty-warning"));

or

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//input[@id='email']"), "data-has-errors", "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