使用 CasperJS 对 Web 网站进行功能测试
作者:网络转载 发布时间:[ 2013/9/4 13:46:46 ] 推荐标签:
CasperJS 包含一些非常有用的工具用来对 Web 网站进行功能性的测试,例如我们可以编写如下的 JavaScript 脚本来测试 Google 的搜索:
view source
print
01 var casper = require('casper').create();
02
03 casper.start('http://www.google.fr/', function() {
04 this.test.assertTitle('Google', 'google homepage title is the one expected');
05 this.test.assertExists('form[action="/search"]', 'main form is found');
06 this.fill('form[action="/search"]', {
07 q: 'foo'
08 }, true);
09 });
10
11 casper.then(function() {
12 this.test.assertTitle('foo - Recherche Google', 'google title is ok');
13 this.test.assertUrlMatch(/q=foo/, 'search term has been submitted');
14 this.test.assertEval(function() {
15 return __utils__.findAll('h3.r').length >= 10;
16 }, 'google search for "foo" retrieves 10 or more results');
17 });
18
19 casper.run(function() {
20 this.test.renderResults(true);
21 });
如你所见,casper.test 是 tester.Tester 对象实例的引用,用来对结果进行断言和输出。
tester.Tester API documentation 请看 dedicated section.
现在开始运行这个测试脚本:
view source
print?
$ casperjs samples/googletest.js
下面是运行结果:
如果是断言失败,那可能是下面这种结果:
将结果导出为 xUnit 格式

sales@spasvo.com