javascript - 打破柏树中的if循环
问题描述
我们有一个场景如下
iterate over name elements in the page
if(name.text() == expName)
{
name.click()
exit from if loop
}
else
{
createName()
}
我如何在 cypress 中实现这一点,因为 return false 没有从 if 循环中退出。这是我尝试过的代码
export function addProtocol(protocolName, protocolStatus) {
let flag = "false";
subject.view({ name: "ProtocolsExplorerList" }).as('currentView');
cy.get('@currentView').find('td textarea').each($el => {
cy.wrap($el).invoke('val').then($value => {
console.log("valueee:" + $value);
if ($value == protocolName) {
cy.get($el).click();
flag = "true";
return false;
}
})
})
if (flag == "false") {
subject.button({ class: "New" }).click();
subject.field("Name").type(protocolName);
subject.field("Status").setCaption(protocolStatus);
save();
cy.contains(protocolName).click();
}
}
请问有什么建议吗?谢谢。
解决方案
问题是您没有从each
回调中返回false
. 您从then
回调中返回false
. then
回调通常异步运行,这意味着回调在each
迭代完成后执行。
我不熟悉赛普拉斯,但假设他们在他们的集合上实现了迭代器协议,你可以使用:
const textareas = cy.get('@currentView').find('td textarea');
for (const $el of textareas) {
const $value = await cy.wrap($el).invoke('val');
console.log("valueee:" + $value);
if ($value == protocolName) {
cy.get($el).click();
flag = "true";
break;
}
}
您必须将包装功能设为 anasync function
才能使用await
.
推荐阅读
- flutter - 有没有办法在 Flutter 中为自定义绘画制作透明画布?
- selenium-webdriver - 使用谷歌浏览器进行硒自动化测试的浏览器版本控制问题
- javascript - 当我们点击任何按钮(事件)时使用 JavaScript 重置搜索结果
- arrays - JSONArray 无法转换为 JSONObject 错误,当用作类似代码时
- jenkins - Jenkins 组织文件夹扫描未检测到存储库
- c# - 未显示根线时,TreeView 将 +- 号添加到根节点
- java - 在java中读取多个JSON对象
- mysql - Mysql Sum 使用排名(所有玩家所有回合的最佳 x 分数)
- java - Can we map char[] to VARCHAR in Hibernate entity?
- html - 如何创建一个可以使用 gatsby 进行预编译和静态服务的交互式 React Web 应用程序?