'extract integer value from webelement and compare again using cypress

enter image description here

How to extract 3 from this and compare it with same if this integer value changes 3



Solution 1:[1]

Easy ways to extract element text and compare it with expected value are:

cy.get(locatorOfElement).invoke("text").then((value) => {
    expect(value).to.equal("3");
});
cy.get(locatorOfElement).invoke("text").should("be.eq", "3");

and if you want to compare it with integer value then you have to convert the extracted value to an integer and then compare it to an expected integer value.

cy.get(locatorOfElement).invoke("text").then((value) => {
    expect(parseInt(value)).to.equal(3);
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krishna