使用 PhantomJsDriver (GhostDriver) 测试我的 Web 应用程序。尝试使用以下调用访问本地存储值:
(String) js.executeScript(String.format("return localStorage.getItem('%s');", key));
在 webDriver 初始化中,我正在设置功能:
DesiredCapabilities dCaps = new DesiredCapabilities()
dCaps.setJavascriptEnabled(true)
dCaps.setCapability("webStorageEnabled", true)
return new PhantomJSDriver(dCaps)
在尝试执行上面的 JS 时收到 WebDriverException,看起来好像webStorageEnabled
从未设置为 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"}} 命令持续时间或超时:8 毫秒构建信息:版本:'2.39.0',修订:'14fa800511cc5d66d426e08b0b2ab926c7ed7398',时间:'2013-12- 16 13:18:38'密码":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{}, "chunks":["execute"]},"urlOriginal":"/session/5f67ce30-310a-11e4-83c7-6d52cfce6b64/execute"}} 命令持续时间或超时:8 毫秒构建信息:版本:'2.39.0' ,修订:'14fa800511cc5d66d426e08b0b2ab926c7ed7398',时间:'2013-12-16 13:18:38'密码":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{}, "chunks":["execute"]},"urlOriginal":"/session/5f67ce30-310a-11e4-83c7-6d52cfce6b64/execute"}} 命令持续时间或超时:8 毫秒构建信息:版本:'2.39.0' ,修订:'14fa800511cc5d66d426e08b0b2ab926c7ed7398',时间:'2013-12-16 13:18:38'/session/5f67ce30-310a-11e4-83c7-6d52cfce6b64/execute"}} 命令持续时间或超时:8 毫秒构建信息:版本:'2.39.0',修订:'14fa800511cc5d66d426e08b0b2ab926c7ed7398',时间:'2013-12-16 13 :18:38'/session/5f67ce30-310a-11e4-83c7-6d52cfce6b64/execute"}} 命令持续时间或超时:8 毫秒构建信息:版本:'2.39.0',修订:'14fa800511cc5d66d426e08b0b2ab926c7ed7398',时间:'2013-12-16 13 :18:38'
驱动程序信息: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,takeScreenshot=false}] 在 sun.reflect .NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
我遇到的问题可能是什么原因?是否可以为 PhantomJSDriver 设置 webStorageEnabled?如果不是,我该如何解决这个问题?