Protractor - empty local storage

前端 未结 5 737
臣服心动
臣服心动 2020-12-03 13:30

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 ot

相关标签:
5条回答
  • 2020-12-03 13:54

    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();
        }
      });
    });
    
    0 讨论(0)
  • 2020-12-03 13:54

    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) {}");
    
    0 讨论(0)
  • 2020-12-03 14:02

    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...

    0 讨论(0)
  • 2020-12-03 14:03

    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.

    0 讨论(0)
  • 2020-12-03 14:08

    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();');
    });
    
    0 讨论(0)
提交回复
热议问题