Phantomjs Tutorial

,
Want to master JavaScript in a week? Example based, to the point. Xah JavaScript Tutorial.

(this is my learning notes)

http://phantomjs.org/

console.log("hi there");
phantom.exit();

save the above code in a file, and run it like this: phantomjs filename.

// create a screenshot of a webpage
var page = require('webpage').create();
page.open('http://example.com', function() {
  page.render('example.png');
  phantom.exit();
});

the following script tests a web page load speed.

// test a webpage loading speed
var page = require('webpage').create();
var system = require('system');
var timeStamp;
var myURL;

if (system.args.length === 1) {
  console.log('Usage: loadspeed.js <some URL>');
  phantom.exit();
}

timeStamp = Date.now();
myURL = system.args[1];
page.open( myURL, function(status) {
  if (status !== 'success') {
    console.log('FAIL to load page');
  } else {
    timeStamp = Date.now() - timeStamp;
    console.log('Loading time ' + timeStamp + ' milisecond');
  }
  phantom.exit();
});

run it like this phantomjs loadspeed.js http://www.google.com

http://phantomjs.org/

blog comments powered by Disqus