Clear localstorage with Casperjs

前端 未结 3 406
温柔的废话
温柔的废话 2021-01-14 23:45

I\'m using casperjs to do some testings of a backbonejs application using localstorage.

My problem is that I can not clear the localstorage when testing with casperj

相关标签:
3条回答
  • 2021-01-15 00:03

    What about running this after loading a page ?

    casper.evaluate(function() {
      localStorage.clear();
    }, {});
    
    0 讨论(0)
  • 2021-01-15 00:15

    PhantomJS does not clear the localstorage correct. localStorage.clear(); does not work (At least not reliable)

    You have to delete the '.localstorage' File on the Filesystem. The fs.remove Command has to be called before you open the Page. Phantomjs locks that file while the page is open.

    I did it in our Project like that:

    function clearStorage() {
    var fs = require('fs');
    var system = require('system');
    var myDomain = [I get this value from the commandline Parameter I give to CasperJS];
    
    if(system.os.name === 'windows') {
        var userName = system.env['USERPROFILE'].split('\\')[2];
        var localstoragePath = 'C:/Users/' + userName + '/AppData/Local/Ofi Labs/PhantomJS/';
        var localStorageFilename = myDomain.replace('://', '_').replace('/', '') + '_0.localstorage';
    } else {
        var userName = system.env['USER'];
        var localstoragePath = '/home/' + userName + '/.local/share/Ofi Labs/PhantomJS/';
        var localStorageFilename = myDomain.replace('://', '_') + '_0.localstorage'; //Linux does not have the last "/" so no replace needed for that
    }
    
    if(fs.exists(localstoragePath + localStorageFilename)) {
        fs.remove(localstoragePath + localStorageFilename, function(err) {
            if (err) {
                casper.echo(err);
            }
            casper.echo("File deleted successfully!");
        });
    }
    

    }

    0 讨论(0)
  • 2021-01-15 00:28

    I found workaround for this problem by deleting file *.localstorage in one of the following directories:

    Windows c:\Users\YOUR_USER\AppData\Local\Ofi Labs\PhantomJS\

    Mac OS X: /Users/YOUR_USER/Library/Application Support/Ofi Labs/PhantomJS

    Linux: /home/YOUR_USER/.local/share/data/Ofi Labs/PhantomJS

    0 讨论(0)
提交回复
热议问题