iteration - 赛普拉斯:检查href列表,断言它们不包含某个字符串
问题描述
我有一个需要检查的 href 列表。我只需要确保它们都不包含字符串“creditcheck”
html 看起来像这样:
<div class="yiiTab clearfix content-right-side" id="clientinfotabs">
<ul class="tabs left-side-tabs">
<li>
<a reloadonclick="" loadurl="/index.php?r=foo/bar;id=12345&language=de" href="#foofoobar" class="active">Test<span class="tab-item-count">8</span></a></li>
<li>
<li>
<li>
<li>
.
.
.
</ul>
总体思路是这样的:
- 获取 ul
- 遍历每个 li 项目
- 使用 .children() 获取“a”
- 调用 href 并与字符串进行比较
我尝试遍历列表并使用多种方法检查href。这是我最近的尝试:
describe('check hrefs', () => {
it('check hrefs', () => {
cy.login()
cy.visit('url3')
cy.get('tbody > :nth-child(1) > .col-id-large').click({force: true})
cy.get('#clientinfotabs > ul')
.each(($el) => {
cy.get($el).children().invoke('attr','href').should('not.include','creditcheck');
})
})
})
这就是我运行此脚本时赛普拉斯返回的内容:
AssertionError
Timed out retrying after 10000ms: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given
cypress/integration/test2.spec.js:10:62
8 | cy.get('#clientinfotabs > ul')
9 | .each(($el) => {
> 10 | cy.get($el).children().invoke('attr','href').should('not.include','creditcheck');
| ^
11 | })
12 | })
13 | })
解决方案
所以你真的很亲近。而不是cy.get()
你必须使用cy.wrap()
. 你也可以使用 selector 直接循环 li #clientinfotabs > ul > li
。
describe('check hrefs', () => {
it('check hrefs', () => {
cy.login()
cy.visit('url3')
cy.get('tbody > :nth-child(1) > .col-id-large').click({force: true})
cy.get('#clientinfotabs > ul >li').each(($el) => {
cy.wrap($el).invoke('attr', 'href').should('not.include', 'creditcheck')
})
})
})
推荐阅读
- magento - 编辑 admin/sales_order/index 页面
- video - 如何在 macOS 上使用带有 gpu 支持的 ffmpeg
- google-maps - 使用 Flutter Google Maps 插件自定义标记
- c# - PS Add-Type 期间“找不到元文件 .dll”(C# 代码)
- node.js - 如何使用 REST PI 自分配证书:Angular
- android - 如何通过更改文件来切换手机的蓝牙状态?
- python - 如何对法律领域的文本文档进行分类
- docker - 在其他 docker 容器中访问网络
- scala - 如何在 spark/scala 中编写或调用 -SQL 游标?
- airflow - dag 中的气流“并发”参数不一致