Protractor - empty local storage

China☆狼群 提交于 2019-12-17 13:51:06

问题


I'm using Protractor (with Jasmine) to test my AngulaJs application.

As result of some of my action I get some data saved in the localStorage. Now I need to test other case, so I need to empty my storage (or better delete only some items) but If I try to run:

browser.executeScript('localStorage.removeItem("config");');

I get the following error:

UnknownError: <unknown>: Failed to read the 'localStorage' property from 'Window': Storage is disabled inside 'data:' URLs.
  (Session info: chrome=35.0.1916.153)
  (Driver info: chromedriver=2.10.267517,platform=Mac OS X 10.9.2 x86_64)

Any idea on how to solve?

Thanks in advance


回答1:


Another potential solution is to put any state clearing in an afterEach, which will run after any test is run: (see https://github.com/angular/protractor/issues/188)

afterEach(function() {
    browser.executeScript('window.sessionStorage.clear();');
    browser.executeScript('window.localStorage.clear();');
});



回答2:


It's because you haven't navigated to a valid url yet. Do a browser.get('/foo') before trying to interact with page objects like localStorage.




回答3:


I solved this issue by checking the window.location before attempting to clear/modify sessionStorage or localStorage.

If a page has not been loaded then window.location.hostname will equal the empty string ''. So if you get the emptystring, then don't attempt to interact with sessionStorage or localStorage.

Here's some (ES6) code I used in my protractor suite to prevent this error. Note it's a cucumber-js After function, but it is still executed from protractor using chrome, and it demonstrates what you need to do to avoid this error:

this.After(function(scenario) {

  function getWindowLocation() {
    return window.location;
  }

  function clearStorage() {
    window.sessionStorage.clear();
    window.localStorage.clear();
  }

  return browser.executeScript(getWindowLocation).then(function(location) {
    // NB If no page is loaded in the scneario then calling clearStorage will cause exception
    // so guard against this by checking hostname (If no page loaded then hostname == '')
    if (location.hostname.length > 0) {
      return browser.executeScript(clearStorage);
    }
    else {
      return Promise.resolve();
    }
  });
});



回答4:


I haven't found a cool way to do that, but if I run my statement

 browser.executeScript("localStorage.removeItem('config');")

within a it('description') statement it works. Example:

it('should compile and save base config for billing',function(){
    browser.executeScript("localStorage.removeItem('config');")

    //my test
});

This remove the item named config, and so my test works, but while searching and talking about this issue the main response I got is:

"localStorage is not you product, so you don't need (read: you must not) test it. The right way is to mock it and inject it contents when they are needed"

I'm still looking into this to understrand how, meanwhile I think that a not philosophically perfect test is still better than nothing so..

Hope this helps...




回答5:


If there's still a problem with the localStorage when executing a "removeItem()", you can use a try-catch, like:

browser.executeScript("try {localStorage.removeItem('access_token');} catch(exception) {}");


来源:https://stackoverflow.com/questions/24205856/protractor-empty-local-storage

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