原版地址: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. [PY3]——创建多值映射字典?/defaultdict默认字典/setdefault()

    Defaultdict 默认字典 collections 模块中的 defaultdict(默认字典),可以用来构造“一个键映射多个值”这样的字典 如果你想保持元素的插入顺序就应该使用list, 如果 ...

  2. solr6.6教程-基础环境搭建(一)

    目前网上关于solr6.+的安装教程很少,有些6.0之前的教程在应用到6.+的版本中出现很多的问题,所以特别整理出来这一片文章,希望能给各位码农一些帮助! 很少写些文章,如有不对的地方,还希望多多指导 ...

  3. 【转】C#中的委托,匿名方法和Lambda表达式

    简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...

  4. c#中的程序集

     程序集:一些相关类的包,比如三层中Model层都是一些数据库表的实体类.我们所用到的类都是位于各个程序集中,若需要调用某个类,就必须引用其所在的程序集. 访问级别:程序集中的类有四种访问级别,int ...

  5. node.js控制请求处理数量

    问题: 现在有一个接口,这个接口用到了无头浏览器,总之是一个比较消耗内存的接口,并发上来后,这个接口会把服务器内存榨干,导致服务器宕机.现在在不加机器的情况下,并发上来后我该怎么做既能处理掉所有请求又 ...

  6. linux环境的基本搭建

    1.准备Linux环境(我的是centos系统) 如果你是hadoop用户在使用sudo之前需要配置一下:获取sudo权限 切换到root vi /etc/sudoersroot ALL=(ALL) ...

  7. 卸载或安装程序出现:The feature you are trying to use is on a network resource ...

    卸载或安装程序出现:The feature you are trying to use is on a network resource ... 这种情况可能是因为原先已经安装过这个软件,所以要先卸载 ...

  8. java 中国网建实现发送短信验证码

    现在中国网建上注册一个自己的账户, 然后里面有代码案例,也有相应的下载jar包的地址 package com.direct.note; import java.io.IOException; impo ...

  9. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1

    1.异常提示: 12:59:10.000 [http-nio-8080-exec-40] DEBUG o.s.j.s.SQLStateSQLExceptionTranslator - Extracte ...

  10. 微信小程序中使用wxParse展示HTML内容

    wxParse的GitHub地址:https://github.com/icindy/wxParse 一.数据内容: 请求地址:https://m.quanchepin.com/index.php?a ...