Cypress 12.14 TypeError: 无法读取未定义的属性(读取 ‘set’)

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

Cypress 12.14 TypeError: Cannot read properties of undefined (reading 'set')

问题

遇到了一个非常奇怪的问题,Cypress无法“获取”任何元素。所有我的命令都可以正常工作,直到代码到达cy.get。

我的代码:

cy.get('#username').type('blueUser');

错误:

无法读取未定义的属性读取 'set')。

这真的让人困惑,因为我知道该元素存在,因为我使用了较旧版本的Cypress,它运行得很好。我甚至尝试了Cypress Studio(录制和回放),它创建了与我的代码相同的代码,但仍然抛出相同的错误。请帮忙!

堆栈跟踪粘贴在评论中:

在获取 (xxxxxxxxxx.com/__cypress/runner/cypress_runner.js:148433:10) 处
在 $Command.overridden (xxxxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159898:21) 处
在 cyFn (xxxxxxxxxxx/__cypress/runner/cypress_runner.js:160714:19) 处
(xxxxxxxxxxx/__cypress/runner/cypress_runner.js:159444:15) 处

从先前的事件中:
在 CommandQueue.runCommand (xxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159410:8) 处
在 next (xxxxxxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159610:19) 处
在 next (xxxxxxxxxxxxxxxx/__cypr

英文:

I've encountered a very strange problem with Cypress where its unable to 'get' any element. All my commands work until the code gets to cy.get.

My code:

cy.get('#username').type('blueUser');

Error:

Cannot read properties of undefined (reading 'set').

Cypress 12.14 TypeError: 无法读取未定义的属性(读取 ‘set’)

This is really puzzling because I know the element exists because I used older versions of Cypress and it worked just fine. I even tried the Cypress Studio (Record and Playback) and it created identical code to my own but still threw that same error. Help please !

Stack trace pasted into comments:

at get (xxxxxxxxxx.com/__cypress/runner/cypress_runner.js:148433:10)
at $Command.overridden (xxxxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159898:21)
at cyFn (xxxxxxxxxxx/__cypress/runner/cypress_runner.js:160714:19)
at <unknown> (xxxxxxxxxxx/__cypress/runner/cypress_runner.js:159444:15)

From previous event:
at CommandQueue.runCommand (xxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159410:8)
at next (xxxxxxxxxxxxxxxx/__cypress/runner/cypress_runner.js:159610:19)
at next (xxxxxxxxxxxxxxxx/__cypr

答案1

得分: 3

感谢添加堆栈跟踪信息。如评论中所提到的,堆栈指示正在使用Cypress.Commands.overwriteQuery(),但使用了错误形式的回调函数。

文档自定义查询显示

Cypress.Commands.overwriteQuery('get', function (originalFn, ...args) {
  return originalFn.apply(this, args)
})

但如果您改用以下形式

Cypress.Commands.overwriteQuery('get', (originalFn, ...args) => {  // 箭头函数
  return originalFn.apply(this, args)
})

会导致由于this未定义而引发错误。

文档中提供了警告:

查询API依赖于this来设置超时,这意味着callbackFn应始终使用function () {}而不是箭头函数() => {}

英文:

Thanks for adding the stack trace. As mentioned in comments, the stack is indicating a Cypress.Commands.overwriteQuery() is being used which has used the wrong form of callback.

The docs Custom Queries show

Cypress.Commands.overwriteQuery(&#39;get&#39;, function (originalFn, ...args) {
  return originalFn.apply(this, args)
})

but if instead you use

Cypress.Commands.overwriteQuery(&#39;get&#39;, (originalFn, ...args) =&gt; {  // arrow function
  return originalFn.apply(this, args)
})

you get errors caused by this being undefined.

The warning is given in the docs:

> The query API relies on this to set timeouts, which means that callbackFn should always use function () {} and not be an arrow function (() =&gt; {}).

huangapple
  • 本文由 发表于 2023年6月16日 05:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485602.html
匿名

发表评论

匿名网友

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

确定