Comparing Text Values in Cypress

I'm trying to compare two values on a page to make an assertion. I want to capture the value of one text element and compare it with another value on the same page. I'm not sure how to do that in javascript. In Java/selenium this is easy but cypress seems less flexible on this..

3

1 Answer

This is a test comparing two generated strings and if they are not equal after clicking a button:

    it('Test generating new password', () => {
        let password1;
        cy.get('#password').should(($div) => {
            password1 = $div.text();
        });
        cy.get('#generate-button').click();
        cy.get('#password').should(($div) => {
            const password2 = $div.text();
            expect(password1).not.equal(password2);
        });
    });
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest