原版地址:http://docs.angularjs.org/guide/dev_guide.e2e-testing

  当一个应用的复杂度、大小在增加时,使得依靠人工去测试新特性的可靠性、抓Bug和回归测试是不切实际的。

  为了解决这个问题,我们建立了Angular Scenario Runner,模仿用户的操作,帮助我们去验证angular应用的健壮性。

一、   总括

  我们可以在javascript中写情景测试(scenario test),描述我们的应用发生的行为,在某个状态下给与某些互动。一个情景包含一个或者多个”it”块(我们可以将这些当作对我们应用的要求),依次由命令(command)和期望(expectation)组成。command告诉Runner在应用中做某些事情(例如转到某个页面或者单击某个按钮),expectation告诉runner去判断一些关于状态的东西(例如某个域的值或者当前的URL)。如果任何expectation失败了,那么runner标记这个”it”为”false”,然后继续下一个”it”。Scenario也可以拥有” beforeEach”和” afterEach”block,这些block会在每一个”it”block之前或者之后运行,不管它是否通过。

  除了上述元素外,scenario也可以包含helper function,避免在”it”block中有重复的代码。

  这里是一个简单的scenario例子:

 
describe('Buzz Client', function() {
it('should filter results', function() {
input('user').enter('jacksparrow');
element(':button').click();
expect(repeater('ul li').count()).toEqual(10);
Input('filterText').enter('Bees');
expect(repeater('ul li').count()).toEqual(1);
});
});
 

  这个scenario描述了网络客户端的要求,明确地,它应该有过滤user的能力。它开始的时候,输入了一个值到”user”输入框中,单击页面上唯一的按钮,然后它验证是否有10个项目列表。然后,它输入”Bees”到”filterText”的输入框中,然后验证那个列表是不是会减少到只有一个项。

下面的API章节,列出了在Runner中可用的command和expectation。

二、   API

  源代码:https://github.com/angular/angular.js/blob/master/src/ngScenario/dsl.js

  pause()

  暂停执行测试,直到我们在console中调用resume()(也可以在Runner界面中点击resume链接)

  

  sleep(seconds)

  暂停测试执行N秒。

  browser().navigateTo(url)

  在tset frame中加载指定url。

  browser().navigateTo(url,fn)

  在test frame中加载fn返回的url地址。这里的url参数只是用作测试输出。当目的url是动态的时候可以使用这个API(写测试的时候,地址还是未知的)。

  browser().reload()

  在test frame中刷新当前加载的页面。

  browser().window().href()

  返回test frame当前页面的window.location.href。

  browser().window().path()

  返回test frame当前页面的window.location.pathname。

  browser().window().search()

  返回test frame当前页面的window.location.search。

  

  browser().window().hash()

  返回test frame当前页面的window.location.hash(不包含#)。

  browser().location().url()

  返回test frame 当前页面的$location.url()的返回结果(http://docs.angularjs.org/api/ng.$location)

  browser().location().path()

  返回test frame 当前页面的$location. path ()的返回结果(http://docs.angularjs.org/api/ng.$location)

  browser().location().search()

  返回test frame 当前页面的$location. search ()的返回结果(http://docs.angularjs.org/api/ng.$location)

  browser().location().hash()

  返回test frame 当前页面的$location. hash ()的返回结果(http://docs.angularjs.org/api/ng.$location)

  expect(future).{matcher}

  判断给定的期望(future)值是否满足matcher。所有API的声明都返回一个在它们执行完毕之后获取到的一个指定值的future对象。matcher是使用angular.scenario.matcher定义的,他们使用futures的值去执行expectation。例如:

  

expect(browser().location().href()).toEqual(‘http://www.google.com’);

  expect(future).not().{matcher}

  判断给定future的值是否与指定的matcher的预期相反。

  using(selector,label)

  Scopes the next DSL element selection.(大概是限定选择器的作用域,label估计是用于测试输出)

  例子:

using('#foo', "'Foo' text field").input('bar')

  binding(name)

  返回第一个与指定的name匹配的绑定(也许是跟ng-bind相关)。

  input(name).enter(value)

  输入指定的value到name指定的表单域。

  

  input(name).check()

  选中或者解除选中指定name的checkbox。

  input(name).select(value)

  选中指定name的radio中值为value的input[type=” radio”]。

  input(name).val()

  返回指定name的input的当前值。

  repeater(selector,label).count()

  返回与指定selector(jQuery selector)匹配的repeater的行数。label只用作测试输出。

  

repeater('#products table', 'Product List').count() //number of rows

  repeater(selector,label).row(index)

  返回一个数组,绑定指定selector(jQuery selector)匹配的repeater中指定index的行。label仅仅用于测试输出。

repeater('#products table', 'Product List').row(1) //all bindings in row as an array

  repeater(selector,label).column(binding)

  返回一个数组,值为指定selector(jQuery selector)匹配的repeater中符合指定binding的列。label仅仅用于测试输出。

repeater('#products table', 'Product List').column('product.name') //all values across all rows in an array

  

  select(name).option(value)

  选择指定name的select中指定value的option。

  select(name).option(value1,value2)

  选择指定name的select中指定value的option(多选)。

  element(selector,label).count()

  返回与指定selector匹配的元素的个数。label仅仅用作测试输出。

  element(selector,label).click()

  单击与指定selector匹配的元素。label仅仅用作测试输出。

  element(selector,label).query(fn)

  执行指定的fn(selectedElements,done),selectedElement就是与指定selector匹配的元素集合;而done是一个function,会在fn执行完毕后执行。label仅仅用作测试输出。

  element(selector,label).{method}()

  返回在指定selector匹配的元素上执行method的返回值。method可以是以下的jQuery方法:val、text、html、height、innerHeight、outerHeight、width、innerWidth、outerWidth、position、scrollLelft、scrollTop、offset。label仅仅用作测试输出。

  element(selector,label).{method}(value)

  在指定selector匹配的元素上执行指定method,并以key、value作为参数。method可以是以下的jQuery方法:val、text、html、height、innerHeight、outerHeight、width、innerWidth、outerWidth、position、scrollLelft、scrollTop、offset。label仅仅用作测试输出。

  element(selector,label).{method}(key)

  返回在指定selector匹配的元素上执行指定method的结果,这些方法可以是以下的jQuery方法:attr,prop,css。label仅仅用作测试输出。

 

  element(selector,label).{method}(key,value)

在指定的selector匹配的元素上执行method并以key、value作为参数,这些方法可以是以下的jQuery方法:attr,prop,css。label仅仅用作测试输出。

  

  javascript是动态类型的语言,带来了强大力量的表达式,但它同时让我们从编译器中几乎得不到任何帮助。因此,我们很强烈地感受到,任何用javascript写的代码都需要进行大量、全面的测试。angular有很多特性,可以让我们更加容易地测试我们的应用。所以我们没有借口不去写测试。(-_-!!)

  1. AngularJs学习笔记--bootstrap
  2. AngularJs学习笔记--html compiler
  3. AngularJs学习笔记--concepts
  4. AngularJs学习笔记--directive
  5. AngularJs学习笔记--expression
  6. AngularJs学习笔记--Forms
  7. AngularJs学习笔记--I18n/L10n
  8. AngularJs学习笔记--IE Compatibility
  9. AngularJs学习笔记--Modules
  10. AngularJs学习笔记--Scope
  11. AngularJs学习笔记--Dependency Injection
  12. AngularJs学习笔记--Understanding the Model Component
  13. AngularJs学习笔记--Understanding the Controller Component
  14. AngularJs学习笔记--E2E Testing
  15. AngularJs学习笔记--Understanding Angular Templates
  16. AngularJs学习笔记--Using $location
  17. AngularJs学习笔记--Creating Services
  18. AngularJs学习笔记--Injecting Services Into Controllers
  19. AngularJs学习笔记--Managing Service Dependencies
  20. AngularJs学习笔记--unit-testing

AngularJs学习笔记--E2E Testing的更多相关文章

  1. AngularJs学习笔记--Forms

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...

  2. AngularJs学习笔记--expression

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...

  3. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

  4. AngularJs学习笔记--Guide教程系列文章索引

    在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...

  5. AngularJs学习笔记--bootstrap

    AngularJs学习笔记系列第一篇,希望我可以坚持写下去.本文内容主要来自 http://docs.angularjs.org/guide/ 文档的内容,但也加入些许自己的理解与尝试结果. 一.总括 ...

  6. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...

  7. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  8. AngularJs学习笔记--Using $location

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/dev_guide.services.$location 一.What does it do? $loc ...

  9. AngularJs学习笔记--unit-testing

    原版地址:http://docs.angularjs.org/guide/dev_guide.unit-testing javascript是一门动态类型语言,这给她带来了很强的表现能力,但同时也使编 ...

随机推荐

  1. ubuntu下安装Sublime Text2

    1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用,如linjiqin@ubuntu:~$ sudo ...

  2. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  3. r.js 配置文件 example.build.js 不完整注释

    /* * This is an example build file that demonstrates how to use the build system for * require.js. * ...

  4. 通过mongodump和mongorestore实现Mongodb备份和恢复

    Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复. mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询, ...

  5. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jboss.resteasy.plug

    之前做的项目是resteasy的上传,代码没有问题,断点都不进来呢. 我以为可以直接移植到SpringMVC,但是SpringMVC不支持MultipartFormDataInput , 用Multi ...

  6. Halcon学习笔记——条形码的定位与识别

    一维码的原理与结构 条码基本原理是利用条纹和间隔或宽窄条纹(间隔)构成二进制的”0“和”1“,反映的是某种信息. 一维条码数据结构,分四个区域.组成分别为静区.起始/终止符.校验符.数据符. 一维条码 ...

  7. Shiro登录校验

    shiro是一种权限认证框架,实现一个简单的登录鉴权: 1.控制器层: @Controller @RequestMapping("/blogger") public class B ...

  8. Java Object类的toString()方法

    Java只要定义一个类,那么它都在继承,没有说明它在继承哪个类的时候,则默认继承java.lang.Object类,也就是说Object类是所有类的父类.看下面一段代码. public class O ...

  9. 操作Hadoop集群

    操作Hadoop集群 所有必要的配置完成后,将文件分发到所有机器上的HADOOP_CONF_DIR目录.这应该是所有机器上相同的目录. 一般来说,建议HDFS和YARN作为单独的用户运行.在大多数安装 ...

  10. 讲解JavaScript两个圆括号、自调用和闭包函数

    一.JavaSript圆括号的使用 先来看一组通过函数声明来定义的函数: 先附代码: 运行结果如下: 这里我们可以看出: Ø  若没有加圆括号,则返回的是这个函数的内容 Ø  若加上圆括号,则返回的是 ...