Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress, how to check for text typed into a field?

Tags:

cypress

I can verify text appears "somewhere" on the results page with

it.only('can verify an input element has certain text typed into it', function() {
  cy.visit('http://google.com')
  cy.get("input[name=q]").type('abc123{enter}')  // with or without the {enter}
  cy.contains('abc123') // Anywhere on the page :(
})

but how can I verify the text I type in the input text box?

I tried chaining to the element with

it.only('can verify an input element has certain text typed into it', function() {
  cy.visit('http://google.com')
  cy.get("input[name=q]").type('abc123{enter}')
  cy.get("input[name=q]").contains('abc123')
})

but I get

CypressError: Timed out retrying: Expected to find content: 'abc123' within the element: <input.gLFyf.gsfi> but never did.

I tried cy.get("input[name=q]").contains('abc123') and
cy.contains('input[name=q]', 'abc123')
but both time out and fail.

like image 622
Michael Durrant Avatar asked Dec 14 '22 09:12

Michael Durrant


1 Answers

Change .contains to use .should('have.value'...

cy.get("input[name=q]").type('abc123{enter}')
cy.get("input[name=q]").should('have.value', 'abc123')
like image 116
Michael Durrant Avatar answered Dec 31 '22 13:12

Michael Durrant