📜 ⬆️ ⬇️

PhantomJS: Webkit in console

image

PhantomJS is all WebKit buns from a console running on JS and supporting various standards and technologies: DOM, CSS, JSON, Canvas and SVG.

Inside are a few usage examples.

Launch


Launching scripts with PhantomJS is very simple:
phantomjs script.js [arguments] 

')

Using


This is the simplest application for PhantomJS
 console.log('Hello, world!'); phantom.exit(); 


Take an example more complicated. This script will show page load time:
 if (phantom.state.length === 0) { if (phantom.args.length === 0) { console.log('Usage: loadspeed.js <some URL>'); phantom.exit(); } else { var address = phantom.args[0]; phantom.state = Date.now().toString(); console.log('Loading ' + address); phantom.open(address); } } else { var elapsed = Date.now() - new Date().setTime(phantom.state); if (phantom.loadStatus === 'success') { console.log('Page title is ' + document.title); console.log('Loading time ' + elapsed + ' msec'); } else { console.log('FAIL to load the address'); } phantom.exit(); } 

After launching it
 phantomjs loadspeed.js http://www.google.com 

we'll get
 Loading http://www.google.com Page title is Google Loading time 719 msec 


Unit Tests


This, in my opinion the most useful application.
An example is integration with QUnit.
 if (phantom.state.length === 0) { if (phantom.args.length === 0 || phantom.args.length > 2) { console.log('Usage: run-qunit.js URL'); phantom.exit(); } else { phantom.state = 'run-qunit'; phantom.open(phantom.args[0]); } } else { setInterval(function() { var el = document.getElementById('qunit-testresult'); if (phantom.state !== 'finish') { if (el && el.innerText.match('completed')) { phantom.state = 'finish'; console.log(el.innerText); try { failed = el.getElementsByClassName('failed')[0].innerHTML; } catch (e) { } phantom.exit((parseInt(failed, 10) > 0) ? 1 : 0); } } }, 100); } 

As results we will get
 Tests completed in 1486 milliseconds. 1267 tests of 1267 passed, 0 failed. 


Rendering


Also a useful thing. Someone can write another service to create screenshots of pages.
 if (phantom.state.length === 0) { if (phantom.args.length !== 2) { console.log('Usage: rasterize.js URL filename'); phantom.exit(); } else { var address = phantom.args[0]; phantom.state = 'rasterize'; phantom.viewportSize = { width: 600, height: 600 }; phantom.open(address); } } else { var output = phantom.args[1]; phantom.sleep(200); phantom.render(output); phantom.exit(); } 

Putting it on http://raphaeljs.com/polar-clock.html
 phantomjs rasterize.js http://raphaeljs.com/polar-clock.html clock.png 

Will get
image

Or, for example, you need to get pdf from the print version on wikipedia:
 phantomjs rasterize.js "http://en.wikipedia.org/w/index.php?title=Jakarta&printable=yes" jakarta.pdf 

what will give us
image
(doesn’t look very good due to the strong zoom out in FoxitPDF Reader)

You can download Windows binaries and source code with google code
Well, of course the project website: http://www.phantomjs.org

Source: https://habr.com/ru/post/116789/


All Articles