赛普拉斯:检查 Select 选项是否存在
Cypress: Check if Select Option Exists
我正在尝试使用下面的代码检查我的 select 中是否有一个选项,但它一直失败,有人可以提供帮助吗?
我的 Select 有大约 70 个名字,我试图循环所有这些名字来寻找特定的名字。
cy.get('[id="names"] option').each(($ele) => {
expect($ele).to.have.text('Have This Name')
})
提前致谢,
我不会使用.each()
,只有一个会通过,其他的都会失败。
如果您的文字足够具体(不是多个选项),请使用 .contains()
cy.contains('[id="names"] option', 'Have This Name') // fails only if
// no option has the text
如果必须完全匹配,请过滤选项
cy.get('[id="names"] option')
.filter((idx, el) => el.innerText === 'Have This Name') // fails if filter
// returns 0 items
如果您出于其他原因需要.each()
,这样做
let found;
cy.get('[id="names"] option')
.each(($option) => {
if ($option.text() === 'Have This Name') {
found = $option
return false // return now, have found it
}
})
.then(() => { // after loop exit
expect(found).to.have.text('Have This Name')
})
我正在尝试使用下面的代码检查我的 select 中是否有一个选项,但它一直失败,有人可以提供帮助吗?
我的 Select 有大约 70 个名字,我试图循环所有这些名字来寻找特定的名字。
cy.get('[id="names"] option').each(($ele) => {
expect($ele).to.have.text('Have This Name')
})
提前致谢,
我不会使用.each()
,只有一个会通过,其他的都会失败。
如果您的文字足够具体(不是多个选项),请使用 .contains()
cy.contains('[id="names"] option', 'Have This Name') // fails only if
// no option has the text
如果必须完全匹配,请过滤选项
cy.get('[id="names"] option')
.filter((idx, el) => el.innerText === 'Have This Name') // fails if filter
// returns 0 items
如果您出于其他原因需要.each()
,这样做
let found;
cy.get('[id="names"] option')
.each(($option) => {
if ($option.text() === 'Have This Name') {
found = $option
return false // return now, have found it
}
})
.then(() => { // after loop exit
expect(found).to.have.text('Have This Name')
})