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 &quot;casperjs&quot; 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使用说明-测试的更多相关文章

  1. [译文]casperjs使用说明-选择器

    casperjs的选择器可以在dom下工作,他既支持css也支持xpath. 下面所有的例子都基于这段html代码: <!doctype html> <html> <he ...

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

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

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

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

  4. [译文]casperjs的API-colorizer模块

    colorizer模块包含了一个Colorizer类,它能够生成一个标准化的颜色字符串: var colorizer = require('colorizer').create('Colorizer' ...

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

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

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

    mouse类 这个类是对各种鼠标操作的抽象,比如移动,点击,双击,滚动等.它要求一个已经获得DOM属性的casper对象,能用这种方式创造一个鼠标对象: var casper = require(&q ...

  7. 使用 CasperJS 构建 Web 爬虫

    转载:https://www.oschina.net/translate/building-your-own-web-scraper-in-nodejs 从你的应用中收集数据有时候可能有点困难和艰辛. ...

  8. 利用BLEU进行机器翻译检测(Python-NLTK-BLEU评分方法)

    双语评估替换分数(简称BLEU)是一种对生成语句进行评估的指标.完美匹配的得分为1.0,而完全不匹配则得分为0.0.这种评分标准是为了评估自动机器翻译系统的预测结果而开发的,具备了以下一些优点: 计算 ...

  9. 聊聊 PHP 私有组件以及如何创建自己的 PHP 组件 (转)

    1.私有组件 大多数时候我们使用的都是公开可用的开源组件,但有时候如果公司使用内部开发的PHP组件,而基于许可证和安全方面的问题不能将其开源,就需要使用私有组件.对Composer而言,这是小菜一碟. ...

随机推荐

  1. BMP结构详解

    位图BITMAPINFOHEADER 与BITMAPFILEHEADER: 先来看BITMAPINFOHEADER,只写几个主要的biSize包含的是这个结构体的大小(包括颜色表)    biWidt ...

  2. 设计模式(java)--状态模式

    状态模式(State Pattern)是设计模式的一种,属于行为模式. 定义(源于Design Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要 ...

  3. php eval

    // Our PHP code inside a variable $phpCode = ' class Foo { private $name; public function __construc ...

  4. 【转载】redis.conf文件详解

    转载地址:http://blog.csdn.net/zhutulang/article/details/51969760 Redis.conf文件可以在github上查看,下面是我整理的其中的配置项( ...

  5. 敏捷开发之XP

    敏捷方法论有一个共同的特点,那就是都将矛头指向了“文档”,它们认为传统的软件工程方法文档量太“重”了,称为“重量级”方法,而相应的敏捷方法则是“轻量级”方法.正是因为“轻量级”感觉没有什么力量,不但不 ...

  6. Alpha冲刺(六)

    Information: 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 - 过去两天完成了哪些任务 1. 基于ssm框架的前后端交互 ...

  7. Hortonwork Ambari配置Hive集成Hbase的java开发maven配置

    集群环境 ambari 2.7.3 hdp/hortonwork 2.6.0.3 maven <dependency> <groupId>org.apache.hive< ...

  8. 【Linux】程序、进程和线程的区别

    程序.进程和线程的区别 程序是一组指令及参数的集合,指令按照既定的逻辑控制计算机运行.进程则是运行着的程序,是操作系统执行的基本单位.线程则是为了节省资源而可以在同一个进程中共享资源的一个执行单位. ...

  9. Sublime Text 3 Settings

    Preferences - Setting User - > : { "font_size": 14, "word_wrap": true, " ...

  10. Log4net日志

    log4net简介(摘抄于百度百科):      log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库 ...