我正在开发一个诊断应用程序,它可以向用户询问大量问题(100 多个)。我一直在使用递归函数来处理问题及其答案,但我注意到随着时间的推移,测试会变得缓慢,经过大量研究,我开始意识到这可能是递归在做。任何帮助将不胜感激。
目前的解决方案是这样的:
Cypress.Commands.add('intro_Questions', (data) => {
const intro_Questions_Loop = () => {
cy.get('@question_tree').then(question_tree => {//gets the current question tree
cy.get('@link').then(link => { //get the question number
if (action == 'LAST') {
return;
}
cy.Question({q: link, qt: question_tree, question_log: data.fixture}) //checks the question on screen vs question tree
cy.Answer({q: link, qt: question_tree}) //picks a random answer and wraps action with the next question code
cy.editAnswer({}) //edits questions at random dependant on set percentage
intro_Questions_Loop();
})
})
}
intro_Questions_Loop();
})
任何改进、提高效率或只是以不同方式编写的帮助将不胜感激!
最佳答案
递归函数可能会占用大量内存,但 100 次迭代并不是一个大数字。
如果没有看到您未发布的所有隐藏细节,我会说(主要)问题在于您递归到同步函数intro_Questions_Loop
的方式。
尝试将递归调用放在 Cypress 命令队列上
...
cy.editAnswer({})
.then(() => {
intro_Questions_Loop()
})
这应该让测试运行者有时间在进行下一次迭代之前处理每次迭代。
除此之外,很难说,因为太多的实际代码隐藏在自定义命令中。
关于recursion - Cypress 中的递归函数存在问题。测试随着时间的推移而减慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74695637/