PhantomJS无法持久存储localStorage [英] PhantomJS not persisting localStorage consistently
问题描述
我正在尝试创建一个单独的测试用例来测试PhantomJS localStorage
行为,但是在我的testCase中,localStorage似乎不是持久性的.
I am trying to make an isolated test case to experiment with PhantomJS localStorage
behavior, but in my testCase, localStorage does not appear to be persistent.
这是PhantomJS脚本:
Here is the PhantomJS script:
var page = require('webpage').create();
page.open('http://localhost:8080/myapp/test.html', function() {
var x;
x = page.evaluate(function() {
return localStorage.getItem("foo");
});
console.log(x);
phantom.exit();
});
当test.html调用localStorage.setItem("foo", "bar")
时,脚本按我的期望显示"bar".但是,当我从test.html中删除localStorage.setItem
调用时,脚本将打印出一个空行-即使该项目应位于上一次调用的localStorage中.
When test.html has a call to localStorage.setItem("foo", "bar")
the script displays "bar" as i expect. But when I remove the localStorage.setItem
call from test.html, the script prints out a blank line instead - even though the item should be in localStorage from the previous invocation.
从对我的实际应用程序进行的更复杂的测试来看,localStorage 仍然存在.我不确定使用PhantomJS更改localStorage的触发因素是什么.
From more complex tests on my real application, localStorage is persisting. I'm not sure what the trigger is that makes changes to localStorage stick with PhantomJS.
我在Windows上使用PhantomJS 1.9.7和在Linux上使用1.9.1获得了相同的结果.
I got identical results with PhantomJS 1.9.7 on Windows and 1.9.1 on Linux.
推荐答案
它可能与时间相关.如果您太快退出幻像,对localStorage的更改可能不会保留.将phantom.exit()
更改为setTimeout(phantom.exit, 1500)
似乎可行.
It may be timing-related. If you exit phantom too quickly the changes to localStorage may not be persisted. Changing phantom.exit()
to setTimeout(phantom.exit, 1500)
seemed to work.
这篇关于PhantomJS无法持久存储localStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!