Value not be empty workaround in cypress

Viewed 1287

Please cypress is giving me undefined error when I want to check that the input is not empty like this:

cy.get('car').find('input').should('not.be.empty');

When I put a value into input like bmw and use a code like this, all is fine

cy.get('car').find('input').should('have.value', 'bmw');

So it finds the input and can read the value, but why the not.be.empty is not working? Is there some workaround to that? I do not want to specific the value of the field, could be random. Thx

2 Answers

I assume you want to check whether your value is empty or not. You can do something like this:

cy.get('car').find('input').invoke('val').should('not.be.empty')

An empty input will have a string value of "".

cy.get('car').find('input').should('not.have.value', '');

The assertion .should('not.be.empty') applies to the text content e.g <div>MyText</div>.

Related