admin管理员组文章数量:1389772
So, I have the elements in the following structure:
<div class="MuiBox-root" data-testid="all-events">
<ul ....>
<div class="MuiBox-root" data-testid="event">
<div class="MuiBox-root" data-testid="event">
<div class="MuiBox-root" data-testid="event">
So, I have the elements in the following structure:
<div class="MuiBox-root" data-testid="all-events">
<ul ....>
<div class="MuiBox-root" data-testid="event">
<div class="MuiBox-root" data-testid="event">
<div class="MuiBox-root" data-testid="event">
Now, within each event element is the following structure:
<div class="MuiBox-root" data-testid="event">
<li .....>
<div class="MuiBox-root ....." data-testid="event-status">
<div> Text Value </div>
<div class="MuiBox-root ....." data-testid="event-name">
So, what I want to test is that if the element event-status is say pleted or active, then the element event-name will be present. So the approach that I am using is as follows:
cy.get("[data-testid='events']").its("length").then((numEvents) => {
for (let i = 0; i < numEvents; i++) {
cy.get("[data-testid='event-status']").each((status, index) => {
if (index === i) {
if (isEventActive(status)) {
cy.get("[data-testid='event-name']").each((name, index) => {
if (index === i) {
cy.get(name).should("have.text", "some value");
}
});
} else {
cy.get("[data-testid='event-name']").each((name, index) => {
if (index === i) {
cy.get(name).should("not.exist");
}
});
}
}
});
}
});
The above code is working to test what I want to test out but it is incredibly messy and any simpler approaches are wele.
Share Improve this question edited Jan 1, 2021 at 9:55 Sayantan Ghosh asked Dec 24, 2020 at 14:56 Sayantan GhoshSayantan Ghosh 3381 gold badge8 silver badges26 bronze badges 1-
You can replace that
index === 0
check with something more flexible likeif (event-status === 'condition')
– Joshua Commented Dec 25, 2020 at 9:15
1 Answer
Reset to default 5 +50Convert the for loop to an .each()
. Using .within()
on the event will allow internal get
mands to be specific to the event, and eliminate the need to check index values.
cy.get("[data-testid='events']").each($event=> {
cy.wrap($event).within(() => {
cy.get("[data-testid='event-status']").each($status => {
if (isEventActive($status)) {
cy.get("[data-testid='event-name']").should("have.text", "some value");
}) else {
cy.get("[data-testid='event-name']").should("not.exist")
})
})
})
});
Maybe this will also work. Uses closure to get the two inner elements ($status and $name) into a ternary expression, and uses .should("satisfy", callbackFn)
to do an either/or check.
cy.get("[data-testid='events']").each($event => {
cy.wrap($event).find("[data-testid='event-status']").then($status => {
cy.wrap($event).find("[data-testid='event-name']")
.should("satisfy", $name => isEventActive($status)
? $name.text() === "some value"
: $name.length === 0;
);
})
})
});
版权声明:本文标题:javascript - How to iterate over and test different child elements within an element in cypress? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744730198a2622002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论