Maksim Nesterenko
Maksim Nesterenko

Reputation: 6213

How to assert that input value is truthy with cypress

I have some input

<input />

How to assert that input has truthy value?

I think it's possible with

cy('input').should('not.have.value', '')

but I think it's slightly unreliable. Or maybe not. But anyway, would be nice to know some other way to check for truthy value.

Upvotes: 13

Views: 19553

Answers (3)

Peter
Peter

Reputation: 630

Something like this would also work:

cy.get('input')
  .invoke('val')
  .then(value => assert.isTrue(!!value));

Upvotes: 1

Maksim Nesterenko
Maksim Nesterenko

Reputation: 6213

Just found that invoke thing, so it's possible to do so:

cy.get('input').invoke('val').should("be.ok");
cy.get('input').invoke('val').should('not.be.empty') // works in the same way.

invoke is the way to call functions (jquery command in that case).

Upvotes: 42

Mr. J.
Mr. J.

Reputation: 3741

there are multiple solutions, you can also use:

cy.('input').should('not.be.empty')

It's more robuust than indeed checking if the value is not an empty string.

Upvotes: -2

Related Questions