問題描述
我目前正在使用 Protractor 為我不起眼的 Angular 應(yīng)用程序編寫一些 e2e 測(cè)試.
I'm currently writing some e2e tests for my humble Angular app with Protractor.
我的應(yīng)用程序運(yùn)行良好,單元測(cè)試通過了所有測(cè)試,也使用了 e2e...直到這個(gè):
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);
});
});
這就是我編寫測(cè)試的方式,但是,
This is how I would have written my test but,
問題是: items.count() 返回一個(gè)承諾,我知道,但我無法強(qiáng)制 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!
});
但是最后得到了同樣的結(jié)果……我不能把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 存儲(chǔ)庫(kù)問題、StackOverflow 和 Google AngularJs 組.
附錄:
console.log(startCount)
輸出:
{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }
我本可以編寫 .toEqual(6)
,但我不想在每次需要更改應(yīng)用啟動(dòng)狀態(tài)時(shí)都重寫測(cè)試.
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,然后進(jìn)行斷言.
You need to resolve the promise and then do the assertion.
Protractor 將解析你傳遞給 expect() 的 Promise,但它不能在 Promise 中添加數(shù)字.你需要先解決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);
});
這篇關(guān)于如何使用量角器在 e2e 測(cè)試中預(yù)期元素的動(dòng)態(tài)計(jì)數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!