javascript - Disabling LocalStorage on PhantomJS for clean testing? -
just started working casperjs few days ago.
i create script today test how localstorage behaves , if disable because that's necessary in order keep tests affecting each other.
background
i'm working on backbone wizard ask value on first page. when click continue button, saves value localstorage displays on second page.
i'm using casperjs test <script.js>
both , without --local-storage-quota=0
.
first attempt
wrote casperjs script following:
- load page
- check contents of model (empty, should be)
- click continue
- check contents of model after page 2 loads (contains value, should)
- open page 2 directly new page using casper.thenopen()
- check contents of model after page 2 loads
if localstorage enabled, step 6 should have same result step 4 (value exists in model).
if localstorage disabled, step 6 should have same result step 2 (model empty).
everytime ran script, determined localstorage enabled. '--local-storage-quota=0' parameter made no difference.
second attempt
at point, decided determine if localstorage attached specific casper instance. if so, work around creating new casper instance every test, thereby starting clean slate.
var casper = require( 'casper' ); casper = casper.create(); casper.test.begin( 'test local storage, part 1', 0, function suite (test) { ... }); casper = casper.create(); casper.test.begin( 'test local storage, part 2', 0, function suite (test) { ... });
however, second test suite never runs. don't know if casper wasn't intended have multiple instances made in same script, or if i'm organizing incorrectly.
addendum
i should add test suites end following step in case it's relevant:
casper.run( function () { test.done(); casper.exit(); });
the docs specify test.done() required. however, test scripts hang forever until added call casper.exit().
you cannot disable localstorage or sessionstorage in phantomjs. advisable clean execution environment every time run test. suggest adding general test function complete setup following:
function begintest(casper, description, num, tests){ function clearstorage(){ casper.evaluate(function() { localstorage.clear(); sessionstorage.clear(); }); } // select between 2 possible signatures , queue casper test if (typeof num == "number" && typeof tests == "function") { casper.test.begin(description, num, function suite(test){ phantom.clearcookies(); casper.start(config.baseurl, clearstorage); tests(test); casper.run(function(){ test.done(); }); }); } else if (typeof num == "function" && !tests) { casper.test.begin(description, function suite(test){ phantom.clearcookies(); casper.start(config.baseurl, clearstorage); num(test); casper.run(function(){ test.done(); }); }); } }
you can invoke with
begintest(casper, 'test local storage, part 1', 0, function suite(test){ /* ... */ }); begintest(casper, 'test local storage, part 2', 0, function suite(test){ /* ... */
Comments
Post a Comment