英文:
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) 处
在
从先前的事件中:
在 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').
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('get', function (originalFn, ...args) {
return originalFn.apply(this, args)
})
but if instead you use
Cypress.Commands.overwriteQuery('get', (originalFn, ...args) => { // 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 (() => {}
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论