Baking some cookies

How about creating our own cookies? Normally, users do not push the creation of cookies, but in PhantomJS, we can do that in our script to change the browser behavior.

Let us modify our previous code, and before displaying the list of cookies, we will add a "username" property to the cookie.

page.open(url, function(status) {
  if ( status === "success" ) {
    phantom.addCookie({
      'name'    : 'username',
      'value'   : 'aries',
      'domain'  : '.yahoo.com'
    });
    console.log(JSON.stringify(phantom.cookies, null, 4));
    phantom.exit(0);
  }
});

We will use the addCookie function from the phantom object. The function expects an object to be passed in JSON format. However, the name, value, and domain property are the only properties required to be present; ...

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.