PHANTOMJS is a WebKit-based server-side JavaScript API. It fully supports the web without the need for browser support, its fast, native support for a variety of Web standards: DOM processing, CSS selectors, JSON, Canvas, and SVG. PHANTOMJS can be used for page automation, network monitoring, web screen screenshots, and no interface testing.
PHANTOMJS Official website: http://phantomjs.org/
Github:https://github.com/ariya/phantomjs/wiki/quick-start
First, installation
installation package Download Address: http://phantomjs.org/download.html, including Windows, Mac os,linux version, select the corresponding version of the download decompression can be (for the convenience of use, can be set for the PHANTOMJS environment variables), It has a example folder with a lot of written code to use. This article assumes that the PHANTOMJS has been installed and that environment variables have been set.
Second, the use
Hello, world!.
Create a new text file that contains the following two lines of script:
Console.log (' Hello, world! ');
Phantom.exit ();
Save the file as a hello.js, and then execute it:
Phantomjs Hello.js
The output is: Hello, world!
The first line will print out the string at the terminal, and the second row Phantom.exit will quit running.
It is important to call Phantom.exit in this script, otherwise PHANTOMJS will not stop at all.
Script Parameters –script Arguments
How does Phantomjs pass parameters? As shown below:
Copy Code code as follows:
Phantomjs examples/arguments.js Foo Bar baz
Where the Foo, bar, Baz is the parameter to pass, how to get it:
var system = require (' system ');
if (system.args.length = = 1) {
console.log (' Try to pass some args when invoking this script! ');
} else {
Syst Em.args.forEach (function (arg, i) {
console.log (i + ': ' + arg);
}}
Phantom.exit ();
It will output:
0:foo
1:bar
2:baz
Page load –page Loading
By creating a Web object, a Web page can be loaded, parsed, and rendered.
The following script uses the simplest use of the sample Page object, which loads example.com and saves it as a picture, example.png.
var page = require (' webpage '). Create ();
Page.open (' http://example.com ', function () {
page.render (' example.png ');
Phantom.exit ();
});
Because of this feature, PHANTOMJS can be used to screen screenshots, to intercept some of the content of the snapshot, such as the Web page, SVG saved as a picture, PDF, etc., this function is very cow x.
The next Loadspeed.js script loads a special URL (don't forget the HTTP protocol) and measures the time it took to load the page.
var page = require (' webpage '). Create (),
system = require (' System '),
t, address;
if (system.args.length = = 1) {
console.log (' Usage:loadspeed.js <some url> ');
Phantom.exit ();
}
t = Date.now ();
address = system.args[1];
Page.open (address, function (status) {
if (status!== ' success ') {
console.log ("FAIL to Load");
} else {
t = Date.now ()-T;
Console.log (' Loading time ' + t + ' msec ');
}
Phantom.exit ();
});
Run the script at the command line:
Phantomjs Loadspeed.js http://www.google.com
It outputs something like the following:
Loading http://www.google.com Loading Time 719 msec
Code Operations –code Evaluation
To perform an operation on JavaScript or coffeescript in the context of a Web page, use the Evaluate () method. The code runs in the sandbox, and it has no way to read any JavaScript objects and variables that are outside the context of the page to which it belongs. Evaluate () returns an object, but it is limited to simple objects and cannot contain methods or closures.
Here's an example to display the page title:
var page = require (' webpage '). Create ();
Page.open (URL, function (status) {
var title = Page.evaluate (function () {return
document.title;
});
Console.log (' Page title is ' + title);
};
Any console information that comes from the Web page and includes internal code from evaluate () is not displayed by default. To override this behavior, using the Onconsolemessage callback function, the previous example can be rewritten as:
var page = require (' webpage '). Create ();
Page.onconsolemessage = function (msg) {
console.log (' Page title is ' + msg);
Page.open (URL, function (status) {
page.evaluate (function () {
console.log (document.title);
});
DOM Operations –dom Manipulation
The standard DOM scripts and CSS selectors work well because the scripts seem to run on a Web browser. This makes PHANTOMJS fit to support a variety of page automation tasks.
The following useragent.js reads the Textcontent property of an element with an ID of myagent:
var page = require (' webpage '). Create ();
Console.log (' The Default User agent is ' + page.settings.userAgent);
page.settings.userAgent = ' specialagent ';
Page.open (' http://www.httpuseragent.org ', function (status) {
if (status!== ' success ') {
console.log (' Unable to access network ');
} else {
var ua = page.evaluate (function () {return
document.getElementById (' myagent '). textcontent;
});
Console.log (UA);
}
Phantom.exit ();
});
The example above also provides a way to customize the user agent.
Use jquery and other class libraries:
var page = require (' webpage '). Create ();
Page.open (' http://www.sample.com ', function () {
Page.includejs ("http://ajax.googleapis.com/ajax/libs/jquery/ 1.6.1/jquery.min.js ", function () {
page.evaluate (function () {
$ (" button "). Click ();
});
Phantom.exit ()
});
Network request and Response –network Requests and Responses
When a page requests a resource from a remote server, both the request and the response can be traced through the onresourcerequested and Onresourcereceived callback methods. Sample netlog.js:
var page = require (' webpage '). Create ();
page.onresourcerequested = function (request) {
Console.log (' request ' + json.stringify (request, undefined, 4)); c12/>};
page.onresourcereceived = function (response) {
console.log (' Receive ' + json.stringify (response, Undefined, 4)); c15/>};
Page.open (URL);
For more information on how to use this feature for Har output and for YSlow profiling, see the Network Monitoring page.