【问题标题】:How to get the value from other method/function in JavaScript如何从 JavaScript 中的其他方法/函数获取值
【发布时间】:2021-10-06 09:24:01
【问题描述】:

我是 Cypress 和 JavaScript 的新手,请帮助我了解更多信息

我必须使用 cypress 自动化网站以验证 EMI 值。

下面是我的 .js 代码,我需要在其中实现 EMI 计算器逻辑并需要将其传递给另一个方法(cypress 中的“it”块)。 'it' 块将从网站获取值,并应使用我在代码 EMILogic() 方法中返回的 EMI 逻辑进行验证。

EMILogic() 将返回一个值 (emi),我在 cy.get('#emi').should('have.text', EMIdata.emi) 行中传递该值但是在执行代码时,我在 cypress 中遇到断言错误,因为 Timed out retrying after 4000ms: expected '' to have text undefined, but the text is '925.11'

describe('EMI Calculation', function () {

 class abc{
 EMILogic() {

    const amount = 20000;
    const rate = 5;
    const month = 2;
let emi;
rate = rate/(12*100);
period = period*12;

emi = (amount*rate*Math.pow(1+rate, month))/(Math.pow(1+rate, month)-1);
return emi

   }
   }

下面是我需要从上面的类中传递 emi 值的代码

    it('EMI calculator', function () {
    const EMIdata = new abc();

    cy.visit('emi-calculator.html')
    cy.get('.MR15').click()
    cy.get('#emi').should('have.text', EMIdata.emi)

   })
   })

【问题讨论】:

    标签: javascript object cypress method-call


    【解决方案1】:

    问题:

    const EMIdata = new abc();
    

    这行代码表示 EMIdata 是 abc 类的对象。在类 abc 上没有名为 emi 的成员变量。因此,您在使用 EMIdata.emi 时会得到一个未定义的结果。

    解决方案:

    作为您的成员函数 EMILogic 返回 emi 值。代替EMIdata.emi,您可以在您的应该函数中使用EMIdata.EMILogic() 调用EMILogic。

    cy.get('#emi').should('have.text', EMIdata.EMILogic())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2020-04-26
      • 2022-11-08
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多