我试图断言名称显示在表的列中.我编写了一个inResults
函数,它将迭代列的文本以查看是否存在名称.这是我正在尝试的:
页面对象:
this.names = element.all(by.repeater('row in rows').column('{{row}}'));
this.inResults = function(nameString) {
var foundit = '';
this.names.each(function(name) {
name.getText().then(function(it) {
console.log(it); // each name IS printed...
if(it == nameString) {
console.log('it\'s TRUE!!!!'); // this gets printed...
foundit = true;
}
});
});
return foundit; // returns '' but should be true?
};
Run Code Online (Sandbox Code Playgroud)
规格期望:
expect(friendPage.inResults('Jo')).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
两个控制台语句按预期打印...但我的期望失败,因为foundit
价值仍然存在''
.我已经尝试了很多方法而且都没有.我错过了什么?
我已经设计出了我认为更好/更清洁的方法来解决这个问题.它不那么复杂,并且在方法中不需要定位符/ css代码.
friend.page.js
// locator
this.friendName = function(text) { return element.all(by.cssContainingText('td.ng-binding', text)) };
// method
this.inResults = function(name) {
return this.friendName(name).then(function(found) {
return found.length > 0;
});
};
Run Code Online (Sandbox Code Playgroud)
friend.spec.js
expect(friendPage.inResults('Jo')).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
我已将此添加到GitHub上的protractor_example项目中 ...
我建议您使用过滤器:http://angular.github.io/protractor/#/api ?view=ElementArrayFinder.prototype.filter
this.inResults = function(nameString) {
return this.names.filter(function(name) {
return name.getText().then(function(text) {
return text === nameString;
});
}).then(function(filteredElements) {
// Only the elements that passed the filter will be here. This is an array.
return filteredElements.length > 0;
});
});
// This will be a promise that resolves to a boolean.
expect(friendPage.inResults('Jo')).toBe(true);
Run Code Online (Sandbox Code Playgroud)