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