一、使用标准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. 一篇关于spring ioc 依赖注入3种方式的文章引用

    今天看到一篇spring ioc 容器依赖注入3种方式的文章,为了方便后面的复习,在此引用别人的文章,查看请戳我.

  2. jsp数据库开发

    完全卸载mysql数据库图文教程 https://jingyan.baidu.com/article/f96699bbaa8fc1894f3c1b5a.html MySQl:123456 JDBC概述 ...

  3. GIT情况展示说明

    旧仓库:https://git.coding.net/shenbaishan/GIFT.git 公开的 新仓库:https://git.coding.net/shenbaishan/gift-sele ...

  4. bash基本功能 -命令的别名和快捷键

    命令的别名 == 人的小名 如何查看和设定别名 alias 查看系统中的所有别名 ls --color=auto alias ll = 'ls - l --color=auto' touch abc ...

  5. Android控件第2类——ImageView

    1.ImageView不仅仅可以显示图片,ImageView可以显示任何Drawable对象. adjustViewBounds:设置ImageView是否调整自己的边界来保证图片的长宽比. crop ...

  6. Python进阶-配置文件

    一. 什么是配置文件?为什么要做配置文件? 将所有的代码和配置都变成模块化可配置化,这样就提高了代码的重用性,不再每次都去修改代码内部,这个就是我们逐步要做的事情,可配置化 二. 配置文件长啥样? 配 ...

  7. 1643【例 3】Fibonacci 前 n 项和

    1643:[例 3]Fibonacci 前 n 项和 时间限制: 1000 ms         内存限制: 524288 KB sol:这题应该挺水的吧,就像个板子一样 1 0 01 1 0   * ...

  8. 第一天进入博客这个神奇的领域 在此%%%erosun

    第一条博客水一水    等会要找一下调博客模板 独立动手,丰衣足食

  9. 洛谷 P2245 星际导航 解题报告

    P2245 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向 ...

  10. Hadoop生态圈-Kafka配置文件详解

    Hadoop生态圈-Kafka配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.默认kafka配置文件内容([yinzhengjie@s101 ~]$ more /s ...