문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top