量角器和 for 循环 [英] protractor and for loops
问题描述
我有一个关于以下在量角器中进行测试的代码的问题.即如您所见,我首先找到标签列表,然后检查其编号(三个).然后我有第一个循环,我将每个标签与我的表中的值进行比较.这里我使用 i <table.length
然后它可以正常工作.在第二个循环中,我使用 labels.count()
它等于三,因为我之前检查过它但它根本不起作用.无论检查的输出是什么,Protractor 都会经历这个循环,并且测试以 PASSED 结束.谁能告诉我为什么循环中的 i
i
I have a question concerning below code for test in Protractor.
Namely as you can see firstly I find list of labels and then I check its number(three). Then I have a first loop where I compare each label with value from my table. Here I use i <table.length
and then it works correctly.
In the second loop I use labels.count()
which is equal to three because i checked it earlier but it doesnt work at all. Protractor goes through this loop no matter what the output of the check is and the test finishes as PASSED.
Can anyone tell me why i <table.length
condition in the loop works and i<labels.count
doesn't?
//labels list
var labels = element.all(by.xpath("//form[@name='form']//label"));
//test start
describe('angularjs homepage', function() {
it('test1', function() {
browser.get('http://www.way2automation.com/angularjs-protractor/registeration/#/login');
//shows 3
labels.count().then(function(text){
console.log( text);
});
var table = ["Usern1ame","Password","Username *"];
//first loop -> this one works if there is a difference between 'table' and element from 'label' list
for (var i = 0; i <table.length; i++) {
expect(labels.get(i).getText()).toEqual(table[i]);
}
//this one doesn't -> if there is a difference between 'table' and 'label'
//list nothing happens, no errors, test passes
for (var i = 0; i <labels.count(); i++) {
expect(labels.get(i).getText()).toEqual(table[i]);
}
});
});
推荐答案
在您的示例中,labels.count()
是一个承诺,您不能直接使用它.要获得count的值,首先需要解析promise.看下面的代码,
In your example, labels.count()
is a promise and you cannot use it directly. To get the value of count, you need to resolve the promise first. Look at below code,
labels.count().then(function(labelCount){
for (var i = 0; i <labelCount; i++) {
expect(labels.get(i).getText()).toEqual(table[i]);
}
})
这篇关于量角器和 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!