[英]Cypress, get the index of a specific element returned from cy.get()
I'm writing Cypress tests for a website that has a series of quotations, some of which have a "Read More" button at the end, which displays the rest of the quote.我正在为一个有一系列报价的网站编写赛普拉斯测试,其中一些报价的末尾有一个“阅读更多”按钮,它显示了报价的 rest。 If I use
如果我使用
cy.get(".quote")
it returns a list of several quotes.它返回几个引号的列表。 I can use
我可以用
cy.get(".quote").find(".read-more").first()
to find the first quote that has a Read More button, but I want to know what the index of that element is in the original list of quotes.找到具有阅读更多按钮的第一个引号,但我想知道该元素的索引在原始引号列表中是什么。 I'm testing to make sure that the Read More button correctly reveals the full quote, but the button disappears (entirely removed from the DOM, not just set to visibility: none) once it's been clicked, so I can't use the same command to find the quote again.
我正在测试以确保“阅读更多”按钮正确显示完整的引用,但是一旦单击该按钮就会消失(完全从 DOM 中删除,而不仅仅是设置为可见性:无),所以我不能使用相同的命令以再次查找报价。 If I could access the element of that quote, I could just use
如果我可以访问该引用的元素,我可以使用
cy.get(".quote").eq(element)
to pull out that specific quote again once the Read More button is gone.在“阅读更多”按钮消失后再次提取该特定报价。
You can do something like this.你可以做这样的事情。 Apply
each
over the quotes, then inside each search for the quote you are looking for and then get the index of that quote.将
each
应用在引号上,然后在每次搜索中搜索您要查找的报价,然后获取该报价的索引。
cy.get('.quote').each(($ele, index) => {
if ($ele.text().includes('some part of the quote')) {
cy.log(index) //logs the index
}
})
Read More button correctly reveals the full quote then button disappears once clicked阅读更多按钮正确显示完整报价,然后单击后按钮消失
Assuming the read more
button removes the .read-more
from the quote element after being clicked, then you can do the following.假设
read more
按钮在单击后从引用元素中删除.read-more
,那么您可以执行以下操作。
cy.get('.quote') // returns all quotes
.find('.read-more') // only quotes with 'read more'
.each(($quote) => {
cy.wrap($quote).find('.read-more-button').should('be.visible').click() //check read more button is visible, then click
cy.wrap($quote).invoke('text').should('include.text',COMPLETE_QUOTE) // you can alter the should to however you best see to the text assertion
cy.wrap($quote).find('.read-more-button').should('not.exist') // particular read more button does not exist in DOM
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.