public static ExpectedCondition < Object > jsReturnsValue ( final String javaScript ) { return new ExpectedCondition < Object > ( ) { @ Override public Object apply ( WebDriver driver ) { try { Object value = ( ( JavascriptExecutor ) driver ) . executeScript ( javaScript ) ; if ( value instanceof List ) { return ( ( List < ? > ) value ) . isEmpty ( ) ? null : value ; } if ( value instanceof String ) { return ( ( String ) value ) . isEmpty ( ) ? null : value ; } return value ; } catch ( WebDriverException e ) { return null ; } } @ Override public String toString ( ) { return String . format ( "js %s to be executable" , javaScript ) ; } } ; }
An expectation for String value from javascript