无法使用TypeScrip在Cypress的每个循环内返回值.patientBalance值未定义.

主类代码

let patientBalance: number = getPatientBalance(claimNumber);

功能如下:

let amountInDollar1: string[];
let amount1: number;
export let getPatientBalance = (claimNumber: string) => {
  getClaimNumberCells().each((input, index) => {
    const claimText = input.text();
    getChargesTable().scrollTo('right');
    if (claimText.includes(claimNumber)) {
      getPatientBalanceCell()
        .eq(index)
        .each((data) => {
          amountInDollar1 = data.text().split('$');
          amount1 = parseFloat(amountInDollar[1]);
          console.log('Amount in loop', +amount1);
        });
    }
  });
  return amount1;
};

此外,我想在整个测试过程中在多个点上使用这patientBalance个.如何将此值赋给变量?

推荐答案

.each()命令是异步的,因此您不能像您try 的那样同步返回.

我认为增加大约.then()个电话应该可以做到你想做的事情:

export let getPatientBalance = (claimNumber: string) => {

  getClaimNumberCells().each((input, index) => {
    // calculate
  })

  return cy.then(() => {  // returning the cy.then() ensures async completed

    // option 1 - put value in an alias for multiple use in same test
    cy.wrap(amount1).as('oldPatientBalance')  // an alias stores the value 

    // option 2 - put value in an env variable for use across multiple tests
    Cypress.env('oldPatientBalance', amount1)

    // option 3 - return the value directly 
    return amount1                            // or direct return of value
  })
})

// In test(s) 

// option 3 -  use the returned value
getClaimNumberCells().then(amount1 => {   
  // use it here
})

// option 1 - take the value from the alias later in the same test
cy.get('@oldPatientBalance').then(oldPatientBalance => {
  console.log('oldPatientBalance', oldPatientBalance);
}

// option 2 - should be accessible in other tests (not same test)
it('another test for where amount is calculated', () => {
  const oldPatientBalance = Cypress.env('oldPatientBalance')
})

Typescript相关问答推荐

如何让这个函数正确推断返回类型?

替换类型脚本类型中的多个特定字符串

TypScript在对象上强制执行值类型时推断对象文字类型?

更新:Typescript实用程序函数合并对象键路径

带有微前端的动态React路由问题

如何防止TypeScript允许重新分配NTFS?

如何使用泛型类型访问对象

react 路由6操作未订阅从react 挂钩表单提交+也不使用RTK查询Mutations

带占位符的模板文字类型何时扩展另一个?

在数据加载后,如何使用NgFor在使用异步管道的可观测对象上生成子组件?

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

如何连接属性名称和值?

编剧- Select 下拉框

来自枚举的<;复选框和组件列表

完全在类型系统中构建的东西意味着什么?

Select 类型的子项

类型分布在第一个泛型上,但不分布在第二个泛型上

定义一个只允许来自另一个类型的特定字符串文字的类型的最佳方式

Typescript 和 Rust 中跨平台的位移数字不一致

如何在由函数参数推断的记录类型中具有多个对象字段类型