首页 > 解决方案 > 如何一个接一个地多次运行柏树测试?

问题描述

我正在使用反应创建垄断(用骰子玩的棋盘游戏)。我已经为游戏制作了滚动系统。现在我想测试一下。

我的游戏中有两个按钮

现在我正在使用柏树来测试这个。我已经编写了测试,它运行良好,但只运行了 1 次。当我使用循环多次运行测试时,它失败了

describe("Rolling System", function () {
   //The required test
   it("disables button correctly", function () {

      //Visits the page
      cy.visit("localhost:3000").then(() => {

         //A variable which tell if doubles were rolled at last roll or not 
         let isRollDisabled = false;
         for (let i = 0; i < 10; i++) {

            //If roll is over then end the turn
            if (isRollDisabled) {
               cy.get(".btn-end-turn").click();
            }

            //If roll button is enabled then click it.
            else {
               console.log(isRollDisabled);

               //Get and click button
               cy.get(".btn-roll")
                  .click()
                  .then((x) => {

                     //Get the dice elements
                     cy.get(".dice").then((dices) => {

                        //Get the rolls of both die
                        let roll1 = dices[0].children.length;
                        let roll2 = dices[1].children.length;

                        //If not doubles
                        if (roll1 !== roll2) {
                           cy.get(".btn-roll").should("be.disabled");
                           console.log("roll disabled changed");
                           isRollDisabled = true;
                        } 

                        //If doubles are rolled
                        else {
                           cy.get(".btn-roll").should("not.be.disabled");
                           isRollDisabled = false;
                        }
                     });
                  });
            }
         }
      });
   });
});

注意上面代码中的两个日志console.log(isRollDisabled);console.log("roll disabled changed");

在日志中,我看到false记录的10时间,然后"roll disabled change"记录。

我想我已经编写了代码,使得一个测试一个接一个地运行,但我无法修复它。

标签: javascriptreact-hookscypress

解决方案


从测试体内删除循环并从命令行使用cypress-repeat代替。


推荐阅读