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

一世执手 提交于 2019-12-21 09:35:16

问题


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)

    })

回答1:


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));



回答2:


From https://github.com/cypress-io/cypress/issues/630

You should be able to do:

cy
  .get('input[name="email"]')
  .invoke('text')  // for input or textarea, .invoke('val')
  .then(text => {
    const someText = text;
    cy.log(someText);
  });

This is working for me in a test on the following element:

<span class="abProgress" style="width: 0%;">100%</span>


来源:https://stackoverflow.com/questions/51793521/how-to-get-the-text-input-field-value-to-a-const-and-log-that-value-in-cypress-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!