Cypress,如何检查字段中键入的文本?

Cypress,如何检查字段中键入的文本?,cypress,Cypress,我可以验证文本是否显示在结果页面上的“某处” it.only('can verify an input element has certain text typed into it', function() { cy.visit('http://google.com') cy.get("input[name=q]").type('abc123{enter}') // with or without the {enter} cy.contains('abc123') // Anywh

我可以验证文本是否显示在结果页面上的“某处”

it.only('can verify an input element has certain text typed into it', function() {
  cy.visit('http://google.com')
  cy.get("input[name=q]").type('abc123{enter}')  // with or without the {enter}
  cy.contains('abc123') // Anywhere on the page :(
})  
但是如何验证我在输入文本框中键入的文本

我试着用

it.only('can verify an input element has certain text typed into it', function() {
  cy.visit('http://google.com')
  cy.get("input[name=q]").type('abc123{enter}')
  cy.get("input[name=q]").contains('abc123')
})  
但我明白了

CypressError: Timed out retrying: Expected to find content: 'abc123' within the element: <input.gLFyf.gsfi> but never did.
CypressError:超时重试:应在元素中找到内容:“abc123”:但从未找到。
我尝试了
cy.get(“input[name=q]”)。包含('abc123')

cy.contains('input[name=q],'abc123')


但是超时和失败。

更改
.contains
以使用
。应该('have.value'…

cy.get("input[name=q]").type('abc123{enter}')
cy.get("input[name=q]").should('have.value', 'abc123')

您可能不喜欢这个想法,但这里只是一个建议,这样您就不必每次都打电话给
cy.get

您可以始终为输入名称设置常量值(可以在外部文件中),以便:

无论何时调用inputField,这都将执行get

那么你的电话是:

inputField.type('abc123{enter}').should('have.value', 'abc123');

这与其说是一个实际的解决方案,不如说是一个设置,因为我知道你自己解决了这个问题,但是上面的方法很好,所以你不必一直在同一个字段上执行cy.get。

也许我们可以在一行中编写该测试,并断言
cy.get(“input[name=q]”)。type('abc123{enter}')。应该('have.value','abc123'))
@soccerway视情况而定,因为他正在键入
{enter}
最后,他可能发布了可能触发页面重新加载的表单。不确定Cypress是否有时间做出断言。但是,如果不是这样,正如Cypress所示:你知道这里发生了什么吗?我很好奇,下面的答案是指我在上面链接到的关于
^
$在已删除的Vim中定位。
inputField.type('abc123{enter}').should('have.value', 'abc123');