javascript - 我如何保存 div 文本以便在其他地方使用?

标签 javascript testing cypress end-to-end

我需要保存 div 文本值并在它断言后在另一个地方?

cy.get('div').should(($div) => {
    const myText = $div.innerText
})
cy.get('.value').should('have.text', myText)

最佳答案

Cypress 是异步的。这意味着如果你做这样的事情:

cy.get('div').should(($div) => {
  const myText = $div.innerText
})
cy.get('.value').should('have.text', myText)

...你的测试总是会失败。 const myText = $div.innerText 将在 .should('have.text', myText) 被评估后执行。

如果你想以这种方式使用myText,只需使用cy.get()返回的Promise即可。 :

cy.get('div')
.then($div => {
  const myText = $div.text()
  // write the rest of your test here
  cy.get('.value').should('have.text', myText)
})

关于javascript - 我如何保存 div 文本以便在其他地方使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56396152/

相关文章:

cypress - 如何在 Cypress 中的自定义命令中记录上一个主题

javascript - 在 Cypress 中通过 HTML 查找元素

javascript - 如何使用 WebExtensions 存储 API 恢复复选框的状态

javascript - 如何在 Meteor 中向 Google map 添加新标记?

PhpSpec - 检查方法返回一个整数而不是 "willReturn(x)"

node.js - 如何强制 Jest 在根 lvl __snapshots__ 文件夹中输出快照?

typescript - 使用声明文件覆盖 Cypress typescript

javascript - App Engine Channel API 的 Javascript 客户端没有使用我的 onError 回调

javascript - 在 Node :7 image; libgs. 中安装 ghostscript4js 所以找不到

c# - 应该在没有 TestMethod 属性的方法中使用 Assert 吗?