Cypress的`invoke(“text”)`为什么会卡住并失败?

huangapple go评论55阅读模式
英文:

Why does Cypress’s `invoke("text")` hang and fail?

问题

我正在使用 Cypress 12.17.1。我的测试运行如下:cy.contains("Mentions légales").invoke("text").should("eq", "Mentions légales")(愚蠢的示例,我知道)。

在 Cypress 中,我可以看到已找到 HTML 元素。但接下来的 .invoke("text") 似乎无法工作。Cypress 在这个指令上等待了几秒钟并返回了一个错误:

TypeError: Cannot set property message of [object DOMException] which has only a getter.

我无法看到与文档或示例(示例链接)中的示例有任何区别。

这似乎不是特定于 text 的问题,任何其他我尝试的方法都以相同的方式失败。我还尝试了使用 get 来定位一个元素,但没有成功。我还尝试了许多其他语法,如:

.should(($div) => {
    const text = $div.text()
    expect(text).to.match(/.*/)
})

.should(($div) => {
    expect($div.get(0).innerText).to.eq('foobarbaz')
})

测试从一个简单的 cy.visit(…) 开始,我还尝试在其后添加了 cy.wait(1000)

英文:

I’m using Cypress 12.17.1. My test runs cy.contains("Mentions légales").invoke("text").should("eq", "Mentions légales"). (silly example, I know)

In Cypress, I can see that the HTML element has been found. But the .invoke("text") coming next doesn’t seem to work. Cypress waits a few seconds on this instruction and returns an error:

> TypeError: Cannot set property message of [object DOMException] which has only a getter.

I can’t see a difference compared to the examples in the docs or recipes (example).

It’s not specific to text it seems, any other method I tried failed in the same way. I also used get to target an element, no luck. I’ve also tried many other syntaxes like:

  .should(($div) => {
     const text = $div.text()
     expect(text).to.match(/.*/)
  })

  .should(($div) => {
     expect($div.get(0).innerText).to.eq('foobarbaz')
  })

The test begins with a simple cy.visit(…) and I also tried following it with cy.wait(1000).

答案1

得分: 0

我注意到这只在我正在开发的网站上发生,无论是在我的本地开发环境还是在实际网站上都是如此。我开始禁用脚本,很快就注意到这个指令是问题的根本原因:

const $ = document.querySelector.bind(document);

只需移除它,问题就解决了。

我以为 jQuery 在 Cypress 中以 Cypress.$ 的形式暴露,以防止出现这种冲突,但显然它不能阻止这种情况发生。幸运的是,我并没有过多使用这个别名,所以移除它很容易。

英文:

I noticed this was happening only on the website I was working on, both in my local dev environment and on the live website. I started disabling scripts and quickly noticed that this instruction here was the cause of the issue:

const $ = document.querySelector.bind(document);

Just removing it fixed the whole thing.

I thought jQuery was exposed in Cypress as Cypress.$ to prevent conflicts like this, but apparently it doesn’t prevent this one from happening. Luckily I wasn’t using this alias too much, so removing it was easy.

huangapple
  • 本文由 发表于 2023年7月12日 20:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定