[译文]casperjs使用说明-测试
capserjs自带了一个测试框架,它提供了一个使你能够更容易的测试你的web应用的工具集。
注意:
1.1版本变更
这个测试框架,包括它的所有API,仅能使用在casperjs test子命令下
如果你在测试框架的范围以外使用casper.test的属性,会报error
从1.1-beta3开始,你能够在测试环境下改写casper的初始化配置,想知道更多,可以去dedicated FAQ entry.了解
单元测试
设想Cow为我们想要测试的对象:
function Cow() {
this.mowed = false;
this.moo = function moo() {
this.mowed = true; // mootable state: don't do that at home
return 'moo!';
};
}
我们来写一个测试套件:
// cow-test.js
casper.test.begin('Cow can moo', 2, function suite(test) {
var cow = new Cow();
test.assertEquals(cow.moo(), 'moo!');
test.assert(cow.mowed);
test.done();
});
用casperjs test 命令来运行我们的测试:
$ casperjs test cow-test.js
你可以看到如下执行结果:

使他发生错误:
casper.test.begin('Cow can moo', 2, function suite(test) {
var cow = new Cow();
test.assertEquals(cow.moo(), 'BAZINGA!');
test.assert(cow.mowed);
test.done();
});
你将得到如下结果:

浏览器测试
现在写一个测试google搜索的用例:
// googletesting.js
casper.test.begin('Google search retrieves 10 or more results', 5, function suite(test) {
casper.start("http://www.google.fr/", function() {
test.assertTitle("Google", "google homepage title is the one expected");
test.assertExists('form[action="/search"]', "main form is found");
this.fill('form[action="/search"]', {
q: "casperjs"
}, true);
}); casper.then(function() {
test.assertTitle("casperjs - Recherche Google", "google title is ok");
test.assertUrlMatch(/q=casperjs/, "search term has been submitted");
test.assertEval(function() {
return __utils__.findAll("h3.r").length >= 10;
}, "google search for \"casperjs\" retrieves 10 or more results");
}); casper.run(function() {
test.done();
});
});
现在运行这个用例:
$ casperjs test googletesting.js
你可以得到如下结果:

在测试环境设置casper选项:
你必须测试环境里已经预配置casper 选项,可以采用以下方式更新属性:
casper.options.optionName = optionValue; // where optionName is obviously the desired option name
casper.options.clientScripts.push("new-script.js");
高级技术:
Tester#begin()接受一个方法或者对象来描述一个套件,对象属性允许设置setUp() 和 tearDown()方法:
// cow-test.js
casper.test.begin('Cow can moo', 2, {
setUp: function(test) {
this.cow = new Cow();
}, tearDown: function(test) {
this.cow.destroy();
}, test: function(test) {
test.assertEquals(this.cow.moo(), 'moo!');
test.assert(this.cow.mowed);
test.done();
}
});
test的命令行参数和选项:
参数设置命令
casperjs test命令 将传递的自变量,作为文件或者目录路径处理,他将递归的扫描所有通过的参数,并寻找对应的’*.js’或者’*.coffee’并且增加他们到堆栈中。
注意:
当你在写测试用例时,有两个重要的注意事项:
在一个测试文件里,你不能创建多个Casper对象
只有在解析到Tester.done()时,测试用例才会真正执行
选项
选项使用“--”作为标识符
--xunit=<filename> 将测试结果记录到xunit格式的xml文件中
--direct or –verbose 打印一个log消息到控制台
--log-level=<logLevel> 设置展示在控制台的log消息的等级
--auto-exit=no 当所有的测试都执行完毕后阻止测试运行结束,他通常用来追加执行其他操作,通过手动设置exit可以触发exit事件:
// $ casperjs test --auto-exit=no
casper.test.on("exit", function() {
someTediousAsyncProcess(function() {
casper.exit();
});
});
1.0版本新增
--includes=foo.js,bar.js 在测试文件执行时引入foo.js,bar.js
--pre=pre-test.js 在所有的测试用例执行前增加pre-test.js
--post=post-test.js 在所有的测试用例执行后增加post-test.js
--fail-fast 当产生第一个失败后立即结束执行
--concise 创建一个更简洁的输出结果
--no-colors 输出时不带颜色
命令行实例:
$ casperjs test --includes=foo.js,bar.js \
--pre=pre-test.js \
--post=post-test.js \
--direct \
--log-level=debug \
--fail-fast \
test1.js test2.js /path/to/some/test/dir
注意:
自1.1版本废弃:
--direct选项已经重命名为--verbose,尽管--direct还能工作, 但是他的不被提倡的
输出xunit结果:
casperjs能够以xml格式输出可被xunit解析的结果,他能够被持续集成工具如jenkins兼容,要保存测试结果,使用--xunit选项
$ casperjs test googletesting.js --xunit=log.xml
你将得到一个向下面一样漂亮的结果:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites duration="1.249">
<testsuite errors="0" failures="0" name="Google search retrieves 10 or more results" package="googletesting" tests="5" time="1.249" timestamp="2012-12-30T21:27:26.320Z">
<testcase classname="googletesting" name="google homepage title is the one expected" time="0.813"/>
<testcase classname="googletesting" name="main form is found" time="0.002"/>
<testcase classname="googletesting" name="google title is ok" time="0.416"/>
<testcase classname="googletesting" name="search term has been submitted" time="0.017"/>
<testcase classname="googletesting" name="google search for "casperjs" retrieves 10 or more results" time="0.001"/>
<system-out/>
</testsuite>
</testsuites>
casperjs自测:
casperjs拥有自己的单元测试集,它放置在tests子目录下,可以这样运行它:
$ casperjs selftest
运行这个测试集的一个好处是发现你运行平台的BUG,如果它失败了请在github的 file an issue 或者在CasperJS mailing-list反馈
扩展casperjs测试框架:
这个命令:
$ casperjs test [path]
是这个命令的截短:
$ casperjs /path/to/casperjs/tests/run.js [path]
如果你想扩展casperjs测试框架的能力,你可以编写自己的runner方法和扩充casper对象
[译文]casperjs使用说明-测试的更多相关文章
- [译文]casperjs使用说明-选择器
casperjs的选择器可以在dom下工作,他既支持css也支持xpath. 下面所有的例子都基于这段html代码: <!doctype html> <html> <he ...
- [译文]casperjs使用说明-使用命令行
使用命令行 Casperjs使用内置的phantomjs命令行解析器,在cli模块里,它传递参数位置的命名选项 但是不要担心不能熟练操控CLI模块的API,一个casper实例已经包含了cli属性,允 ...
- [译文]casperjs 的API-casper模块
Casper class: 可以通过这个模块的create()方法来获取这个模块的一个实例,这是最容易的: var casper = require('casper').create(); 我们也可以 ...
- [译文]casperjs的API-colorizer模块
colorizer模块包含了一个Colorizer类,它能够生成一个标准化的颜色字符串: var colorizer = require('colorizer').create('Colorizer' ...
- [译文]casperjs的API-clientutils模块
casper提供了少量的客户端接口用来进行远程DOM环境注入,通过clientutils模块的ClientUtils类实例中的__utils__对象来执行: casper.evaluate(funct ...
- [译文]casperjs的API-mouse模块
mouse类 这个类是对各种鼠标操作的抽象,比如移动,点击,双击,滚动等.它要求一个已经获得DOM属性的casper对象,能用这种方式创造一个鼠标对象: var casper = require(&q ...
- 使用 CasperJS 构建 Web 爬虫
转载:https://www.oschina.net/translate/building-your-own-web-scraper-in-nodejs 从你的应用中收集数据有时候可能有点困难和艰辛. ...
- 利用BLEU进行机器翻译检测(Python-NLTK-BLEU评分方法)
双语评估替换分数(简称BLEU)是一种对生成语句进行评估的指标.完美匹配的得分为1.0,而完全不匹配则得分为0.0.这种评分标准是为了评估自动机器翻译系统的预测结果而开发的,具备了以下一些优点: 计算 ...
- 聊聊 PHP 私有组件以及如何创建自己的 PHP 组件 (转)
1.私有组件 大多数时候我们使用的都是公开可用的开源组件,但有时候如果公司使用内部开发的PHP组件,而基于许可证和安全方面的问题不能将其开源,就需要使用私有组件.对Composer而言,这是小菜一碟. ...
随机推荐
- java求最长公共子串的长度
1这道题目就是给定两个字符串,然后求这两个字符串的最长公共子串的最大长度,假设我的f()方法是来求两个字符串的最大公共子串,从头开始逐一比较,如果相等,则 继续调用这个方法,使得递归的长度+1,如果不 ...
- 卸载 Windows 8/8.1/10 无法常规卸载的内置应用
现在已经有一款可以卸载内置应用的软件了:http://www.thewindowsclub.com/10appsmanager-windows-10 在应用商店里下了一个计算器+,于是想把内置的计算器 ...
- VMware Workstation 虚拟机暂停后无法启动 出现Exception 0xc0000006 (disk error while paging) has occurred.错误
虚拟机暂停了,突然停电,再开机后无法启动暂停的虚拟机,出现下面的错误 VMware Workstation unrecoverable error: (vmx)Exception 0xc0000006 ...
- Python使用signal模块实现定时执行
在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点在程序中可以用定时器实现这种功能,于是就开始摸索了,发现需要一些信号的知识... ...
- Web大文件(夹)上传(断点续传)控件发布-Xproer.HttpUploader6
版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- form action 相对路径出问题
http://www.w3chtml.com/html5/tag/base.html <base> 标签为页面上的所有链接规定默认地址或默认目标. 通常情况下,浏览器会从当前文档的 URL ...
- 常见的MIME
"css": "text/css", "gif": "image/gif", "html": &qu ...
- python抓网页数据【ref:http://www.1point3acres.com/bbs/thread-83337-1-1.html】
前言:数据科学越来越火了,网页是数据很大的一个来源.最近很多人问怎么抓网页数据,据我所知,常见的编程语言(C++,java,python)都可以实现抓网页数据,甚至很多统计\计算的语言(R,Matla ...
- mORMot使用synDBDataSet时字段类型不正确引起的问题
当SQL表中的nvarchar字段类型内容为null时synDBDataSet使用ftwideMemo类型造成不能修改的问题,按下面红字修改synDBVCL.pas就可以: synDBVCL.pas ...
- css长按复制内容
复制2333333 <style> p { -webkit-user-select: none; user-select: none; } p>i { -webkit-user-se ...