Example Scripts for PhantomJS
PhantomJS comes with a lot of included examples.
Basic examples
- arguments.js shows the arguments passed to the script
- countdown.js prints a 10 second countdown
- echoToFile.js writes the command line arguments to a file
- fibo.js lists the first few numbers in the Fibonacci sequence
- hello.js displays the famous message
- module.js and
universe.jsdemonstrate the use of module system - outputEncoding.js displays a string in various encodings
- printenv.js displays the system’s environment variables
- scandir.js lists all files in a directory and its subdirectories
- sleepsort.js sorts integers and delays display depending on their values
- version.js prints out PhantomJS version number
- page_events.js prints out page events firing: useful to better grasp
page.on*callbacks
Rendering/rasterization
- colorwheel.js creates a color wheel using HTML5 canvas
- rasterize.js rasterizes a web page to image or PDF
- render_multi_url.js renders multiple web pages to images
Page automation
- injectme.js injects itself into a web page context
- phantomwebintro.js uses jQuery to read .version element text from phantomjs.org
- unrandomize.js modifies a global object at page initialization
- waitfor.js waits until a test condition is true or a timeout occurs
Network
- detectsniff.js detects if a web page sniffs the user agent
- loadspeed.js computes the loading speed of a web site
- netlog.js dumps all network requests and responses
- netsniff.js captures network traffic in HAR format
- post.js sends an HTTP POST request to a test server
- postserver.js starts a web server and sends an HTTP POST request to it
- server.js starts a web server and sends an HTTP GET request to it
- serverkeepalive.js starts a web server which answers in plain text
- simpleserver.js starts a web server which answers in HTML
Testing
- run-jasmine.js runs Jasmine based tests
- run-qunit.js runs QUnit based tests
Browser
- features.js detects browser features using
modernizr.js - useragent.js changes the browser’s user agent property