Cypress throws an assertion error but the result is correct.
Error message:
expected 564792 to equal '564792'
I have no idea whats wrong with it?!
Does someone has experince with this issue?
Code:
// global variables
var value1 = 'global value'
var value2 = 'global value'
var value3 = 'global value'
The value for the global variables I get for instance like this:
cy.get('.table1 > tbody > tr.table_row3 > td:nth-child(1)').each(($e1, index, $list) => {
var titletext=$e1.text()
if(titletext.includes('Some Headline Text')) {
cy.get('.table1 > tbody > tr.table_row3 > td:nth-child(14)').eq(index).then(function(amount) {
value1 = amount.text().trim()
})
}
})
This code is the part with the assertion error message:
cy.get('#specific_id').then(function(calculation){
var valueOfCalculation = value1 - value2
expect (valueOfCalculation).to.equal(value3)
})
With this code above I get the error message, for example:
AssertionError
expected 564792 to equal '564792'
In other cases with assertions like in this example below the comparison works:
expect (value4).to.equal(23040)
Any idea what could cause this error?
Thank you very much
Solution 1: Convert valueOfCalculation to a string
cy.get('#specific_id').then(function (calculation) {
var valueOfCalculation = value1 - value2;
// Convert the numeric value to a string before comparison
expect(valueOfCalculation.toString()).to.equal(value3);
});
Solution 2: Convert value3 to a number
cy.get('#specific_id').then(function (calculation) {
var valueOfCalculation = value1 - value2;
// Convert the string value to a number before comparison
expect(valueOfCalculation).to.equal(Number(value3));
});