I am currently covering the company's internal project (something like an employee management system) using selenium tests. Thus, there are many required contributions in the form. If any of them is empty, then the whole form is checked. And the text labels near the input become (become) red, and the corresponding validator message appears. So, my approach to validating the validator is pretty simple: - I wrote a colored text method to get the color of the labels near the input (if it is red, then the validator worked on submitting the form)
public String jsGetColor(String css){ JavascriptExecutor js = (JavascriptExecutor) driver; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("var x=$(\'"+css+"\');"); stringBuilder.append("return x.css('color')"); String res= (String) js.executeScript(stringBuilder.toString()); return res; }
Also I have to add that if the validator is working, an error message appears. Thus, you can find the locator of this message and compare the corresponding text. In other cases, when the validator does not work, for example. when submitting a form, the validator message style will look like "display: none". Therefore, to understand that everything is in order, you can simply check if mesage is visible:
driver.findElement(By.cssSelector(...blabla...)).isDisplayed()
And the last one. If you want to check the validator message.
public void addNewTimeOffValidatorsMessagesCheck(String expectedValidationMsg) { Assert.assertTrue(driver.findElement(By.cssSelector(propertyKeysLoader("rms.home.timeoffsportlet.addnewtimeoff.validationmessage"))).getText().trim().equals(expectedValidationMsg)); } addNewTimeOffValidatorsMessagesCheck("Timeoff type was not selected!");
I hope now it will become more clear to you)
source share