Casperjs API Introduction

Source: Internet
Author: User

First, use standard JavaScript objects as optional parameters to construct Casperjs instance 1 directly inside the Create () function
var casper = require ( ' Casper '). Create ({clientscripts: [ ' includes/jquery.js ', //These TW o scripts would be injected in remote  ' includes/underscore.js ' // DOM on every request], PageSettings: {loadimages: false, //the W Ebpage instance used by Casper would loadplugins: false //use These settings}, LogLevel:  "info", //only "info" level messages Would be logged verbose: true //log messages would be printed off to the console});               
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
2 dynamically added at run time
var casper = require(‘casper‘).create();casper.options.waitTimeout = 1000;
    • 1
    • 2
Second, the most basic API introduction 1. Start
    • Prototype: Start (String url[, Function then])
    • Description: Configure and start Casperjs, then open the URL, and then follow the steps later.
    • Parameters:
      • URL: The URL that needs to be opened.
      • Then: Subsequent actions that need to be performed.
    • Instance:

      • Code:

        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
      • Operation Result:
2. Run
  • Prototype: Run (FN oncomplete[, int time])
  • Description: Perform all the steps, and you can perform a callback after all the steps have been executed.
  • Parameters:
    • OnComplete: Optional parameter, when all steps are completed after execution of the callback function. Note: If you use a callback function, be sure to call exit () inside the callback function for easy return. At the same time, the operation after exit () does not have any effect because it is returned from the callback function after calling exit ().
  • Instance:

    • Code:

      var casper = require ( ' Casper '). Create (); Casper.start ( ' http://www.baidu.com/', function () {this.echo ( function () {this.echo (this.exit (); //<---don ' t forget me! this.echo ( ' after exit () '); //<----Don ' t be executed.});          
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
    • Operation Result:

3 Then
  • Prototype: Then (Function then)
  • Description: By providing a simple function, use the standard way to add a navigation function to the execution stack.
  • Parameters:
    • Function: Simple functions.
  • Instance:

    • Code:

      var casper = require ( ' Casper '). Create (); Casper.start ( 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
    • Operation Result:

Iii. Comprehensive Examples

Gets the HTTP response of the current access

    1. Code:

      var casper =Require' Casper '). Create (); Casper.start (function () {this.echo ( "HTTP Response Info:"); this.echo ( "--------------------------------------");}); Casper.then (function (response {require ( ' utils '). Dump (response);}); Casper.then (function () {this.echo ( "--------------------------------------"); this.echo (               
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
    2. Results:

Reprint: http://blog.csdn.net/Kandy_Ye/article/details/47355169

Casperjs API Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.