I am writing a test in Cypress where I enter two values into two separate inputs and then compare them by clicking a button. If the button displays “=”, I need to reset and reenter two new values until I get something other than “=”. It could be “+” or “-“.
I created a function called checkResult()
that uses an if statement to check if the conditional is true (text === “=”). If it is true, it then calls another function called reEnterInputs()
, which resets the value of the inputs and enters two new values.
The issue I am having is that I thought that by using this.reEnterInputs() in the if statement, it would rerun itself until I got a value other than “=”. It runs once and it stops. Can someone help?
describe('template spec', () => {
const numberArray = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const topInput = Math.floor(Math.random() * numberArray.length).toString();
const bottomInput = Math.floor(Math.random() * numberArray.length).toString();
it('Check values inside html element', () => {
cy.visit('/');
//Fills out one input
cy.get('#top').type(topInput);
//Fills out another input
cy.get('#bottom').type(bottomInput);
//clicks button to compare
cy.get('#compare').click();
//Runs this function
checkResult();
});
});
**Functions***
//This function checks the result of the values in the top and bottom inputs
public checkResult() {
cy.get('#value')
.invoke('text')
.then((text) => {
if (text === '=') {
this.reEnterInputs();
//if the "=" appears again after running reEnterInputs(), I need reEnterInputs() to run again until a value different than "=" is given
} else {
cy.log('B');
}
});
}
//This function runs in the if statment
public reEnterInputs() {
let numArr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
let topNumber = Math.floor(Math.random() * numArr.length).toString();
let bottomNumber = Math.floor(Math.random() * numArr.length).toString();
//Clicks the reset button
cy.get('#resetValues').click();
//reenters the number in the inputs
cy.get('#top').type(topNumber);
cy.get('#bottom').type(bottomNumber);
//checks value of inputs
cy.get('#compare').click();
}