javascript - 赛普拉斯一开始就停止
问题描述
当我针对该 HTML 代码运行测试时
<body>
<div tester='notmatching'>
foo
</div>
<div tester='matching'>
bar
</div>
</body>
</html>
赛普拉斯并没有超越第一个“div”。
这是我的测试:
context('Home', () => {
beforeEach(() => {
cy.visit('http://example.org')
})
it('Find a div', () => {
cy.get('div').should('have.attr', 'tester', 'matching')
})
})
我收到以下错误:
CypressError: Timed out retrying: expected '[ <div>, 1 more... ]' to have attribute 'tester' with the value 'matching', but the value was 'notmatching'
所以当我把这条线:
cy.get('div').should('have.attr', 'tester', 'notmatching')
有用。
我究竟做错了什么 ?
解决方案
我认为您必须使用 eq 定位第二个匹配元素:
cy.get('div').eq(1).should('have.attr', 'tester', 'matching')
https://docs.cypress.io/api/commands/eq.html#Syntax
编辑:
如果你想遍历它们并检查每一个,你可以这样做:
cy
.get('div')
.each(($el, index, $list) => {
if ($el.attr('tester') === 'matching') {
// do something here
编辑2:
如果您只想将 div 与该属性匹配 - 您可以这样做:
cy.get("div[tester='matching']").should(...
推荐阅读
- ms-access - 错误:“操作必须使用可更新的查询” - MS Access
- clojure - 无法从 Eclipse 运行 clara rules clojure 程序
- sql - 从 CSV 导入事务:错误:平面文件源 1 未通过预执行阶段并返回错误代码 0xC020200E
- composite-primary-key - 如何在 clickhouse 中为数据库设置复合主键?
- java - Magnolia 6.0 Tomcat 9 禁止访问 /.resources*
- python - 在 conda/MacOS 上使用 pip 安装 mmh3 包
- javascript - 如何删除子数组中的重复项?
- reactjs - React Native 遍历数组对象并计算总和值
- vue.js - 如何在 webpack 中正确注入 app 元素?- 接收 [Vue 警告]:找不到元素:#app
- django - Django使用html按钮下载静态文件