我目前正在使用 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.
附录:
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 测试中预期元素的动态计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)
XMLHttpRequest 的 getResponseHeader() 的限制?Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)