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) })
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.
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.
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.
You can check a value of an input element using the built-in Chai-jQuery assertion "have. value".
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));
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With