Home > Blockchain >  How to get the value from other method/function in JavaScript
How to get the value from other method/function in JavaScript

Time:10-07

I am new to Cypress and JavaScript, Please help me with this query to understand more

I have to automate a website using cypress to validate the EMI value.

Below is my .js code where I need to implement the EMI calculator logic and need to pass it to another method ('it' block in cypress). 'it' block will get the value from the website and should validate with the EMI logic which I return in code EMILogic() method.

EMILogic() will return a value (emi) and I am passing this value in line cy.get('#emi').should('have.text', EMIdata.emi) but while executing the code I am getting assertion error in cypress as Timed out retrying after 4000ms: expected '<span#emi>' to have text undefined, but the text was '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

   }
   }

Below is the code where I need to pass the value of emi from above class

    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)

   })
   })

CodePudding user response:

Problem:

const EMIdata = new abc();

This line of code means EMIdata is an object of class abc. there is no member variable named emi on class abc. hence you are getting an undefined when using EMIdata.emi.

Solution:

As your member function EMILogic returns the emi value. Instead of EMIdata.emi you can call EMILogic using EMIdata.EMILogic() inside your should function.

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