【问题标题】:protractor, loop of elements量角器,元素循环
【发布时间】:2018-06-13 22:08:56
【问题描述】:

我是 Protractor 的新手。我正在做自动化测试,在我的功能文件中我有这个要检查:

When('there are at least two ports loaded', function (next) { }

在我的 html 中我有这个:

<div class="port-element ">
   <div class="image-loader" style="display: inline;">
      <picture> (...) </picture>

我正在尝试做一个循环来检查这个,但也许我真的不明白如何:

let port_element = element.all(by.css('port-element '));
      port_element.each(function (item) { 
        console.log('Port element count: ', item);
      });
      next();

但我不明白怎么做。

根据建议,我在 stepdefinition 中使用了这个:

let count = await element.all(by.css('.port-element')).count();
for(let i=1;i<=count;i++)
{
  let selector = 'div.port-element div.image-loader:nth-child('+i+')');
  //selector is the element within each div to now check
}

expect(element.all(by.css('div.port-item div.image-loader')).count()).to.be.above(2)

但我现在有这个错误:

AssertionError: expected ManagedPromise::4833 {[[PromiseStatus]]: "pending"} to be a number or a date

现在我尝试这样做:

let img_loader_count = element.all(by.css('div.port-element div.image-loader'));
    console.log('img loader count: ', img_loader_count.count());                  

但我在控制台上有这个: img 加载器计数:0

img loader 有 12 个,所以这是不可能的。为什么在控制台上打印 0? 有人可以帮我吗?

谢谢。

【问题讨论】:

    标签: javascript loops protractor cucumber cucumberjs


    【解决方案1】:

    我见过的唯一方法是使用 element.count() 作为循环的索引,例如:

    let count = await element.all(by.css('.port-element')).count();
    for(let i=1;i<=count;i++)
    {
      let selector = 'div.port-element div.image-loader:nth-child('+i+')');
      //selector is the element within each div to now check
    }
    

    但是,如果您只是想检查端口元素中是否有一定数量的图像,那么您可以这样做

    expect(element.all(by.css('div.port-element div.image-loader').count()).toBe(2);
    

    每个问题的编辑:
    您应该使用expect() 而不是console.log()。当你尝试记录这样的变量时,js 的异步特性可能会导致奇怪的事情发生,因为它没有包含在 Promise 中。使用 expect(img_loader_count.count()).toBe(12); 应该会给您预期的结果。如果您出于某种原因需要使用console.log(),则必须正确排队:

    element.all(by.css('div.port-element div.image-loader').count().then(function(value){
      console.log('The count is: '+value);
    });
    

    Here 是一些承诺内容的文档。

    【讨论】:

    • 本,我不知道为什么,但现在我有这个错误:TypeError: by.css(...).count is not a function
    • 改变括号的顺序,它返回这个错误: AssertionError: expected ManagedPromise::4833 {[[PromiseStatus]]: "pending"} to be a number or a date
    • @Spicchio 没有一个好的例子很难说。如果您可以提出新问题或将其添加到此问题中,我将很乐意提供帮助。
    • @Spicchio 仅使用我答案的最后一部分。第一部分回答了如何循环元素的技术问题,但我认为没有必要解决您的问题。我有点误读了这个问题。它们是解决问题的两种不同方法,不能同时使用
    • 感谢您的回答。不幸的是,错误与我在问题中添加的错误相同:AssertionError: expected ManagedPromise::4827 {[[PromiseStatus]]: "pending"} to be a number or a date
    【解决方案2】:

    您可以使用 count() 来检查当前页面中加载了多少类。

    我对您的代码做了一些小改动,希望对您有所帮助。

        let port_element = element.all(by.css('.port-element'));
            expect(port_element.count()).toBe(2);
    

    【讨论】:

    • 谢谢Akash,这样可以,但问题是我需要了解如何循环这些元素。看,例如下一个场景是这样的:“我想在每个端口元素上看到一个图像”,所以我必须循环这些元素并检查里面的元素。我怎样才能做到这一点?可以举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2015-12-27
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多