To filter out invalid requests, you can use the onResourceRequested callback : this allows you to cancel unwanted URLs.
Here is a basic example for stackoverflow.
var system = require('system'); var page = require('webpage').create(); var domain = 'stackoverflow.com' var url = 'http://www.stackoverflow.com'; page.onResourceRequested = function (requestData, networkRequest) { if (requestData.url.indexOf('.js')===-1 && requestData.url.indexOf(domain) === -1) { networkRequest.abort(); console.log('aborted :'+ requestData.url) } }; page.onResourceReceived = function (response) { console.log('Response (#' + response.url + ', stage "' + response.stage + '"): '); }; if (system.args.length !== 1) { console.log("Usage: phantomjs filter.js url"); } else { page.open(url, function (status) { if (status = 'succeed') { console.log("status", status); phantom.exit(0); } }); }
Please note that it is not recommended to interrupt js files, as this may cause a javascript error on your page.
Another way to speed up your test is to disable images using the argument --load-images=false
source share