There are multiple ways, but the easiest would be to use the page.addCookie
or phantom.addCookie
functions which PhantomJS provides, but you would have to set the domain (and path). Keep in mind that page.addCookie
has to be done on a loaded page whereas phantom.addCookie
can be done before.
var cookie = "someCookieName=Value; otherName=Value";
var domain = "example.com";
cookie.split(";").forEach(function(pair){
pair = pair.split("=");
phantom.addCookie({
'name': pair[0],
'value': pair[1],
'domain': domain
});
});
casper.start("http://example.com", function(){
// check that cookie was indeed set:
this.capture("screen.png");
}).run();
domain
property is very important. Here is an example script where it works for me. Create a screenshot with theaddCookie
call and one without to see whether there is a difference. – Voluble