Cypress, If else /switch case doesn't work

16,681

You are using Cypress commands and expecting them to generate results right away. This is not how Cypress works. Calling a Cypress function is simply a way to ask Cypress to add a command to its list of commands to eventually run.

.then() was created with this type of situation in mind. It allows you to add some code to be run directly after the previous command in the chain:

cy.get('.myDiv').then(elem => {
    // elem is a jQuery object
    console.log(elem.text());
    if (elem.text() == 'Some text') {
        // do something
    else {
        // ...
    }
}

I strongly suggest reading the introduction to Cypress in the docs. It is well-written and easy to read. Cypress is not like other testing frameworks, and a basic understanding of how Cypress works is necessary to write good Cypress code.

Share:
16,681

Related videos on Youtube

nik
Author by

nik

Updated on June 04, 2022

Comments

  • nik
    nik almost 2 years

    I am trying to add else if /switch case in my test , but else if - it goes to only if case, if 'if' fail it doesn't go in else if it's happen in switch case also. module.exports.selectEnviroment = function(env) {

    switch (env) {
    case 'alpha':
      cy.get('[role="presentation"]')
        .find('[href="#/company-detail/5bb3765e64f66ca0027e15245"]')
        .click();
      break;
    case 'beta':
      cy.get('[role="presentation"]')
        .find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]')
        .eq(0)
        .click();
      break;
    

    }

     it('Booking should be done using invoice', () => {
        cy.visit(`${blah_URL}#/xyz/`);
        let env = blah.split('.')[1];
        selectEnviroment(env);
    

    According to environment, it should select the case ,but it doesn't

        if (
        cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]') ) {
        cy.get('[role="presentation"]')
          .find('[ng-href="#/company-detail/5bb62c019ee36000273a6e2b"]')
          .eq(0)
          .click();
      } //alpha
      else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bae05a39af4a90027fcdf43"]')) {
        cy.get('[role="presentation"]')
          .find('[ng-href="#/company-detail/5bae05a39af4a90027fcdf43"]')
          .eq(0)
          .click();
      } //QA
      else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5b855022323d37000f48bcdc"]')) {
        cy.get('[role="presentation"]')
          .find('[ng-href="#/company-detail/5b855022323d37000f48bcdc"]')
          .eq(0)
          .click();
      } //Gamma
      else if (cy.get('[role="presentation"]').find('[ng-href="#/company-detail/5bb62ccf5cb043002737d929"]')
      ) {
        cy.get('[role="presentation"]')
          .find('[ng-href="#/company-detail/5bb62ccf5cb043002737d929"]')
          .eq(0)
          .click();
      }
    
    it('flight booking should be done using new credit card', () => {
    cy.visit(`${COCKPIT_URL}#/company-list/`);
    selectEnviroment();
    

    failure message