javascript - 使用每个验证多个元素
问题描述
所以我有一个基本上只有一个图标的列表,我必须运行一个循环并检查图标是否存在。为此,下面的代码就像一个魅力 -
cy.get('span.test').each((element) => {
cy.wrap(element).find('i').should('have.attr', 'data-icon-name', 'Archive')
});
当相同的列表项有两个图标时会出现问题,然后按照上面的方法,它只检查第一个不是存档的图标并且失败。我如何确保在有多个图标的情况下,它也应该检查其他块,直到找到存档图标。
解决方案
您正在使用“存档”对支票进行硬编码。您已经在使用 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”属性,它将给出一个空值。
推荐阅读
- angular6 - Angular 6:如何构建一个简单的多个复选框以供用户选中/取消选中?
- pandas - Pandas - 根据数据框上的多个条件操作列
- bash - Mac OS 上的 Bash shell 脚本 - 某些命令只回显到 shell,不执行
- django - 如何在 Django 中锁定交易?
- c - strcpy() 只存储最后一个字符串
- time-complexity - BIG(O) 时间复杂度
- regex - 如何在 Nexus v3 groovy 脚本中使用正则表达式参数
- inno-setup - 如何卸载组件
- mysql - MySQL FTS - 禁用全文搜索的特殊运算符
- android - android将视图从轮播更改为自定义列表视图