Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 568
learningruby347 Avatar asked Sep 18 '25 17:09

learningruby347


1 Answers

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);
        });
    });
like image 129
Filip Kwiatkowski Avatar answered Sep 21 '25 08:09

Filip Kwiatkowski