Using PhantomJsDriver (GhostDriver) for testing my web application. Trying to access the local storage values using calls like:
(String) js.executeScript(String.format("return localStorage.getItem('%s');", key));
In webDriver initialisation I'm setting the capability:
DesiredCapabilities dCaps = new DesiredCapabilities()
dCaps.setJavascriptEnabled(true)
dCaps.setCapability("webStorageEnabled", true)
return new PhantomJSDriver(dCaps)
Receiving WebDriverException while trying to execution the JS above, from which it looks like the webStorageEnabled
have been never set to true:
org.openqa.selenium.WebDriverException: {"errorMessage":"SECURITY_ERR: DOM Exception 18","request":{"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"98","Content-Type":"application/json; charset=utf-8","Host":"localhost:20884"},"httpVersion":"1.1","method":"POST","post":"{\"args\":[],\"script\":\"return localStorage.getItem('cartId_136d7735-13d7-8178-8abf-010beb62f8cf');\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/5f67ce30-310a-11e4-83c7-6d52cfce6b64/execute"}} Command duration or timeout: 8 milliseconds Build info: version: '2.39.0', revision: '14fa800511cc5d66d426e08b0b2ab926c7ed7398', time: '2013-12-16 13:18:38'
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver Capabilities [{platform=MAC, acceptSslCerts=false, javascriptEnabled=true, browserName=phantomjs, rotatable=false, driverVersion=1.1.0, locationContextEnabled=false, version=1.9.7, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=false, browserConnectionEnabled=false, webStorageEnabled=false, nativeEvents=true, proxy={proxyType=direct}, applicationCacheEnabled=false, driverName=ghostdriver, takesScreenshot=false}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
What could be the reason for the issues I experiencing? Should it be possible to set webStorageEnabled for PhantomJSDriver? If no, how can I work around this issue?
After some thinking I have managed to solve this issue.
In general, it's possible to access localStorage using using JavaScript. The issue described here happened because of something else. Here is the flow I had:
1) Initialize WebDriver
2) Access localStorage
3) Doing driver.get(url)
The problem is, driver.get(url)
must happen before accessing localStorage. That solved the issue.
Here is my LocalStorage driver implementation (in Scala), if you need one: LocalStorageDriver