前言

  项目中需要用到前端自动化测试,自己被当作一个探针研究了下目前用的比较多的web自动化测试工具。一开始研究的是的selenium,但由于项目使用了大量的dijit控件,写起testCase来很费劲;最主要的是selenium有严重的浏览器兼容性问题,于是彻底放弃,改投doh门下,毕竟是dojo他爸爸开发的跟dojo继承起来非常方便。这几篇主要介绍doh、doh/Robot以及将doh与selenium结合进而与CI工具集成起来。

  一、DOH 是什么

  DOH 是 Dojo Objective Harness 的简称,是 Dojo 在 0.9 版本之后新增的单元测试工具。随着 Javascript 代码的数量和复杂度的增加,Web 前端开发者也需要一个 Javascript 代码的单元测试框架来保证自己写出来的 Javascript 代码是健壮的。所以,DOH 是 Web 前端开发者对 JUnit 的回应。DOH有如下特点:

  1. 提供用户界面:JUnit中的红条测试失败、绿条测试通过,大家都已经很熟悉了,DOH也有类似的用户界面,用户在测试时更加一目了然;
  2. 平台无关:DOH并不依赖某种浏览器平台,甚至不依赖于浏览器;用户可以根据自己的需要在命令行进行Javascript的自动化单元测试;
  3. 支持Ajax:Ajax编程在Web前端开发中是必不可少的一环,DOH最有价值的一个特性就是支持Ajax的测试;
  4. 不只适合与于Dojo,可用于任何JavaScript程序的单元测试。

  下载dojo源码包,解压后可以看到以下目录:

  

  二、DOH第一次亲密接触

  部署到服务器后,我们访问runner.html

    

  与JUnit类似,DOH也为我们提供了一个类似的界面,左侧表示运行的测试案例,右侧是测试日志,最上方的进度条表示的是本次运行中的已执行案例,同样绿色表示成功,红色表示失败。默认情况下会加载“dojo/tests/module.js”文件,该文件的作用就是像runner.html中加载dojo所有核心模块的测试案例。

  

  如果想要单独加载某一模块的测试案例,需要用到test参数指向该模块:

http://<server>/util/doh/runner.html?test=dojo/tests/fx

  

  我们可以吧测试案例放到自己的项目目录下,通过test参数指向自定义测试模块,这时需要用到paths参数:

 util/doh/runner.html?paths=org/myorg,../../../mycode/org/myorg&test=org/myorg/mymodule/tests/alltests

  paths参数中逗号前后的值相当于dojoConfig定义packages对象时的name和location    

  

  同样在path中我们可以定义多个模块, 模块之间用“;”分隔开来

util/doh/runner.html?paths=org/myorg,../../../mycode/org/myorg;com/mycom,../../../x/com/mycom&test=com/mycom/tests

  doh中测试模块要么是一个用来请求多个测试文件的文件,要么是一个使用doh.register方法注册时测试案例,或者两者皆有。

define([
"my/test/widget/Foo0",
"my/test/widget/Foo1",
"my/test/widget/Foo2"
]);

  或者

define(["doh/runner"], function(doh){

    doh.register("MyTests", [
function assertTrueTest(){
doh.assertTrue(true);
doh.assertTrue(1);
doh.assertTrue(!false);
},
{
name: "thingerTest",
setUp: function(){
this.thingerToTest = new Thinger();
this.thingerToTest.doStuffToInit();
},
runTest: function(){
doh.assertEqual("blah", this.thingerToTest.blahProp);
doh.assertFalse(this.thingerToTest.falseProp);
// ...
},
tearDown: function(){
}
},
// ...
]); });

  三、测试用例

  doh为我们提供了以下断言函数:

doh.assertTrue(boolean)
Note: This function is aliased to doh.t(); doh.assertFalse(boolean)
Note: This function is aliased to doh.f(); doh.assertEqual(obj1, obj2)
Note: This function is aliased to doh.is(); doh.assertNotEqual(obj1, obj2)
Note: This function is aliased to doh.isNot();

  Junit中,编写一个测试用例我们需要继承Junit类,同样在doh中也有自己的规则来将一段代码,包装成doh可识别的测试案例。

  doh中主要提供了以下方法(最常用的就是doh.register):

doh.register(...)
An almost 'magical' function. The doh.register() method accepts the function signatures of any of the other registration functions and determines the correct underlying function (listed below) to dispatch registration to. It's the function you'll most commonly use for registering Unit Tests.
doh.registerTest(group, testFuncOrObj)
This function registers a test as a member of the group 'group', and the test can either be a simple function definition or a 'Test Fixture', which is an object that defines the run requirements of the test.
doh.registerTests(group, testFuncOrObjArr)
This function registers an array of tests as a member of the group 'group'. The contents of the array of tests can be an array of simple test functions or an array of 'test fixtures', or a mix of them.
doh.registerTestNs(group, obj)
This function registers an object comprised of functions as a member of the group 'group'. Note that this function will only add in non-private (functions without an _ at the beginning of the name), as a test function. If you'd like to use fixtures (setUp(), tearDown(), and runTest()), please use doh.register(), doh.registerTest() or doh.registerTests().
doh.registerTestUrl(url)
This function registers a URL as a location to load tests from. The URL is used to populate the contents of an iframe, and usually refers to an HTML page that boot-loads D.O.H. internally for running tests in a segmented iframe. A good example showing this is the dojo/tests/fx.html. It loads dojo, doh, and then on dojo load completion calls doh.registerTests(). The D.O.H. instance in the iframe will proxy back the results of the test run to the primary D.O.H. instance.

  上面提到的dojo/test/fx模块中我们可以看到该文件中主要定义了两个TestSuite:

define(["doh/main", "require"], function(doh, require){
if(doh.isBrowser){
doh.register("tests.fx", require.toUrl("./fx.html"), 30000);
doh.register("tests.NodeList-fx", require.toUrl("./NodeList-fx.html"), 30000);
}
});

  对应于runner.html中

  

  打开该目录下的fx.html文件,我们可以看到该文件中定义了一系列TestCase

  

  doh中有两种测试结构:

  1、Simple Tests  将一个单独的函数放到doh.register参数testCase数组里

  同步形式:

function mySimpleTest(doh){
doh.assertTrue(true);
}

  异步形式:

function mySimpleAsyncTest(doh){
var deferred = new doh.Deferred();
setTimeout(deferred.getTestCallback(function(){
doh.assertTrue(true);
}), 100);
return deferred;
}

2、Test Fixture

  同步形式:

{
name: "thingerTest",
setUp: function(){
// Setup to do before runTest.//类似于JUnit中的@beforeTest
this.thingerToTest = new Thinger();
this.thingerToTest.doStuffToInit();
},
runTest: function(){
// Our test function to run.//类似于JUnit中的@Test
doh.assertEqual("blah", this.thingerToTest.blahProp);
doh.assertFalse(this.thingerToTest.falseProp);
// ...
},
tearDown: function(){
// cleanup to do after runTest.//类似于JUnit中的@afterTest
},
timeout: 3000 // 3 second timeout.//测试运行时间,超过改时间会报错
}

  异步形式:

{
name: "thingerTest",
setUp: function(){
// Setup to do before runTest.
this.thingerToTest = new Thinger();
this.thingerToTest.doStuffToInit();
},
runTest: function(){
// Our test function to run.
var deferred = new doh.Deferred();
setTimeout(deferred.getTestCallback(function(){
doh.assertEqual("blah", this.thingerToTest.blahProp);
doh.assertFalse(this.thingerToTest.falseProp);
}), 100);
return deferred;
},
tearDown: function(){
// cleanup to do after runTest.
},
timeout: 3000 // 3 second timeout.
}

参考资料:

http://www.infoq.com/cn/articles/dojo-unit-test-DOH/

http://tech.ddvip.com/2010-04/1271148731150772_2.html

前端自动化测试工具doh学习总结(一)的更多相关文章

  1. 前端自动化测试工具doh学习总结(二)

    一.robot简介 robot是dojo框架中用来进行前端自动化测试的工具,doh主要目的在于单元测试,而robot可以用来模仿用户操作来测试UI.总所周知,Selenium也是一款比较流行的前端自动 ...

  2. 前端模块化工具--webpack学习心得

    话说前头 webpack前段时间有听说一下,现在已经到了3.x的版本,自己没去接触.因为之前使用gulp来作为自己的项目构建工具.现在感觉gulp使用的趋势在减少.现在这段时间去接触了webpack, ...

  3. 前端自动化测试工具--使用karma进行javascript单元测试(转)

    Karma+Jasmine+PhantomJS组合的前端javascript单元测试工具. 1.介绍 Karma是由Google团队开发的一套前端测试运行框架,karma会启动一个web服务器,将js ...

  4. 前端见微知著工具篇:Grunt实现自动化

    转载说明 本篇文章为转载文章,来源为[前端福利]用grunt搭建自动化的web前端开发环境-完整教程,之所以转载,是因为本文写的太详细了,我很想自己来写,但是发现跳不出这篇文章的圈子,因为写的详尽,所 ...

  5. nightwatch-前端自动化测试工具安装

    最近再弄这个前端自动化测试工具,刚开始弄了几天,目前为止遇到很多坑,光是安装就费了不少时间,记录一下,以便自己忘记. 这里是它的官网,目前没找到中文版的官网,全英文,对我这个英语渣来说有点难理解. 一 ...

  6. WebDriver自动化测试工具(1)---环境搭建

    Webdriver是一个前端自动化测试工具,可以模拟用户点击链接,填写表单,点击按钮等操作,下面介绍其使用 一.下载WebdriverC#类库以及对应浏览器驱动 http://www.selenium ...

  7. webpack前端构建工具学习总结(一)之webpack安装、创建项目

    npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器 ...

  8. webpack前端构建工具学习总结(二)之loader的使用

    Webpack 本身只能处理 JavaScript 模块,如果要处理其他类型的文件,就需要使用 loader 进行转换. Loader 可以理解为是模块和资源的转换器,它本身是一个函数,接受源文件作为 ...

  9. webpack前端构建工具学习总结(四)之自动化生成项目中的html页面

    接续上文:webpack前端构建工具学习总结(三)之webpack.config.js配置文件 插件的介绍文档:https://www.npmjs.com/package/html-webpack-p ...

随机推荐

  1. final、finally、finalize的区别

    看C#知识点的时候发现的问题 1.final 修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 abstract的,又被声明为f ...

  2. jquery中的append和appendTo用法

    append(content):向每个匹配的元素内部追加内容.这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似. JavaScript代码 <script ty ...

  3. [09]APUE:进程关系

    [a] getpgid / setpgid #include <unistd.h> pid_t getpgid(pid_t pid) //成功返回进程组 ID,出错返回 -1 int se ...

  4. C#实现:给定任意数字,输出在该数字下所有()括号的集合

    class Program { static void Main(string[] args) { getPairs(, , , , ""); Console.ReadKey(); ...

  5. Numpy 学习之路(1)——数组的创建

    数组是Numpy操作的主要对象,也是python数据分析的主要对象,本系列文章是本人在学习Numpy中的笔记. 文章中以下都基于以下方式的numpy导入: import numpy as np fro ...

  6. C# 获取屏幕的大小

    原文地址:http://www.cnblogs.com/zp89850/archive/2011/08/23/2151052.html C# 获取屏幕的大小 WinForm: int iActulaW ...

  7. HTML5中新添加事件

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  8. 三大基础排序算法BubbleSort、SelectSort、InsertSort

    public class Strategy { public static void main(String[] args) { int [] array=new int[]{26,25,15,42, ...

  9. Mysql 启动不了,问题集锦

    1. 报错信息 mysqld_safe mysqld from pid file xxx.pid ended 解决办法: 可能是pid所在目录,没有权限,赋予权限即可 2. 找不到 /tmp/mysq ...

  10. 基于Django的web开发

    github地址:https://github.com/shirleyandgithub/PythonWeb