Way 1: First, write the following method:
private boolean isTextPresent(String text){
try{
boolean b = driver.getPageSource().contains(text);
return b;
}
catch(Exception e){
return false;
}
}
Now do assertion whether the expected message is present or not on the page by calling the above method:
assertTrue(isTextPresent("Value is required and can't be empty"), "Msg is absent/wrong/misspelled");
Way 2: Another way is as follow:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
private StringBuffer verificationErrors = new StringBuffer();
try {
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Value is required and can't be empty[\\s\\S]*$"));
} catch (Error e) {
verificationErrors.append(e.toString());
}
}