【问题标题】:protractor js, repeater not iterating through the 'result'量角器 js,中继器不遍历“结果”
【发布时间】:2016-07-28 21:30:11
【问题描述】:

在我的 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 或任何其他单词时,它仍然给我相同的答案。我不认为它在读取我的结果

【问题讨论】:

  • 我在您的代码中没有发现任何问题。执行测试时,元素可能尚未创建。尝试等待至少一个元素。
  • @FlorentB。 5000不被认为是等待期吗?你有其他接近等待的方法吗?
  • 第二个参数应该是可选的拒绝回调。所以不,它不会等待 5000 毫秒。看看browser.waitprotractortest.org/#/…
  • @FlorentB。可以举个例子吗

标签: javascript angularjs protractor


【解决方案1】:

我同意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 以隐式处理 Promise,因此您无需在 .count() 之后使用 .then(),除非您正在执行其他操作。所以将它应用到你的代码中,我会这样修改它:

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,感谢您的纠正。我假设 EC 也可以与 element.all 一起使用,是的,它返回一个 element array finder
  • @igniteram1 是的,我希望它与工作 element.all 一起使用 :( 会非常有用
【解决方案2】:

为什么不直接使用量角器的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);
})

【讨论】:

  • 您可以使用显式等待来确定可见性,更新我的答案!也请使用browser.pausebrowser.debugger 了解该特定元素发生了什么。
  • 你应该试试@Gunderson 的建议!
【解决方案3】:

您的代码的唯一问题是您从未真正计算过您选择的元素。

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);

})

上面的例子应该适合你。请注意,为了清楚起见,我更改了一些变量名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2010-11-16
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多