0

使用 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","re​​quest":{"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","re​​lative":"/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?如果不是,我该如何解决这个问题?

4

2 回答 2

0

经过一番思考,我设法解决了这个问题。

通常,可以使用 JavaScript 访问 localStorage。此处描述的问题是由于其他原因而发生的。这是我的流程:

1)初始化WebDriver
2)访问localStorage
3)做driver.get(url)

问题是,driver.get(url)必须在访问 localStorage 之前发生。这解决了这个问题。

这是我的 LocalStorage 驱动程序实现(在 Scala 中),如果您需要的话:LocalStorageDriver

于 2014-09-04T15:09:41.233 回答