英文:
Can we have a callback after each in cypress?
问题
cy.get(someselector).each(($el) => {
cy.wrap($el).invoke('text').then((text) => {
if(text){} else {}
// 执行某些操作
});
});
cy.get(someselector).click(); // 这个是在每个块执行之前调用的
我们可以在每个块执行后执行某些操作吗?尝试使用waitUntil,但没有成功。后来将cy.get(someselector).click();移到了不同的测试中,以解决这个问题。
但是否有一种方法可以处理这种情况?
英文:
cy.get(someselector).each(($el) => {
cy.wrap($el).invoke('text').then((text) => {
if(text){} else {}
// Do something
});
});
cy.get(someselector).click(); // this is getting called before each block executes
Can we do something after each block executed only. Tried with waitUntil, was not successful. Later moved cy.get(someselector).click(); to different test itself to get rid off this issue.
But is there a way to handle this scenario?
答案1
得分: 2
你所提出的问题,很可能不太可能发生 -
// 这在每个块执行之前调用
这是因为Cypress使用了队列系统,像.each()
和.click()
这样的命令必须按照它们在队列中的顺序发生,这就是你在测试中放置它们的顺序。
有可能.each()
回调体内的某些结构会导致执行时有延迟,从而欺骗了队列系统。这就是为什么Cypress有时会执行一些检查,导致消息 -
"错误:在命令内返回了一个Promise"。
但你没有展示完整的代码,所以无法为你诊断。
嗯,我可以这么说 - 增加另一层回调,一种手动队列系统:
cy.get(someselector).each(($el) => {
})
.then(() => {
cy.get(someselector).click()
})
但这不太可能对你有帮助,因为我认为你最初对问题的诊断是错误的。
顺便说一下,你对文本的处理也不好。如果可能的话,你应该尝试将所有代码都保持在.each()
回调中同步(意思是不要使用不必要的.wrap()
命令)。
const text = $el.text()
if (text !== '') {
英文:
What you have asked, it is highly unlikely -
> // this is getting called before each block executes
This is because Cypress uses a queue system and the commands like .each()
and .click()
must happen in the order they are placed in the queue, which is the order you place them in the test.
It is possible some construct inside the body of .each()
callback causes some delay in execution that fools the queue system. That is why Cypress performs sometimes checks that result in message -
> "Error: you returned a promise within a command".
But you have not shown the full codes, so it is not possible to diagnose that for you.
Well, I can say this - add another layer of callback, sort of manual queue system:
cy.get(someselector).each(($el) => {
})
.then(() => {
cy.get(someselector).click()
})
But is unlikely to help you since I think you misdiagnose the problem from the start.
By the way, your handling of text is not good either. You should attempt to keep all code within the .each()
callback synchronous if possible (meaning do not use un-needed .wrap()
command).
const text = $el.text()
if (text !== '') {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论