简体   繁体   English

赛普拉斯在元素列表中查找特定 ID

[英]Cypress Find specific ID within list of elements

I want to assert that a specific ID exists in a list of elements,我想断言元素列表中存在特定的 ID,

This is what I tried:这是我尝试过的:

assertSelectedIdMovedToAnotherList() {
  // selectedId yeilded from another function
  cy.get(@selectedId).then(($selectedId) => {
    cy.get(app .idListA).should('have.id', $selectedId)
  });
}

When I run this code in the test, I find the list but it compares only the first Id of the list to the selected ID, and it always fails because it doesn't scan all the list's IDs..*当我在测试中运行此代码时,我找到了列表,但它仅将列表的第一个 ID 与所选 ID 进行比较,并且总是失败,因为它没有扫描所有列表的 ID..*

I'm not sure I have all the facts, but if for example this DOM我不确定我是否掌握了所有事实,但是如果例如这个 DOM

<ul>
  <li id="1"></li>
  <li id="2"></li>
  <li id="3"></li>
</ul>

you could test with你可以用

const targetId = '2';

cy.get('ul li')
  .should($els => expect([...$els].some(el => el.id === targetId)).to.eq(true))

to explain,解释,

  • .should() with and expect in the callback will retry until true (or timeout) .should() with and expect 在回调中将重试直到为真(或超时)
  • [...$els].some(...) applies the test inside to every item, returns true if any succeed [...$els].some(...)将内部测试应用于每个项目,如果成功则返回 true

A shorter way would be更短的方法是

cy.get('ul li#' + targetId);

In this selector, only the one with the targetId matches.在此选择器中,只有具有 targetId 的选择器匹配。 If targetId is not there, test fails.如果 targetId 不存在,则测试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 赛普拉斯获取特定路径内的元素 - Cypress get elements within specific path 如何使用 document.querySelector() 查找具有特定 ID 的 div 中具有特定名称 class 的元素 - How to use document.querySelector() to find elements with a specific class name that are within a div with a specific ID jQuery查找并列出特定DIV中UL内的所有LI元素 - jQuery Find and List all LI elements within a UL within a specific DIV 如何扫描类中的所有元素以查找特定的ID,并为不同的ID重复该过程? - How do I scan all elements within a class to find a specific id and repeat the process for different ids? 如何在 Beautiful Soup 和 Selenium 中查找特定 div ID 中的所有元素 - How to find all elements within specific div ID in Beautiful Soup and Selenium 遍历元素以查找特定的ID - Loop through elements to find specific ID 使用 jQuery 在列表中查找 id - Using jQuery to find an id within a list 如何删除特定div ID中的div元素 - How to remove div elements within specific div id 从节点内访问具有 ID 的多个元素,但也具有特定的 class - Access multiple elements with ID from within a node, but also with specific class Typescript禁用特定ID中具有类的所有元素 - Typescript disable all elements within specific ID that has a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM