Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Cypress run test on condition

I'm not clear on how to run tests only if an async condition is satisfied: if an element is present on the page.

It'd be something like that, which obviously doesn't work. I can see the alert "enabled", but the nested test "Run proper tests" won't run. What is the right way to do this? Of course, this is simplified, there are a lot of more tests, not just 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", () => {...});
            }
        });
    });
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would make a new instance and use a variable

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
               }
     })
})
about 4 years ago · Juan Pablo Isaza Report

0

You could try Mocha's pending tests feature via 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
            }
        });
    });
});

This is how a skipped test is displayed in the Cypress runner: enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!