首页 > 解决方案 > TypeScript 和 Cypress 无法在自定义 cypress 命令中返回 true/false

问题描述

Cypress.Commands.add('checkIfTenantExist', (tenantName: string) => {
  let result: boolean = false;
  getTenant()
    .each(($tenant) => {
     if ($tenant.text() === tenantName){
       result = true;
     }
    })
    .then(() => {
      return result;
    });
});

我使用 cypress 和 typescript 编写测试。我想添加返回 bool 的自定义 cypress 命令。即使在 if 块中更改了它,它也总是返回 false。你能帮我解决这个问题吗?

标签: typescriptcypress

解决方案


它工作正常,假设getTenant()返回一个或多个元素(它应该考虑你跟随它.each())。

但是您使用自定义命令的方式可能不正确。

它应该与一个.then()

cy.checkIfTenantExist('something').then(exists => {
  // further tests here
}) 

或与.should()

cy.checkIfTenantExist('something').should('eq', true) 

推荐阅读