recursion - Cypress 中的递归函数存在问题。测试随着时间的推移而减慢

标签 recursion cypress cypress-custom-commands

我正在开发一个诊断应用程序,它可以向用户询问大量问题(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/

相关文章:

haskell - 递归 IO 函数中的内存泄漏 - PAP

javascript - 如何显示数字中最大的数字 - javascript

graphql - 如何让 Cypress 动态更新夹具到 graphql 调用

Cypress 条件按钮单击

cypress - 如何在 Cypress 中实现获取表格单元格的命令?

java - 通过递归打印你可以爬楼梯的总数

c++ - 如果比较取决于返回值,是否可以进行尾递归?