Managing cookies with the phantom object
In this recipe, we will discuss how to work with cookies in PhantomJS. The phantom
object exposes two properties (cookies
and cookiesEnabled
) and three methods (addCookie
, clearCookie
, and deleteCookie
) that we can use to inspect and manipulate cookies at runtime.
Getting ready
To run this recipe, we may wish to run PhantomJS with persistent cookies using the cookies-file
command-line argument.
The script in this recipe is available in the downloadable code repository as recipe02.js
under chapter02
. If we run the provided example script, we must change to the root directory for the book's sample code.
Lastly, the script in this recipe runs against the demo site that is included with the cookbook's sample code repository. To run the demo site, we must have Node.js installed. In a separate terminal, change to the phantomjs-sandbox
directory (in the sample code's directory) and start the app with the following command:
node app.js
How to do it…
Consider the...