public static ExpectedCondition < Boolean > textToBePresentInElement ( final WebElement element , final String text ) { return new ExpectedCondition < Boolean > ( ) { @ Override public Boolean apply ( WebDriver driver ) { try { String elementText = element . getText ( ) ; return elementText . contains ( text ) ; } catch ( StaleElementReferenceException e ) { return null ; } } @ Override public String toString ( ) { return String . format ( "text ('%s') to be present in element %s" , text , element ) ; } } ; }
An expectation for checking if the given text is present in the specified element .