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

Mic*_*ant 7 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') // Anywhere on the page :(
})
Run Code Online (Sandbox Code Playgroud)

但如何验证我在输入文本框中输入的文本?

我尝试链接到元素

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')
})
Run Code Online (Sandbox Code Playgroud)

但我得到

CypressError: Timed out retrying: Expected to find content: 'abc123' within the element: <input.gLFyf.gsfi> but never did.
Run Code Online (Sandbox Code Playgroud)

我尝试过cy.get("input[name=q]").contains('abc123')
cy.contains('input[name=q]', 'abc123')
但都超时并失败。

Mic*_*ant 18

更改.contains使用.should('have.value'...

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

  • 也许我们可以在一行中编写该测试并断言 `cy.get("input[name=q]").type('abc123{enter}').should('have.value', 'abc123') ` (2认同)
  • @soccerway 取决于,因为他在最后输入“{enter}”,所以他可能会发布可以触发页面重新加载的表单。如果发生这种情况,不确定赛普拉斯是否有时间做出断言。但除此之外是的,如 Cypress 所示:https://example.cypress.io/commands/actions (2认同)