PhantomJS not persisting localStorage consistently

北城余情 提交于 2019-12-09 03:41:40

问题


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.

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();
});

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.

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.

I got identical results with PhantomJS 1.9.7 on Windows and 1.9.1 on Linux.


回答1:


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.



来源:https://stackoverflow.com/questions/23478891/phantomjs-not-persisting-localstorage-consistently

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!