I found another solution. For me this worked like a charm and it'sIf your driver implements WebStorage
you can invoke
driver.getSessionStorage().clear();
driver.getLocalStorage().clear();
In order to make sure that the prettiest way.driver does implement WebStorage
the idiom is
if (driver instanceof WebStorage) {
WebStorage webStorage = (WebStorage)driver;
webStorage.getSessionStorage().clear();
webStorage.getLocalStorage().clear();
}
or better
if (!(driver instanceof WebStorage)) {
throw new IllegalArgumentException("This test expects the driver to implement WebStorage");
}
WebStorage webStorage = (WebStorage)driver;
webStorage.getSessionStorage().clear();
webStorage.getLocalStorage().clear();