javascript 如何将文本输入字段值设为常量并在 Cypress.io 中记录该值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51793521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:41:59  来源:igfitidea点击:

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

javascriptcypress

提问by soccerway

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

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

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

    })

回答by soccerway

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

使用invoke('val')而不是invoke('text')为我的案例工作。

Reminder of the html tag

html标签的提醒

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

Cypress code

赛普拉斯代码

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

回答by Brendan

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

来自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>

回答by Yuqiu G.

.contains('your-value')worked for me

.contains('your-value')对我来说有效