在 Cypress 中使用 Each/for
我有一张包含更多信息的卡片,其中包含下一个 html 代码。我想要每张卡,除了第一张卡(因此以某种方式从一开始就从 1 开始增加),按下“编辑位置”按钮,然后为其写入代码和其他操作,然后移动到下一张卡片。
我怎样才能在赛普拉斯中写这个?
我在 Cypress 中使用了这段代码,但没有执行循环,它只在第一张卡上运行一次,然后停止。
cy.get(".card-locations").each(($el, index) => {
if (index > 0) {
cy.wrap($el).contains("Edit location").click({ force: true });
//another code
}
});
HTML 代码:
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 card no-select card-locations">
<!-- card header -->
<div class="card-top-gradient card-top-gradient-noactions animated fadeInDown card-top-gradient-default">
<!-- card content -->
<div class="card-content">
<div class="card-right-content">
<div class="card-name">
Location Craiova
</div>
<div class="card-clinic-name">
Bucharest <br>
-- 020202 Romania
</div>
<ul class="card-details card-details-verbose">
<li>
<span class="field-name">Timezone</span>
<span class="field-value">(UTC+02:00) Athens, Bucharest</span>
</li>
<li>
<span class="field-name">Facility code</span>
<span class="field-value">11 Office</span>
</li>
<li>
<span class="field-name">Phone number</span>
<span class="field-value"></span>
</li>
<li>
<span class="field-name">Email address</span>
<span class="field-value"><a href="mailto:[email protected]">[email protected]</a></span>
</li>
</ul>
</div>
</div>
</div>
<!-- card footer -->
<div class="card-footer" style="background-color: #186b65;">
<div class="button-placeholder">
<div class="card-button card-properties footer-left-button">
<span data-action="edit" class="edit" entity-detail="editEntity" title="Edit Location" onclick="$.cmtBhv().getJson({ url: '/...' }).toggleEntityDetailsFor({ target: this });">Edit location</span>
</div>
<div class="card-button card-properties footer-right-button">
<span data-action="edit" class="edit" entity-detail="editEntity" onclick="$.cmtBhv().getJson({ url: '/...' }).toggleEntityDetailsFor({ target: this });">Rooms</span>
</div>
</div>
</div>
</div>
I have a card with more information that has the next html code. I want each card, except for the first one (so somehow to increase from 1 with the start), to press the "Edit location" button, then to write in the code and other actions for it, and then to move to the next card.
How can I write this in Cypress?
I used this code in Cypress, but no loop is executed, it runs only once on the first card and then stops.
cy.get(".card-locations").each(($el, index) => {
if (index > 0) {
cy.wrap($el).contains("Edit location").click({ force: true });
//another code
}
});
HTML code:
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 card no-select card-locations">
<!-- card header -->
<div class="card-top-gradient card-top-gradient-noactions animated fadeInDown card-top-gradient-default">
<!-- card content -->
<div class="card-content">
<div class="card-right-content">
<div class="card-name">
Location Craiova
</div>
<div class="card-clinic-name">
Bucharest <br>
-- 020202 Romania
</div>
<ul class="card-details card-details-verbose">
<li>
<span class="field-name">Timezone</span>
<span class="field-value">(UTC+02:00) Athens, Bucharest</span>
</li>
<li>
<span class="field-name">Facility code</span>
<span class="field-value">11 Office</span>
</li>
<li>
<span class="field-name">Phone number</span>
<span class="field-value"></span>
</li>
<li>
<span class="field-name">Email address</span>
<span class="field-value"><a href="mailto:[email protected]">[email protected]</a></span>
</li>
</ul>
</div>
</div>
</div>
<!-- card footer -->
<div class="card-footer" style="background-color: #186b65;">
<div class="button-placeholder">
<div class="card-button card-properties footer-left-button">
<span data-action="edit" class="edit" entity-detail="editEntity" title="Edit Location" onclick="$.cmtBhv().getJson({ url: '/...' }).toggleEntityDetailsFor({ target: this });">Edit location</span>
</div>
<div class="card-button card-properties footer-right-button">
<span data-action="edit" class="edit" entity-detail="editEntity" onclick="$.cmtBhv().getJson({ url: '/...' }).toggleEntityDetailsFor({ target: this });">Rooms</span>
</div>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cypress 有一个 .each() 函数,可以生成生成的元素以及索引(以及整个列表,如果需要)。您可以轻松地使用它来跳过第一张卡,然后继续。根据您给定的 HTML,我对您想要的确切元素有点困惑,但根据我的最佳猜测(每个
.field-name
都是您想要的),类似以下内容可以工作。即使我的元素不正确,这个概念仍然存在——使用
.each()
生成元素列表,并使用index
参数排除第一张卡。注意:您必须使用
cy.wrap()
将生成的元素(上例中的$el
)插入回Cypress链。Cypress has a
.each()
function that can yield the yielded element, as well as the index (and entire list, if needed). You can easily use this to skip the first card, and continue on. Based on your given HTML, I'm a little confused about which exact elements you want, but based on my best guess (that each.field-name
is what you want), something like the following could work.The concept carries over even if I have the elements incorrect -- use
.each()
to yield a list of elements, and use theindex
parameter to exclude the first card.Note: you will have to use
cy.wrap()
to insert the yielded element ($el
in the above example) back into the Cypress chain.