简体   繁体   English

Cypress,获取从 cy.get() 返回的特定元素的索引

[英]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.

相关问题 cy.get() 如何在 Cypress 中放置 2 个 arguments? - cy.get() how to place 2 arguments in Cypress? 如何在cypress中链接cy.get - How to chain cy.get in cypress 赛普拉斯:在 cy.get 中使用变量 - Cypress: Using Variable in cy.get Cypress:cy.get("a").find("b") 和 cy.get("ab") 之间的任何区别 - Cypress: any difference between cy.get("a").find("b") and cy.get("a b") 赛普拉斯:如果找不到任何元素,我可以防止赛普拉斯 cy.get 失败吗? - Cypress: Can I prevent Cypress cy.get from failing if no elements are found? 赛普拉斯如何在两个元素之间使用“cy.get” - Cypress how to use 'cy.get' between two elements Cypress cy.get 返回 HTML 对象,但如果我将 .then(elem) 添加到 cy.get 函数中,则 elem 是一个空的 HTML 对象 - Cypress cy.get returns HTML object, but if I add .then(elem) to the cy.get function elem is an empty HTML object 赛普拉斯有没有办法容纳 cy.get(`[data-cy="${test}"]`).filter(':visible') 和 cy.get(`[data-cy="${test }"]`) 在单个代码中? - Is there a way in cypress to accomodate cy.get(`[data-cy="${test}"]`).filter(':visible') and cy.get(`[data-cy="${test}"]`) in a single code? 使用 cy.get 和 cypress/javascript 输入 html 框找不到 ID - Typing into a html box using cy.get with cypress/javascript not finding the id's 无法在cypress命令cy.get()中设置变量值以在命令外部使用 - Cannot set a variable's value inside cypress command cy.get() to use outside the command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM