public static ExpectedCondition < Boolean > textMatches ( final By locator , final Pattern pattern ) { return new ExpectedCondition < Boolean > ( ) { private String currentValue = null ; @ Override public Boolean apply ( WebDriver driver ) { try { currentValue = driver . findElement ( locator ) . getText ( ) ; return pattern . matcher ( currentValue ) . find ( ) ; } catch ( Exception e ) { return false ; } } @ Override public String toString ( ) { return String . format ( "text found by %s to match pattern \"%s\". Current text: \"%s\"" , locator , pattern . pattern ( ) , currentValue ) ; } } ; }
An expectation for checking WebElement with given locator has text with a value as a part of it