CasperJS API介绍(2)-- CasperJS最基本API介绍

一、使用标准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
});

2 在运行时动态添加

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

二、最基本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();
    • 运行结果:
      这里写图片描述

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.
      });
    • 运行结果:
      这里写图片描述

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();
    • 运行结果:
      这里写图片描述

三、综合实例

获取当前访问的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();
  2. 结果:
    这里写图片描述这里写图片描述这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值