javascript - 如何使用.then() 之外的.then() 内设置的var 值?

标签 javascript testing cypress

我想在外部范围内使用 .then() 中设置的 var 值

我试过了

let numOfDropdownFields = 0;
cy.get("dropdown")
  .find("button")
  .its("length")
  .as("len")
  .then(($len) => {
    cy.log("No. of buttons:" + $len);
    numOfDropdownFields = $len;
    cy.get("@len").should("eq", numOfDropdownFields);
    cy.log(numOfDropdownFields);
  });
cy.log(numOfDropdownFields);

第一个日志打印: 元素数量:14

第二个日志:14

第三个​​日志:0

如何在 .then() 回调之外看到值 14?

最佳答案

您要实现的目标被认为是 Cypress 的反模式。通常,您应该在 .then() 中使用该变量做任何您想做的事情。 确保使用如下语法:.then() => {all your code in here}

查看此处以获取引用:https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Aliases

您也可以像使用 .as('len') 一样使用别名。

关于javascript - 如何使用.then() 之外的.then() 内设置的var 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66471070/

相关文章:

javascript - Cypress 如何检查 div 中的背景是否发生变化

javascript - 我们应该告诉 browsersync 重新加载特定的资源类型吗?

php - JSONP 与 Laravel

javascript - html页面中元素的z-index定位问题

Spring Framework 测试 RESTful Web 服务( Controller )离线,即无服务器,无数据库

typescript - Cypress 没有看到自定义 cy。命令

javascript - 如何在 Cypress 中测试以某种形状的对象作为参数调用的 stub 函数?

javascript - 需要具有自定义轴位置的多面可视化

testing - Meteorjs 测试 Meteor 方法

android - 使用 SugarORM 和 Robolectric 进行测试