Cypress how to get length of text

Viewed 3048

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.

1 Answers

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)
})
Related