1
votes

I'm doing e2e tests with the help of phantom and protractor, however each time I do a change on my code (not e2e test code),it seems that phantom was not taking the new changes or cleaning localStorage and SessionStorage. I think it could be cache but I am not sure.

I tried to add the next sentence on my protractor.config file in order to clean LocalStorage.

 onPrepare: function () { browser.executeScript("return window.localStorage.clear();"); }

However , It didn't work , instead of that I got this error

var template = new Error(this.message); ^ UnknownError: {"errorMessage":"SecurityError: DOM Exception 18","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"58","Content-Type":"application/json; charset=utf-8","Host":"localhost:9781","User-Agent":"Apac he-HttpClient/4.5.1 (Java/1.7.0_79)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"return window.localStorage.clear();\",\"args\":[]}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative ":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/dc9ce280-e15f-11e5-911a-4b92e3de49f0/execute"}} Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:15:17' Driver info: driver.version: unknown

1

1 Answers

0
votes

Like you, I've also seen plenty advice to call window.localStorage.clear() to clear local storage in PhantomJS. But here's the deal -- if you're doing testing on a remote domain, this code will raise a security exception, because browsers (should not) allow clearing local storage data across domains.

What you can do however, is control where the data file where local storage is written to on your testing machine is located. That's going to be --local-storage-path (command line argument) or (in C#, Java similar):

PhantomJSDriverService service =
                       PhantomJSDriverService.CreateDefaultService();
//IMPORTANT: avoid the common pitfall of trying to specify a file name!!
// provide the path to the folder only
service.LocalStoragePath = @"path\to\where\I\want\local-storage\saved;

var driver = new PhantomJSDriver(service);

Local storage files are saved with the name [domain].localstorage. Before you run each test, check for the existence of the file *.localstorage in the folder you specified, and delete it. That will clear local storage for you.