Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text input field value to a const and log that value in Cypress.io

How to get the text input field value to a 'const' variable in Cypress, so that I can log that variable using cy.log(). The below code doesn't log anything, can someone familiar with Cypress.io please advise

cy.get('input[name="email"]').then(($text)=>{         const txt = $text.text()         cy.log(txt)      }) 
like image 644
soccerway Avatar asked Aug 10 '18 20:08

soccerway


People also ask

How do I get an element's text contents in Cypress?

How do I get an element's text contents? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity   then use the Unicode character \u00a0 instead of   . Tip: watch the Confirming the text with non breaking space entity video.

How do you fetch text in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.

How do you get the value of a text field?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.

How do you value a field in Cypress?

You can check a value of an input element using the built-in Chai-jQuery assertion "have. value".


2 Answers

Using invoke('val') instead of invoke('text') worked for my case.

Reminder of the html tag

<input type="text" class="form-control" name="email"> 

Cypress code

cy.get('input[name="email"]')   .invoke('val')   .then(sometext => cy.log(sometext)); 
like image 57
soccerway Avatar answered Sep 22 '22 09:09

soccerway


Cypress official solution How do I get an input’s value? suggests something like this code below:

cy.get('input[name="email"]').should('have.value', val)
like image 31
Gerard Avatar answered Sep 22 '22 09:09

Gerard