casperjs-options
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类函数
上一篇: 《Java开发手册》