在量角器测试中访问 Angular [英] Accessing Angular inside Protractor Test

查看:33
本文介绍了在量角器测试中访问 Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像在单元测试中那样在量角器测试中访问 angular?

Is it possible to access angular within your protractor tests like you do in unit testing?

用例是我有一个转换文本的服务,我想访问该服务以转换实际测试脚本中的一些数据.我知道量角器中有 addMockModule 方法,但我不知道如何为此目的使用它.

Use case is that I have a service that transforms text and I want to access that service to transform some data within the actual test script. I know there is the addMockModule method in protractor but I cannot figure out how to use it for this purpose.

非常感谢您的帮助!

推荐答案

有一个函数叫做evaluate().在dom中找到一个元素,然后运行表达式.

There is a function called evaluate(). Find an element in the dom and then run the expression.

例如.如果您想计算 http://angularjs.org/ 网站(在 Add Some Control 下)中的待办事项数量,请执行以下操作:

For example. If you want to count the number of todos in the http://angularjs.org/ website (under Add Some Control), do this:

在量角器中打开元素浏览器

Open the element explorer in protractor

./node_modules/protractor/bin/elementexplorer.js
browser.get('http://angularjs.org/')
element(by.model('todoText')).evaluate('todos.length').
  then(function(count) {
    console.log(count)
  });

它应该给你一个 2

您也可以使用 executeAsyncScript

You can also use executeAsyncScript

browser.executeAsyncScript(function(callback) {
  // Here we use document.body, but your app may live under a different
  // element.
  var service = angular.element(document.body)
      .injector()
      .get('myService');
  service.query({}, function(data) {
    callback(data);
  });
}).then(function (output) {
  console.log(output);
});

查看示例:https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js

这篇关于在量角器测试中访问 Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆