Cypress 运行 条件测试

Cypress run test on condition

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

会是那样的,显然是行不通的。我可以看到警报“已启用”,但嵌套测试“运行 正确测试”不会 运行。这样做的正确方法是什么? 当然这是简化了,还有很多测试,不只是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", () => {...});
            }
        });
    });
});

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

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
               }
     })
})

您可以通过 this.skip():

尝试 Mocha 的 pending tests 功能
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
            }
        });
    });
});

这是在 Cypress 运行程序中显示跳过的测试的方式: