سؤال

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