如何将文本输入字段值获取为 const 并将该值记录在 Cypress.io 中

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

如何在 Cypress 中将文本输入字段值获取到 'const' 变量,以便我可以使用 cy.log() 记录该变量。下面的代码没有记录任何东西,熟悉的人可以吗Cypress.io请指教

cy.get('input[name="email"]').then(($text)=>{
        const txt = $text.text()
        cy.log(txt)

    })

来自https://github.com/cypress-io/cypress/issues/630

你应该可以做到:

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

这在对以下元素的测试中对我有用:

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

使用 invoke('val') 而不是 invoke('text') 对我的情况有效。

html 标签提醒

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

赛普拉斯代码

cy.get('input[name="email"]')
  .invoke('val')
  .then(sometext => cy.log(sometext));

.contains('your-value') 为我工作

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)

如果您想在断言之前修改或处理文本:

cy.get('input').should(($input) => {
  const val = $input.val()
})

using-cypress-faq