The Casper class


The easiest way to get a casper instance is to use the module’s create() method:

最简单获取casper实例的方法是casper模块的create()方法:

var casper = require('casper').create();

But you can also retrieve the main Function and instantiate it by yourself:

你也能够从主函数中获取实例,实例化

var casper = new require('casper').Casper();

Both the Casper constructor and the create() function accept a single options argument which is a standard javascript object:

casper构造函数和create函数都可以传入一个基础的JavaScript对象类型的设置:

var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});

Casper.options

An options object can be passed to the Casper constructor, eg.:

一组设置对象能被传入到casper构造函数中,例如:

var casper = require('casper').create({
clientScripts: [
'includes/jquery.js', // These two scripts will be injected in remote DOM on every request
'includes/underscore.js' // 这两个脚本将被将在每一次请求时,注入到远程DOM
],
pageSettings: { // The WebPage instance used by Casper will use these settings
loadImages: false, // 使用casper创建的网页原型将会使用这些设置
},
logLevel: "info", // Only "info" level messages will be logged 只有"info"等级时的信息才会被记录
verbose: true // log messages will be printed out to the console 日志消息将会被输出到控制台
});

You can also alter options at runtime:

你也可以在运行时改变设置:

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

The whole list of available options is detailed below.

全部可选参数如下详述:

clientScripts

类型:Array

默认:[]

A collection of script filepaths to include in every page loaded.

在每一个页面载入时引入的脚本路径的集合

exitOnError

类型: Boolean

默认: true

Sets if CasperJS must exit when an uncaught error has been thrown by the script.

设置是否casperjs当遇到已经被抛出的未捕捉的错误时必须退出

httpStatusHandlers

类型: Object

默认: {}

A javascript Object containing functions to call when a requested resource has a given HTTP status code. A dedicated sample is provided as an example.

当被请求资源时返回一个http状态码时,一个JavaScript对象包含回调函数.一个专门的事例给出作为一个例子.

logLevel

类型: String

默认:error

Logging level (see the logging section for more information)

日志等级(看日志章节获取更多信息)

onAlert

类型: Function

默认: null

具体样式: onAlert(Object Casper, String message)

A function to be called when a javascript alert() is triggered

当javascript的alert函数被触发时调用

onDie

类型: Function

默认: null

具体样式: onDie(Object Casper, String message, String status)

A function to be called when Casper#die() is called

当调用casper中的die时触发回调

onError

类型: Function

默认: null

具体样式: onError(Object Casper, String msg, Array backtrace)

A function to be called when an “error” level event occurs

当“error”等级的事件发生时触发回调函数

onLoadError

类型: Function

默认: null

具体样式: onLoadError(Object Casper, String casper.requestUrl, String status)

A function to be called when a requested resource cannot be loaded

当请求的资源不能被载入时触发

onPageInitialized

类型: Function

默认: null

具体样式: onPageInitialized(Object page)

A function to be called after WebPage instance has been initialized

页面初始化时触发

onResourceReceived

类型: Function

默认: null

具体样式: onResourceReceived(Object Casper, Object resource)

Proxy method for PhantomJS’ WebPage#onResourceReceived() callback, but the current Casper instance is passed as first argument.

PhantomJs的webpage的onResourceReceived方法的回调的替代方法,但是当前casper实例被当做第一个参数传入

onResourceRequested

类型: Function

默认: null

具体样式: onResourceRequested(Object Casper, Object resource)

Proxy method for PhantomJS’ WebPage#onResourceRequested() callback, but the current Casper instance is passed as first argument.

PhantomJs的webpage的onResourceRequested方法的回调的替代方法,但是当前casper实例被当做第一个参数传入

onStepComplete

类型: Function

默认: null

具体样式: onStepComplete(Object Casper, stepResult)

A function to be executed when a step function execution is finished.

当一个步骤函数执行完成触发

onStepTimeout

类型: Function

默认: Function

具体样式: onStepTimeout(Integer timeout, Integer stepNum)

A function to be executed when a step function execution time exceeds the value of the stepTimeout option, if any has been set.

By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results.

当一个步骤函数时间超过stepTimeout选项的值时触发,如果被设置了 .默认情况下,当超时时脚本将会退出展示错误,除了在测试环境下,将会添加一个失败的结果

onTimeout

类型: Function

默认: Function

具体样式: onTimeout(Integer timeout)

A function to be executed when script execution time exceeds the value of the timeout option, if any has been set.

By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results.

当脚本执行时间超过设置的超时timeout时触发,如果被设置了.默认情况下,当超市脚本将会退出展示一个错误,除了测试环境下,将会添加一个失败的结果

onWaitTimeout

类型: Function

默认: Function

具体样式: onWaitTimeout(Integer timeout)

A function to be executed when a waitFor function execution time exceeds the value of the waitTimeout option, if any has been set.

By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results.

当以waitFor开头的等待的函数的执行时间超过了设定的waitTimeout时触发,如果被设置了.默认情况下,当超时脚本将会退出展示一个错误,除了测试环境下,将会添加一个失败的结果

page

类型: WebPage

默认: null

An existing PhantomJS WebPage instance

一个现有的PhantomJS网页实例

Warning

Overriding the page properties can cause some of the casper features may not work. For example, overriding the onUrlChanged property will cause the waitForUrl feature not work.

警告

覆盖page属性将会引起casper特性不工作.比如重写了onUrlChanged属性将会导致waitForUrl特性不工作

pageSettings

类型: Object

默认: {}

PhantomJS’s WebPage settings object. Available settings are:

PhantomJS的网页设置对象,可用的设置如下:

  • javascriptEnabled defines whether to execute the script in the page or not (default to true)

    定义javascript脚本是否可以执行,默认true
  • loadImages defines whether to load the inlined images or not

    定义是否载入内联图片
  • loadPlugins defines whether to load NPAPI plugins (Flash, Silverlight, …) or not

    定义是否载入NPAPI插件
  • localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (default to false)

    定义本地资源是否有权限访问远程url,默认false
  • userAgent defines the user agent sent to server when the web page requests resources

    定义UA
  • userName sets the user name used for HTTP authentication

    设置用户名
  • password sets the password used for HTTP authentication

    设置密码
  • XSSAuditingEnabled defines whether load requests should be monitored for cross-site scripting attempts (default to false)

    定义是否允许跨域请求,默认false

remoteScripts

New in version 1.0.

类型: Array

默认: []

A collection of remote script urls to include in every page loaded

远程脚本url的集合,在每一次页面载入时引入

safeLogs

New in version 1.0.

类型: Boolean

默认: true

When this option is set to true — which is the default, any password information entered in will be obfuscated in log messages. Set safeLogs to false to disclose passwords in plain text (not recommended).

当这个选项被设置成true-也是默认值,被填入任何密码信息,将会在日志信息中混淆.设置safeLogs为false,将会在文本中透露密码(不推荐)

silentErrors

类型: Boolean

默认: false

When this option is enabled, caught step errors are not thrown (though related events are still emitted). Mostly used internally in a testing context.

当这个选项可用时,捕捉的每一个步骤错误将不抛出(尽管相关事件仍被执行).大部分用于内部测试.

stepTimeout

类型: Number

Default: null

Max step timeout in milliseconds; when set, every defined step function will have to execute before this timeout value has been reached. You can define the onStepTimeout() callback to catch such a case. By default, the script will die() with an error message.

最大步骤函数超时毫秒数.当设置了,任何一次定义的步骤函数将不得不在超时前到达时执行.你可以定义onStepTimeout回调函数去捕捉每一种情况.脚本将伴随着错误信息停止掉.

timeout

类型: Number

默认: null

Max timeout in milliseconds

最大超时毫秒数

verbose

类型: Boolean

默认: false

Realtime output of log messages

实时输出日志文件

viewportSize

类型: Object

默认: null

Viewport size, eg. {width: 800, height: 600}

视窗大小,比如. {width: 800, height: 600}

Note:PhantomJS ships with a default viewport of 400x300, and CasperJS won’t override it by default.

笔记:PhantomJS默认采用400x300的视窗,CasperJS不会默认重写

retryTimeout

类型: Number

默认: 100

Default delay between attempts, for wait family functions.

默认尝试等待时间,为wait类函数

waitTimeout

类型: Number

默认: 5000

Default wait timeout, for wait* family functions.

默认等待时间,为wait类函数

casperjs-options的更多相关文章

  1. jquery photoClip支持手机端,PC端 本地裁剪图片后上传插件

    支持手机,PC最好的是jquery photoClip插件,下载地址&示例:https://github.com/topoadmin/photoClip demo.html 代码: <! ...

  2. CasperJS基于PhantomJS抓取页面

    CasperJS基于PhantomJS抓取页面 Casperjs是基于Phantomjs的,而Phantom JS是一个服务器端的 JavaScript API 的 WebKit. CasperJS是 ...

  3. CasperJS API介绍

    一.使用标准JavaScript对象作为可选参数构造CasperJS实例 1 直接在create()函数里面使用 var casper = require('casper').create({ cli ...

  4. 前端端对端测试:基于PhantomJS的CasperJS

    简介 Casperjs是一个基于PhantomJS和SlimerJS的前端端对端测试框架,当然你也可以使用它完成网络爬虫功能,它的特点的通过简单的脚本模拟浏览器行为, 主要有casper.tester ...

  5. [译文]casperjs的API-clientutils模块

    casper提供了少量的客户端接口用来进行远程DOM环境注入,通过clientutils模块的ClientUtils类实例中的__utils__对象来执行: casper.evaluate(funct ...

  6. [译文]casperjs 的API-casper模块

    Casper class: 可以通过这个模块的create()方法来获取这个模块的一个实例,这是最容易的: var casper = require('casper').create(); 我们也可以 ...

  7. [译文]casperjs使用说明-测试

    capserjs自带了一个测试框架,它提供了一个使你能够更容易的测试你的web应用的工具集. 注意: 1.1版本变更 这个测试框架,包括它的所有API,仅能使用在casperjs test子命令下 如 ...

  8. [译文]casperjs使用说明-使用命令行

    使用命令行 Casperjs使用内置的phantomjs命令行解析器,在cli模块里,它传递参数位置的命名选项 但是不要担心不能熟练操控CLI模块的API,一个casper实例已经包含了cli属性,允 ...

  9. casperjs进行web功能自动化测试demo

    通过一周多的学习和总结,终于掌握了casperjs用于自动化的方法,填平了大大小小的各种坑. casperjs是一个新兴的测试框架,网上资料很少,基本上靠翻译英文资料. 贡献出来,供大家参考:   / ...

  10. Phantomjs和Casperjs,后台网页抓取和交互

    var casper = require('casper').create({ verbose: true, logLevel: 'debug', pageSettings: { loadImages ...

随机推荐

  1. (解决某些疑难杂症)Ubuntu16.04 + NVIDIA显卡驱动 + cuda10 + cudnn 安装教程

    一.NVIDIA显卡驱动 打开终端,输入: sudo nautilus 在新打开的文件夹中,进入以下路径(不要用命令行): 左下角点计算机,lib,modules 这时会有几个文件夹,对每个文件夹都进 ...

  2. DNS域名系统,简述工作原理

    DNS工作原理: 当DNS客户端需要在程序中使用名称时,它会查询DNS服务器来解析该名称.客户端发送的每条查询信息包括三条信息:指定的DNS域名,指定的查询类型,DNS域名的指定类别.基于UDP服务, ...

  3. jsp中引入js文件缓存问题解决

    加上版本UUID <script charset="utf-8" src="${basePath}js/souke/soukegalist.js?v=<%=U ...

  4. 浅析HTTP代理原理

    代理服务器是HTTP协议中一个重要的组件,发挥着重要的作用. 关于HTTP代理的文章有很多,本文不再赘述,如果不清楚的可以看一下 HTTP代理的基础知识. 本文主要介绍代理的事例,分析一个真实的案例来 ...

  5. Python中类

    1.类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self,self代表类的实例,而非类. self 不是 python 关键字,我们把他换成 r ...

  6. Autoit3 自动添加打印机

    从网上找的代码进行了修改!! 其原理1\用注册表添加端口,2\重启打印服务 ,3最后使用"rundll32 printui.dll"命令进行添加打印机 如下: #RequireAd ...

  7. Factory Methods

    The static factory method pattern is a way to encapsulate object creation. Without a factory method, ...

  8. maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明

    POM中配置的如下: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

  9. Dataphin帮助企业构建数据中台系列之--萃取数据中心

    Dataphin作为阿里巴巴数据中台OneData (OneModel.OneID.OneService)方法论的产品载体,帮助企业构建三大数据中心:基于数据集成形成的垂直数据中心.基于数据开发沉淀的 ...

  10. ldd3 第12章 PCI驱动程序

    PCI接口 PCI寻址 引导阶段 配置寄存器和初始化 MODULE_DEVICE_TABLE 注册PCI驱动程序 佬式PCI探测 激活PCI设备 访问配置空间 访问I/O和内存空间 PCI中断 硬件抽 ...