Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Recording debugger messages


This recipe introduces the onConsoleMessage callback, discusses how to intercept debugger messages on the web page, and forwards them to the command-line output. The onConsoleMessage demonstration serves as an introduction to the other callbacks (for example, onAlert, onLoadFinished, onResourceReceived, and so on) that can be attached to webpage instances, while also providing a frame of reference for working with them.

Getting ready

To run this recipe, we will need a script that loads a web page, and then we will need JavaScript that executes in the context of that web page and writes messages to its console object.

The script in this recipe is available in the downloadable code repository as recipe06.js under chapter03. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

var webpage = require('webpage').create();

webpage.onConsoleMessage = function(message, lineNum, sourceId...