nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.85k stars 1.35k forks source link

localStorage / webStorage not working #531

Closed HeikoMueller closed 7 years ago

HeikoMueller commented 9 years ago

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?

okmttdhr commented 8 years ago

+1

NiltiakSivad commented 8 years ago

+1

zoness32 commented 8 years ago

+1

LasaleFamine commented 8 years ago

+1 here. No solutions yet?

bboydflo commented 7 years ago

anybody has found a fix for this?

davidlinse commented 7 years ago

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

bboydflo commented 7 years ago

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.

davidlinse commented 7 years ago

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

lauterry commented 7 years ago

Please close this issue

Mark90 commented 6 years ago

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.

davidlinse commented 6 years ago

@Mark90: Thanks for pointing out. Sample updated.

hongdeyuan commented 2 years ago

it would be better to develop a setlocalstorage custom command;

/**