The Casper class The easiest way to get a casper instance is to use the module’s create() method: 最簡單獲取casper實例的方法是casper模塊的create()方法: But you can al ...
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類函數