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
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();
}
});
});
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) {}");
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...
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
.
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();');
});