Alex T
Alex T

Reputation: 3754

Cypress how to get length of text

Lets say I have table which consist of some values. To get the value from specific cell I can use this code to check whether it contains some text:

cy.get('table > tbody > tr:nth-child(1) > td:nth-child(1)', {timeout: 15000}).should('have.text', "Ketchup")

How can I assert/check if that text has minimum length of 5?

I tried using

cy.get('table > tbody > tr:nth-child(1) > td:nth-child(1)', {timeout: 15000}).its('text').should("have.length", 5)
    })

But it does not work.

Upvotes: 6

Views: 7097

Answers (1)

Alapan Das
Alapan Das

Reputation: 18624

You can do something like:

cy.get('table > tbody > tr:nth-child(1) > td:nth-child(1)', {
    timeout: 15000
}).invoke('text').then((text) => {
    expect(text.length).to.be.at.least(5)
})

Upvotes: 9

Related Questions