Michael T.
Michael T.

Reputation: 197

Deleting cookies file after CasperJS script finished

I want to use fs library to create a file at the begin of my script and delete it at the end. The cookies.txt file is created at the begin but not deleted at the end of the script..

phantom.cookiesEnabled = true;
var fs = require('fs');
var cookies = JSON.stringify(phantom.cookies);
fs.write("cookies.txt", cookies, 777); // THIS WORKS !!

casper.start('http://google.fr/', function(){
    this.viewport(320, 480);
});
// my script ....
casper.then(function(){
    fs.remove("cookies.txt"); // THIS NOT WORKS !!
});
casper.run(function() {
    this.test.done();
    this.exit();
});

The command I write to start the script

casperjs test lbc.js --cookies-file=cookies.txt --web-security=no

Upvotes: 0

Views: 2067

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

It works, but it happens so fast that you can't see that it really works.

You start casper with the cookies-file option (see the docs). This means that you want the cookies to persist after the script has exited to use them at the next invocation.

When you exit casper (and phantom for that matter) the cookies file is written to disk, but this happens after you deleted the file with fs.remove("cookies.txt");. So the file is actually deleted and then rewritten.

The question is, what do you want to do?

Upvotes: 1

Related Questions