[英]Cypress iterate through elements in an array
我正在努力使用 cypress/typescript 遍歷數組中的元素。 我正在記錄一切,並且能夠看到每個索引及其顯示的內容,但我仍然返回整個數組與實際索引,兩天后我無法弄清楚如何獲取實際索引 [0]、[1]、[ 2] 等。我嘗試了很多東西,但這是我最近的嘗試。 感謝您幫助結束我的痛苦!
sportPage.getFirstCard().within(() => {
sportPage.getSecondSection().within(() => {
sportPage
.getMyCoolType()
.should('exist')
.each(($item, $index) => {
cy.wrap($item)
.invoke('text')
.then((text) => {
if ($index !== 0) values.push(text);
cy.log($index.toString());
cy.log(text);
});
})
.then(() => expect(values.toString()).to.equal('Yoga');
});
});
});
});
要測試.getMyCoolType()
返回的元素數組中的各個文本,
sportPage.getFirstCard().within(() => {
sportPage.getSecondSection().within(() => {
sportPage
.getMyCoolType()
.should('exist')
.each(($item, $index) => {
cy.wrap($item)
.invoke('text')
.then((text) => {
cy.log($index.toString());
cy.log(text);
if ($index !== 0) {
expect(text.trim()).to.equal('Yoga');
}
});
})
});
});
或者在循環后測試所有項目
sportPage.getFirstCard().within(() => {
sportPage.getSecondSection().within(() => {
sportPage
.getMyCoolType()
.should('exist')
.each(($item, $index) => {
cy.wrap($item)
.invoke('text')
.then((text) => {
if ($index !== 0) values.push(text);
cy.log($index.toString());
cy.log(text);
});
})
.then(() => expect(values.every(item => item === 'Yoga')).to.equal(true) );
});
});
你也可以擺脫這個
sportPage.getFirstCard().within(() => {
sportPage.getSecondSection().within(() => {
sportPage
.getMyCoolType()
.should('exist')
.then(($items) => {
const texts = [...$items].map(item => item.text()); // map elements into texts
return texts.slice(1); // ignoring the first one
})
.then((items) => expect(items.every(item => item === 'Yoga')).to.equal(true) );
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.