By default PhantomJS keeps localStorage settings across sessions. While this is equivalent to a real browser, it can be problematic in a test setup. If a test leaves traces in the localStorage (say, a user is logged in) seemingly random failures can happen (false negatives and positives).
From my observations I guess Chrome and other real browsers are reset across test runs (i.e. they get a new preference directory). I suggest to treat Phantom similarly.
On Mac OS, Phantom writes its localStorage settings by default to /Users/<User>/Library/Application Support/Ofi Labs/PhantomJS. This can be changed with the --local-storage-path flag.
:+1:
Is there anything wrong with calling localStorage.clear() in a before or after callback in the test?
It is certainly advisable to call localStorage.clear() in afterEach. But there are circumstances when accessing window and with it localStorage is not (practically) possible, e.g. in Angular E2E Tests (please correct me if I'm wrong here). Also if afterEach is not run for any reason (exception in Karma, crash, …) you end up with an unknown state that is very well hidden in your system. Perhaps most important of all, right now PhantomJS behaves differently than other test browsers. If a test passes in one browser, it should in the other as long as it is not related to browser differences.
I'm with @zheller I think that one line is pretty explicit.
We could add it to https://github.com/karma-runner/karma/blob/master/static/karma.src.js#L248 however, I think some client side plugins (eg. framework adapters) could use localstorage and this would kill them.
Btw, it's not PhantomJS only, I believe it's the same for other browsers too. The only difference is that Karma starts other browsers with a new profile. Is there any option how to do that with PhantomJS ? That's what should get fixed.
Ad angular e2e test runner, it runs completely in the browser so yep, you can do it.
@trkoch Wouldn't it be advisable to call localStorage.clear() in beforeEach instead? It makes sense for a test not to assume anything about the previous session.
as for localStorage.clear() in beforeEach what to do in case of karma/node crash on CI environment?
Most helpful comment
@trkoch Wouldn't it be advisable to call
localStorage.clear()inbeforeEachinstead? It makes sense for a test not to assume anything about the previous session.