1

我不清楚如何仅在满足异步条件时运行测试:如果页面上存在元素。

它会是这样的,这显然是行不通的。我可以看到警报“启用”,但嵌套测试“运行正确的测试”不会运行。这样做的正确方法是什么?当然,这是简化的,还有很多测试,不只是1。

context("table", () => {

    it("Check availability", () => {
        cy.get("div.table").then($div => {
            if (!Cypress.$(".header[title]", $div).length) {
                alert("not enabled");
            } else {
                alert("enabled");
                it("Run proper tests", () => {...});
            }
        });
    });
});
4

2 回答 2

1

您可以通过以下方式尝试 Mocha 的待定测试功能this.skip()

context("table", () => {

    it("Run proper tests", () => {
        cy.get("div.table").then(function($div) {
            if (!Cypress.$(".header[title]", $div).length) {
                this.skip()
            } else {
                // Run proper test
            }
        });
    });
});

这是在赛普拉斯运行器中显示跳过的测试的方式: 在此处输入图像描述

于 2021-10-28T17:38:29.397 回答
1

我会创建一个新实例并使用一个变量

context("table", () => {

let result

    it("Check availability", () => {
        cy.get("div.table").then($div => {
            if (!Cypress.$(".header[title]", $div).length) {
                alert("not enabled");
                result='fail'
            } 
            else {
                alert("enabled");
            }
     })

     it("Run proper tests", () => {
          if (result === 'fail') {
          console.log('not enabled')
          }
          else {
           Run proper test
               }
     })
})
于 2021-10-28T13:20:25.460 回答