CasperJS實踐筆記(4)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

casperJS的測試module

 

casper.test.begin執行一串完整的測試動作,不過其中的對象this並非capser而是test!

只不過test對象本身具備casper的attribute,另外還有test module提供的attribute(提供了測試相關的諸多API)。

 

casper對象有很多很多options,下面說說這些options的設定,options允許我們設定一些預設值、userAgent、定製功能、等等。

舉例如下:

casper.options.pageSettings = {        loadImages: true,        loadPlugins: true,            userAgent: ‘Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30‘    };casper.options.verbose = true;casper.options.logLevel = "debug";casper.on("page.error", function(_err, _trace){    this.log(_err, "error");});casper.on("remote.message", function(_msg){    this.log(_msg, "warning");});

 

比如logLevel用來設定log等級,有4個值:debug,info,warning,error;

比如使用on監聽事件"page.error",可用抓取js指令碼錯誤;

關於Http請求的響應也有辦法處理,而且還有兩個辦法,代碼如下:

casper.on("http.status.400", function(_resource){    this.log("Remote http.status.400 occur.", "error");    this.log(_resource.url, "error");});casper.options.httpStatusHandlers = {    400:    function(self, _resource){                this.log(casper._Impor_msg_str1, "error");                this.log("Remote http.status.400 occur.", "error");                this.log(_resource.url, "error");                this.log(casper._Impor_msg_str2, "error");            }};

 

代碼執行過程中,肯定需要output一下資訊,才好嘛;有三種方式:casper.echo,casper.log,test.info

其中test.info帶一個參數,就是“想說的話”;

而casper.echo可以帶2個參數:“想說的話”和“重要層級”(debug,info,warning,error);

casper.log則可以帶3個參數:“想說的話”、“重要層級”、“來源”(phantom or casper);

 

好了,感覺自己很慷慨~~~

 

CasperJS實踐筆記(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.