How to expect dynamic count of elements in e2e tests using Protractor(如何使用量角器在 e2e 测试中预期元素的动态计数)
问题描述
我目前正在使用 Protractor 为我不起眼的 Angular 应用程序编写一些 e2e 测试.
I'm currently writing some e2e tests for my humble Angular app with Protractor.
我的应用程序运行良好,单元测试通过了所有测试,也使用了 e2e...直到这个:
My app works fine, unit tests passes all, e2e used too... until this one:
appE2ESpec.js
describe('adding an item', function() {
var items,
addItemButton,
startCount;
beforeEach(function() {
items = element.all(by.css('li.item'));
addItemButton = element(by.id('addItemButton'));
startCount = items.count();
});
it('should display a new item in list', function() {
addItemButton.click();
expect(items.count()).toEqual(startCount+1);
});
});
这就是我编写测试的方式,但是,
This is how I would have written my test but,
问题是: items.count() 返回一个承诺,我知道,但我无法强制 Protractor 解决它.所以我明白了:
The problem is: that items.count() returns a promise, I know that, but I can't manage to force Protractor to resolve it. So I get this:
Failures:
1) myApp adding an item should display a new item in list
Message:
Expected 6 to equal '[object Object]1'.
我的尝试:
items.count().then(function(count) {
startCount = count;
//console.log(startCount) --> "6" Perfect!
});
但是最后得到了同样的结果……我不能把expect放到then里面,我也想过.
But got the same result at the end... I can't put the expect into the then, I thought about that too.
- 我搜索了 Protractor GitHub 存储库问题、StackOverflow 和 Google AngularJs 组.
附录:
console.log(startCount) 输出:
{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }
我本可以编写 .toEqual(6),但我不想在每次需要更改应用启动状态时都重写测试.
I could have written .toEqual(6) but I don't want to rewrite my test each time I need to change my app startup state.
有什么想法吗?提前致谢!!
Any idea? Thanks in advance!!
推荐答案
你需要先解析promise,然后进行断言.
You need to resolve the promise and then do the assertion.
Protractor 将解析你传递给 expect() 的 Promise,但它不能在 Promise 中添加数字.你需要先解决promise的值:
Protractor will resolve the promise that you pass to expect(), but it cannot add a number to a promise. You need to resolve the value of the promise first:
beforeEach(function() {
...
items.count().then(function(originalCount) {
startCount = originalCount;
});
});
it('should display a new item in list', function() {
...
expect(items.count()).toEqual(startCount+1);
});
这篇关于如何使用量角器在 e2e 测试中预期元素的动态计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用量角器在 e2e 测试中预期元素的动态计数
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
