phantomjs介绍

一. phantomjs定义html

    无界面的webkit引擎,它为咱们提供了一个命名行的浏览器接口,能够帮助咱们生成网页的截图以及抓取网页内容。jquery

二. phantomjs 下载及安装git

  1. 下载地址:http://phantomjs.org/download.html
  2. 直接解压安装而且配置path环境变量,执行以下命令查看配置是否正确。
     #phantomjs --version
       1.9.2

 

三.简单demogithub

   经过Phantomjs运行简单的hello world demo以下web

  1. 建立简单的hello.js文件,其内容以下:
    console.log('Hello, world!');
    phantom.exit();
  2. 在命令行执行以下代码:
    #phantom hello.js
     hello world

     

 

   经过Phantomjs抓取网页的列子:浏览器

  1. 建立以下page.js文件,内容以下
    var page = require('webpage').create();
      page.open('http://www.baidu.com',function(){
       page.render('baidu.png');
       phantom.exit();
    });

     

  2. 执行以下命令:框架

    #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 PoltergeistTerminus
Mocha Chutzpahmocha-phantomjs
FuncUnit built-in
Hiro built-in
Karma (née Testacular) built-in
Jasmine Chutzpahgrunt-jasmine-runnerguard-jasminephantom-jasmine
JsTestDriver js-test-driver-phantomjs
Laika built-in
Robot Framework phantomrobot
QUnit built-inChutzpahJS Test RunnerQliveQUnited
tapedeck built-in
Testem built-in
WebDriver GhostDriver
wru built-in
YUITest Groverphantomjs-yuitest

 

以上参考http://phantomjs.org

相关文章
相关标签/搜索