gpt4 book ai didi

cypress - Cypress 惯用的断言 DOM 中存在两个包含某些文本的元素的方法

转载 作者:行者123 更新时间:2023-12-02 02:05:37 25 4
gpt4 key购买 nike

使用 Cypress,断言给定选择器(也包含一些文本)的 DOM 中存在两个元素的惯用方法是什么?

以下是我在 JavaScript 中执行此操作的方法:

Array.from(document.querySelectorAll("selector")).filter(node => node.textContent.includes("text")).length === 2

在 Cypress 中是否有一种惯用的方法可以做到这一点?

我已经尝试过:

cy.get('selector')
.contains('text')
.should('have.length', 2);

但我收到以下错误:

cy.contains() cannot be passed a length option because it will only ever return 1 element.

最佳答案

您可以将 filter()contains() 结合使用,如下所示。 (Cypress Docs)

cy.get('selector')
.filter(':contains("text")')
.should('have.length', 2);

关于cypress - Cypress 惯用的断言 DOM 中存在两个包含某些文本的元素的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68500691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com