Nightwatch: localStorage / webStorage not working

Created on 30 Jun 2015  路  11Comments  路  Source: nightwatchjs/nightwatch

I am trying to test an app that relies on localStorage. Everything works fine when I interact with the browser manually. However, in nightwatch.js instead of the desired string I get a null response when requesting localStorage. This applies both in Chrome and Firefox.

I tried to enable localStore in the nightwatch JSON by assigning "webStorageEnabled" : true in desiredCapabilities like this:

{ "src_folders" : ["tests/functional/tests"],
"output_folder" : "tests/functional/reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "/Library/WebDevelopment/selenium-server/selenium-server-standalone-2.45.0.jar", "log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : { "webdriver.chrome.driver" : "/usr/local/lib/node_modules/chromedriver/lib/chromedriver/chromedriver", "webdriver.ie.driver" : "" }
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : { "enabled" : false, "path" : "" },
"desiredCapabilities": {
"browserName": "chrome",
"webStorageEnabled" : true,
"databaseEnabled" : true,
"applicationCacheEnabled" : true,
"nativeEvents" : true,
"javascriptEnabled": true,
"acceptSslCerts": true
} } }

Is localStorage supposed to work when using nightwatch.js?

enhancement selenium issue

Most helpful comment

Works for me..

module.exports = {

  '@disabled': false,
  '@tags': ['issue', 'localstorage'],

  'Set / Get value from localStorage': function (browser) {
    browser
      .url('http://nightwatchjs.org/')
      .execute(function() {
        window.localStorage.setItem('foo', 'bar');
        return true;
      }, [], function(result) {
        this.assert.ok(result.value);
      })
      .execute(function() {
        return window.localStorage.getItem('foo');
      }, [], function(result) {
        this.assert.equal(result.value, 'bar');
      })
      // Update: commented, since it would break the test
      // as @Mark90 correctly pointed out in comment below
      //
      // .end();
  },


  'Get value from localStorage': function (browser) {
    browser
      .url('http://nightwatchjs.org/')
      .execute(function() {
        return window.localStorage.getItem('foo');
      }, [], function(result) {
        this.assert.equal(result.value, 'bar');
      })
      .end();
  }
};

This only thing is one _MUST NOT_ start e.g. _Chrome_ in _incognito_ mode.

regards
~david

All 11 comments

+1

+1

+1

+1 here. No solutions yet?

anybody has found a fix for this?

Works for me..

module.exports = {

  '@disabled': false,
  '@tags': ['issue', 'localstorage'],

  'Set / Get value from localStorage': function (browser) {
    browser
      .url('http://nightwatchjs.org/')
      .execute(function() {
        window.localStorage.setItem('foo', 'bar');
        return true;
      }, [], function(result) {
        this.assert.ok(result.value);
      })
      .execute(function() {
        return window.localStorage.getItem('foo');
      }, [], function(result) {
        this.assert.equal(result.value, 'bar');
      })
      // Update: commented, since it would break the test
      // as @Mark90 correctly pointed out in comment below
      //
      // .end();
  },


  'Get value from localStorage': function (browser) {
    browser
      .url('http://nightwatchjs.org/')
      .execute(function() {
        return window.localStorage.getItem('foo');
      }, [], function(result) {
        this.assert.equal(result.value, 'bar');
      })
      .end();
  }
};

This only thing is one _MUST NOT_ start e.g. _Chrome_ in _incognito_ mode.

regards
~david

Thanks! @davidlinse I think the important piece was to use window.localStorage and not simply localStorage. I have heard people that have had similar issues.

You're welcome.. Feel free to close this issue.

Please close this issue

Don't know if it helps anyone, but I'll post this just in case.

The second test _Get value from localStorage_ from the example @davidlinse provided did not work for me. Problem was that it spawned a new selenium session for the second test, which naturally does not have the same localStorage contents. Removing the .end() call from the first test _Set / Get value from localStorage_ solves that.

@Mark90: Thanks for pointing out. Sample updated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sgleonardoopitz picture sgleonardoopitz  路  3Comments

lgaticaq picture lgaticaq  路  3Comments

davidlinse picture davidlinse  路  4Comments

antogyn picture antogyn  路  4Comments

MateuszJeziorski picture MateuszJeziorski  路  3Comments