量角器测试内部访问角 [英] Accessing Angular inside Protractor Test
问题描述
是否有可能像你做你的量角器测试中访问角单元测试?
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.
请问AP preciate任何帮助!
Would appreciate any help!
推荐答案
有一个名为函数计算()。查找的DOM元素,然后运行前pression。
There is a function called evaluate(). Find an element in the dom and then run the expression.
例如。如果你要计算待办事项在 http://angularjs.org/ 网站数(下添加一些控制),这样做:
For example. If you want to count the number of todos in the http://angularjs.org/ website (under Add Some Control), do this:
在量角器打开元素探险
./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
It should give you a 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);
});
见一个例子:<一href=\"https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js\">https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js
这篇关于量角器测试内部访问角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!