Reading Values and Page State

Useful Reads

String state = (String) js.executeScript("return document.readyState;");           // "complete"
Boolean jqDone = (Boolean) js.executeScript("return window.jQuery ? jQuery.active == 0 : true;");
String token = (String) js.executeScript("return localStorage.getItem('cartId');");
Long images = (Long) js.executeScript(
        "return Array.from(document.images).filter(i => !i.complete || i.naturalWidth === 0).length;");
System.out.println(state + " | ajax done=" + jqDone + " | cartId=" + token + " | broken images=" + images);

Example

Output:

Run PageStateTest.main()

Example output

complete | ajax done=true | cartId=c-58321 | broken images=0

Highlighting Elements for Debugging

js.executeScript("arguments[0].style.outline = '3px solid red';", driver.findElement(By.id("add-to-cart")));

Useful when recording a demo video or taking a screenshot of a failure: the element the test used is outlined in red.