1

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="[email protected]">

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)

1 Answer 1

1

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, i did the same way and it works well. Thanks for the idea. String expErrorAttribute = "Input__field--empty-warning"; wait.until(ExpectedConditions.attributeContains(inputEmail, "class", expErrorAttribute)); return inputEmail.getAttribute("class").contains(expErrorAttribute);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.