量角器js,转发器没有迭代结果'

时间:2016-07-28 09:04:06

标签: javascript angularjs protractor

在我的protractor.test.js

it('should make sure that there are listings on the page', function()
{
    var count = element.all(by.repeater('res in result'));
    count.then(function(result){
    expect(result.length).toBeGreaterThan(0);
    }, 5000);

})

和我的index.html

  <div class="item item-text-wrap" ng-click="post($event,res)"  ng-repeat="res in result"  ng-controller="recommendedJobsCtrl" ui-sref="menu.jobDetails" >

问题是它说预期0大于0.但是当我在测试或任何其他单词中将其更改为res时,它仍然给出了相同的答案。我不认为它正在阅读我的结果

3 个答案:

答案 0 :(得分:2)

我同意igniteram's answer的部分内容,您应该使用count(),但不能在ElementArrayFinder上使用预期条件(这是element.all返回的内容)。

相反,您也可以尝试使用从alecxe's answer on this question获取的辅助函数。

// in my helper file Util.js

// wait for X number of elements
Util.prototype.presenceOfAll = function (elem, num) {
    console.log('Waiting for elements ' + elem.locator() + ' to have a count of ' + num);
    return browser.wait(function () {
        return elem.count().then(function (count) {
            return count >= num;
        });
    }, 5000, 'Failed waiting for ' + elem.locator() + ' to have ' + num + ' total items');
};

用法:

var userNav = element.all(by.css('li.navbar-item'));
// wait for userNav to have 4 elements/buttons
Util.presenceOfAll(userNav, 4).then(function () {
    // your code
});

另请注意,Protractor修补程序expect隐式处理承诺,因此除非您正在执行其他操作,否则不需要在.then()之后使用.count()。因此,将其应用于您的代码,我会这样修改它:

it('should make sure that there are listings on the page', function() {
    var count = element.all(by.repeater('res in result'));
    Util.presenceOfAll(count, 5); // change 5 for whatever number should be there
    expect(count.count()).toEqual(5);

    // could also have been written as this since presenceOfAll returns a promise
    Util.presenceOfAll(count, 5).then(function() { 
         expect(count.count()).toEqual(5);
    });
});

答案 1 :(得分:0)

为什么不直接使用量角器的count()方法 -

it('should make sure that there are listings on the page', function()
{
var EC = protractor.ExpectedConditions;
var el = element.all(by.repeater('res in result'));
browser.wait(EC.visibilityOf(el), 5000);
expect(el.count()).toBeGreaterThan(0);
})

答案 2 :(得分:0)

您的代码唯一的问题是您实际上从未计算过您选择的元素。

it('should make sure that there are listings on the page', function()
{
    var results = element.all(by.repeater('res in result'));
    results.count().then(function(count){
    expect(count).toBeGreaterThan(0);
    }, 5000);

})

以上示例应该适合您。请注意,为了清楚起见,我更改了一些变量名称。