一. phantomjs定义html
无界面的webkit引擎,它为咱们提供了一个命名行的浏览器接口,能够帮助咱们生成网页的截图以及抓取网页内容。jquery
二. phantomjs 下载及安装git
#phantomjs --version 1.9.2
三.简单demogithub
经过Phantomjs运行简单的hello world demo以下web
console.log('Hello, world!'); phantom.exit();
#phantom hello.js
hello world
经过Phantomjs抓取网页的列子:浏览器
var page = require('webpage').create(); page.open('http://www.baidu.com',function(){ page.render('baidu.png'); phantom.exit(); });
执行以下命令:框架
#phantom page.js
其生成的图片结果以下: grunt
例子3:loadspeed.js测试
var page = require("webpage").create(), system = require('system'), t, address; if(system.args.length===1) { console.log('usage: loadinspeed.js <some URL>'); phantom.exit(); } t = Date.now(); address = system.args[1]; console.log(address); page.open(address, function(status){ console.log(status); if(status !== 'success'){ console.log('faild to load the address'); }else { t = Date.now() - t; console.log('loading time ' + t +' msec'); } phantom.exit(); });
执行结果以下ui
D:\demo>phantomjs loadspeed.js http://www.baidu.com http://www.baidu.com success loading time 513 msec
经过evaluate()获取页面要素:
var page = require('webpage').create(); page.open("http://phantomjs.org/quick-start.html", function(status) { var title = page.evaluate(function() { return document.title; }); console.log('Page title is ' + title); });
运行结果
# hantomjs element.js
the page title is Qucik Start | PhantomJS
四. phantomjs与其余测试框剪的整合
phantomjs自己不是一个测试框架,所以,须要借助其余的测试框剪来进行测试运行:常见的js测试框架已进行运行其列表以下:
Framework | Test Runner |
---|---|
Buster.JS | built-in |
Capybara | Poltergeist, Terminus |
Mocha | Chutzpah, mocha-phantomjs |
FuncUnit | built-in |
Hiro | built-in |
Karma (née Testacular) | built-in |
Jasmine | Chutzpah, grunt-jasmine-runner, guard-jasmine, phantom-jasmine |
JsTestDriver | js-test-driver-phantomjs |
Laika | built-in |
Robot Framework | phantomrobot |
QUnit | built-in, Chutzpah, JS Test Runner, Qlive, QUnited |
tapedeck | built-in |
Testem | built-in |
WebDriver | GhostDriver |
wru | built-in |
YUITest | Grover, phantomjs-yuitest |