首页 > 解决方案 > 使用每个验证多个元素

问题描述

所以我有一个基本上只有一个图标的列表,我必须运行一个循环并检查图标是否存在。为此,下面的代码就像一个魅力 -

cy.get('span.test').each((element) => {
    cy.wrap(element).find('i').should('have.attr', 'data-icon-name', 'Archive')
});

当相同的列表项有两个图标时会出现问题,然后按照上面的方法,它只检查第一个不是存档的图标并且失败。我如何确保在有多个图标的情况下,它也应该检查其他块,直到找到存档图标。

在此处输入图像描述

标签: javascriptloopseachcypress

解决方案


您正在使用“存档”对支票进行硬编码。您已经在使用 find 遍历元素,您可以改为遍历元素并获取它们的属性值并执行您可能需要的操作。

 cy.get('span.test').each((element) => {
  // this will give you all the data-icon-name attribute values, in your example it will print - LockSolid Archived
  // now you can match the value and do any operations e.g. if(element.getAttribute("data-icon-name") == "Archive") alert("Archive found")
  console.log(element.getAttribute("data-icon-name"));
 });

如果不存在“data-icon-name”属性,它将给出一个空值。


推荐阅读