一、使用标准JavaScript对象作为可选参数构造CasperJS实例

1 直接在create()函数里面使用

var casper = require('casper').create({
clientScripts: [
'includes/jquery.js', // These two scripts will be injected in remote
'includes/underscore.js' // DOM on every request
],
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false // use these settings
},
logLevel: "info", // Only "info" level messages will be logged
verbose: true // log messages will be printed out to the console
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2 在运行时动态添加

var casper = require('casper').create();
casper.options.waitTimeout = 1000;
  • 1
  • 2

二、最基本API介绍

1. start

  • 原 型: start(String url[, Function then])
  • 说 明:配置并启动CasperJS,然后打开url,最后进行then后面的步骤。
  • 参数: 
    • url: 需要打开的网址。
    • then: 需要执行的后续操作。
  • 实例:

    • 代码:

      var casper = require('casper').create();
      casper.start('http://www.baidu.com/', function() {
      this.echo("Hello Baidu. I am here now.");
      });
      casper.run();
      • 1
      • 2
      • 3
      • 4
      • 5
    • 运行结果: 

2. run

  • 原 型:run(fn onComplete[, int time])
  • 说 明:执行所有的步骤,当所有的步骤都执行完之后可以执行一个callback。
  • 参数: 
    • onComplete: 可选参数,当所有步骤执行完成之后的回调函数。注意:如果使用回调函数,一定要在回调函数里面调用exit(),便于返回。同时,由于调用exit()之后就从回调函数返回,所以在exit()之后的操作不会有任何作用。
  • 实例:

    • 代码:

      var casper = require('casper').create();
      casper.start('http://www.baidu.com/', function() {
      this.echo("Hello Baidu. I am here now.");
      });
      casper.run(function() {
      this.echo('So the whole suite ended.');
      this.exit(); // <--- don't forget me!
      this.echo('After exit().'); //<----Don't be executed.
      });
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • 运行结果: 

3 then

  • 原 型:then(Function then)
  • 说 明:通过提供一个简单的函数,使用标准的方式来增加一个导航功能到执行栈。
  • 参数: 
    • Function: 简单的函数。
  • 实例:

    • 代码:

      var casper = require('casper').create();
      casper.start('http://www.baidu.com/');
      casper.then(function() {
      this.echo("I'm in Baidu.");
      }); casper.then(function() {
      this.echo('I am a freshman.');
      }); casper.then(function() {
      this.echo('It is amazing. Bye!');
      });
      casper.run();
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 运行结果: 

三、综合实例

获取当前访问的HTTP response

    1. 代码:

      var casper = require('casper').create();
      casper.start('http://www.baidu.com/');
      casper.then(function() {
      this.echo("HTTP Response Info:");
      this.echo("--------------------------------------");
      }); casper.then(function(response) {
      require('utils').dump(response);
      }); casper.then(function() {
      this.echo("--------------------------------------");
      this.echo("end");
      });
      casper.run();
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    2. 结果: 

转载:http://blog.csdn.net/Kandy_Ye/article/details/47355169

CasperJS API介绍的更多相关文章

  1. 常用ArcGIS for Silverlight 开发API介绍

    1.API介绍 2.Map对象  3.Layer对象 4.Symbol对象 5.Task对象

  2. Servlet基础(一) Servlet简介 关键API介绍及结合源码讲解

    Servlet基础(一) Servlet基础和关键的API介绍 Servlet简介 Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servl ...

  3. python学习笔记(win32print API介绍)

    最近博主在研究用python控制打印机 这里整理下win32print的API介绍,官网地址http://timgolden.me.uk/pywin32-docs/win32print.html Op ...

  4. 使用html5中video自定义播放器必备知识点总结以及JS全屏API介绍

    一.video的js知识点: controls(控制器).autoplay(自动播放).loop(循环)==video默认的: 自定义播放器中一些JS中提供的方法和属性的记录: 1.play()控制视 ...

  5. Commons-lang API介绍

    4.1 Commons-lang API介绍 4.1.1 StringUtils 4.1.2 StringEscapeUtils 4.1.3 ArrayUtils 4.1.4 DateUtils 4. ...

  6. APP自动化框架LazyAndroid使用手册(3)--核心API介绍

    作者:黄书力 概述 在前一篇博文中,简要介绍了一款安卓UI自动化测试框架LazyAndroid (http://blog.csdn.net/kaka1121/article/details/53204 ...

  7. Spring Boot 2.x 编写 RESTful API (一) RESTful API 介绍 & RestController

    用Spring Boot编写RESTful API 学习笔记 RESTful API 介绍 REST 是 Representational State Transfer 的缩写 所有的东西都是资源,所 ...

  8. FastDFS api介绍

    1. 命令行api介绍 FastDFS提供了可用于运维测试的命令行api,下面进行介绍: 1.1 fastdfs服务管理 tracker进程服务管理脚本 /etc/init.d/fdfs_tracke ...

  9. ElasticSearch的API介绍

    ElasticSearch的API介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ES是基于Restful风格 1>ES是基于Restful风格 Elasticsea ...

随机推荐

  1. Spherical Hashing,球哈希

    1. Introduction 在传统的LSH.SSH.PCA-ITQ等哈希算法中,本质都是利用超平面对数据点进行划分,但是在D维空间中,至少需要D+1个超平面才能形成一个封闭.紧凑的区域.而球哈希方 ...

  2. week4

    History:Commercialization and Growth course Explosive Growth of the Internet and Web The Year of the ...

  3. Jira 添加自定义字段

    打开添加自定义字段,并选择字段类型 填写名称,并创建 3.选择关联的界面,并更新

  4. idea不能跟随输入法问题

    在写注释的时候会发现输入法不跟随,这是idea工具本身存在的bug,这个问题很头疼,我找了好多办法都不行,比如删除idea自带的jre,这个办法对我的2018.1.5版本并不适用,以下办法是不需要删除 ...

  5. [转帖] sparkdev 的 博客 systemd

    从 init 系统说起 https://www.cnblogs.com/sparkdev/p/8448237.html systemd的内容 需要学习下. linux 操作系统的启动首先从 BIOS ...

  6. layout图形化界面看不到内容 Failed to find the style corresponding to the id

    1.问题 在创建新的工程的时候,选择目标SDK为api21,编译SDK为api23.创建出来的layout文件图形化界面中看不到,并且报错: Failed to find the style corr ...

  7. 阿里云视频直播PHP-SDK接入教程

    阿里云视频直播PHP-SDK接入教程 阿里云 视频直播 配置 及 PHP-SDK 接入教程        准备工作        域名管理        配置鉴权        地址生成器及DEMO演 ...

  8. Latex使用:在latex中添加算法模块

    在Miktex下有三个latex algorithm包,分别为:algorithm,algorithmic,algorithm2e三个,其中algorithm,algorithmic经常成套使用: l ...

  9. Django时间时区问题(received a naive datetime while time zone support is active)

    在django1.4以后,存在两个概念 naive time 与 active time. 简单点讲,naive time就是不带时区的时间,Active time就是带时区的时间. 举例来说,使用d ...

  10. sniffer 和 debug flow

    sniffer 和 debug flow sniffer 和 debug flow 复制模板,直接修改IP即可使用: diagnose sys session filter clear diagnos ...