Zor*_*ran 6 arrays assert cypress
有人可以帮忙提供以下信息:简短说明:打开一页,获取文本元素,然后打开第二页并在 4 或 5 个元素中,需要断言第一页中的该元素位于这几个元素创建的数组内。写了这段代码:
Cypress.Commands.add(
'assertForOpenedElementVisible',
(list1, list2, notDisplayedElementMsg) => {
const textsArray = []
cy.get('body').then((body) => {
if (body.find(list1).length > 0) {
cy.get(list1).each(($el, index) => {
const text1 = $el.text().replace(', ', '')
cy.get(list1).eq(index).click()
cy.wait(1000)
cy.get(list2)
.each(($el, index) => {
const text = $el.text().replace(', ', '')
textsArray.push(text)
cy.log(textsArray)
cy.log(text)
})
.then(() => {
cy.wrap(expect(textsArray).to.include(text1))
})
})
} else {
cy.log(notDisplayedElementMsg)
}
})
}
)
Run Code Online (Sandbox Code Playgroud)
当检查测试运行程序时 - 我得到了元素,但测试失败:
如何正确断言?先感谢您
您可以进行如下断言:
expect(text1).to.be.oneOf(textsArray)
Run Code Online (Sandbox Code Playgroud)
或者,您可以直接断言而不使用each()如下:
cy.get(list2).should(($list2) => {
expect($list2.eq(3)).to.contain('49') //If you know the position
})
cy.get(list2)
.invoke('text')
.then((text) => {
expect(text).to.contain('49') //If you don't know the position
})
Run Code Online (Sandbox Code Playgroud)