Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling LocalStorage on PhantomJS for clean testing?

Just started working with CasperJS a few days ago.

I create a script today to test how LocalStorage behaves and if I could disable it because that's necessary in order to keep tests from affecting each other.

Background

I'm working on a Backbone wizard that ask for a value on the first page. When you click the Continue button, it saves the value to LocalStorage then displays it on the second page.

I'm using casperjs test <script.js> both with and without --local-storage-quota=0.

First Attempt

Wrote a CasperJS script that does the following:

  1. load Page
  2. check the contents of the model (empty, as it should be)
  3. click Continue
  4. check the contents of the model after Page 2 loads (contains the value, as it should)
  5. open Page 2 directly as a new page using casper.thenOpen()
  6. check the contents of the model after Page 2 loads

If LocalStorage is enabled, step 6 should have the same result as step 4 (value exists in model).

If LocalStorage is disabled, step 6 should have the same result as step 2 (model is empty).

Everytime I ran the script, I determined that LocalStorage was enabled. The '--local-storage-quota=0' parameter made no difference.

Second Attempt

At that point, I decided to determine if the LocalStorage was attached to a specific Casper instance. If so, then I could work around this by creating a new Casper instance for every test, thereby starting with a clean slate.

var Casper = require( 'casper' );

casper = Casper.create();
casper.test.begin( 'test for Local Storage, part 1', 0, function suite (test) { ... });

casper = Casper.create();
casper.test.begin( 'test for Local Storage, part 2', 0, function suite (test) { ... });

However, the second test suite never runs. I don't know if Casper wasn't intended to have multiple instances made in the same script, or if I'm just organizing it incorrectly.

Addendum

I should add that all test suites end with the following step in case it's relevant:

casper.run( function () {
    test.done();
    casper.exit();
});

The docs only specify that test.done() is required. However, my test scripts would hang forever until I added the call to casper.exit().

like image 889
odigity Avatar asked Aug 23 '13 17:08

odigity


1 Answers

You cannot disable localStorage or sessionStorage in phantomjs. It is however advisable to clean your execution environment every time you run a test. I suggest adding general test function with complete setup like the following:

function beginTest(casper, description, num, tests){
    function clearStorage(){
        casper.evaluate(function() {
            localStorage.clear();
            sessionStorage.clear();
        });
    }
    // Select between two possible signatures and queue a 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 then invoke this with

beginTest(casper, 'test for Local Storage, part 1', 0, function suite(test){ /* ... */ });
beginTest(casper, 'test for Local Storage, part 2', 0, function suite(test){ /* ... */ 
like image 56
Artjom B. Avatar answered Oct 01 '22 08:10

Artjom B.