【发布时间】:2020-10-14 13:40:16
【问题描述】:
在 Cypress 中,我试图计算有多少元素(在本例中为 li 中有多少按钮)包含文本。当使用“contains”时,返回的项目数总是等于一个,因为“contains”只给出文档中包含搜索文本的第一个项目。
cy.get('li')
.contains('button', 'Submit')
.its('length')
.then(elLength => {
// I want to test here the number of all buttons in li elements containig word 'Submit'
}
当然,这样不行,因为 elLength 始终为 1(如果未找到项目,则为 0)。
赛普拉斯有没有其他方法可以返回所有带有文本的元素,我可以计算它们?
【问题讨论】:
-
最后,在获得了非常鼓舞人心的反对票后;),我找到了解决这个问题的方法:
cy.get('li').then($el => { cy.wrap($el).find(Cypress.$('button:contains("Submit")')) .its('length').should('eq', expected_number)