javascript - Cypress, If else switch case doesn't work - Stack Overflow

I am trying to add else if switch case in my test , butelse if - it goes to only if case, if 'if

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="#/pany-detail/5bb3765e64f66ca0027e15245"]')
    .click();
  break;
case 'beta':
  cy.get('[role="presentation"]')
    .find('[ng-href="#/pany-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="#/pany-detail/5bb62c019ee36000273a6e2b"]') ) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bb62c019ee36000273a6e2b"]')
      .eq(0)
      .click();
  } //alpha
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5bae05a39af4a90027fcdf43"]')) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bae05a39af4a90027fcdf43"]')
      .eq(0)
      .click();
  } //QA
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5b855022323d37000f48bcdc"]')) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5b855022323d37000f48bcdc"]')
      .eq(0)
      .click();
  } //Gamma
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5bb62ccf5cb043002737d929"]')
  ) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bb62ccf5cb043002737d929"]')
      .eq(0)
      .click();
  }

it('flight booking should be done using new credit card', () => {
cy.visit(`${COCKPIT_URL}#/pany-list/`);
selectEnviroment();

failure message

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="#/pany-detail/5bb3765e64f66ca0027e15245"]')
    .click();
  break;
case 'beta':
  cy.get('[role="presentation"]')
    .find('[ng-href="#/pany-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="#/pany-detail/5bb62c019ee36000273a6e2b"]') ) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bb62c019ee36000273a6e2b"]')
      .eq(0)
      .click();
  } //alpha
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5bae05a39af4a90027fcdf43"]')) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bae05a39af4a90027fcdf43"]')
      .eq(0)
      .click();
  } //QA
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5b855022323d37000f48bcdc"]')) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5b855022323d37000f48bcdc"]')
      .eq(0)
      .click();
  } //Gamma
  else if (cy.get('[role="presentation"]').find('[ng-href="#/pany-detail/5bb62ccf5cb043002737d929"]')
  ) {
    cy.get('[role="presentation"]')
      .find('[ng-href="#/pany-detail/5bb62ccf5cb043002737d929"]')
      .eq(0)
      .click();
  }

it('flight booking should be done using new credit card', () => {
cy.visit(`${COCKPIT_URL}#/pany-list/`);
selectEnviroment();

failure message

Share Improve this question asked Oct 5, 2018 at 11:54 niknik 491 gold badge1 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You are using Cypress mands 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 mand to its list of mands 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 mand 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.

it might not be related but for switching between environment, follow below steps

1. In Cypress/plugin/index.js, add below code

const envConfig = require('../support/config');

/* eslint-disable no-param-reassign */
module.exports = (on, config) => {
  config.baseUrl = envConfig(config.env.APP_ENV).baseUrl;

  return config;
}
  1. Under cypress/support , create a file called "config.js" and add below code

    `const config = { prod: { baseUrl: 'https://......./data' }, qa: { baseUrl: 'https://...../data' }, dev: { baseUrl: 'http://localhost:8080' } }

    module.exports = typeof Cypress !== 'undefined' ? config[Cypress.env('APP_ENV')] : env => config[env];`

  2. under cypress/mands, use this method for login

    Cypress.Commands.add('login', (username, password) => { cy.visit(Cypress.config('baseUrl')) cy.url().then(url => { if ( url.indexOf('authorization.oauth2') !== -1 || url.indexOf('auth-corp-aws') !== -1 ) { cy.get('#username').type(Cypress.env('auth_username')) cy.get('#password').type(Cypress.env('auth_password'), { log: false }) cy.get('.ping-button.normal.allow').click() cy.wait(1000) } }) })

  3. use this mand to run the tests in different environments:

    "cy:e2e:qa_env": "CYPRESS_APP_ENV=qa cypress run --headed --browser chrome", "cy:e2e:dev_env": "CYPRESS_APP_ENV=dev cypress run --headed --browser chrome", "cy:e2e:prod_env": "CYPRESS_APP_ENV=prod cypress run --headed --browser chrome",

it('does something different based on the class of the button', () => {
  // RERUN THIS TEST OVER AND OVER AGAIN
  // AND IT WILL SOMETIMES BE TRUE, AND
 // SOMETIMES BE FALSE.

cy.get('button').then(($btn) => {
   if ($btn.hasClass('active')) {
   // do something if it's active
   } else {
   // do something else
   }
 })
})

If your element has any random text for example sometime it is Initiated & sometimes it is Completed then you can do like this:

cy.get('.value-text').then($el => {
        // $el is a jQuery object
        console.log($el.text());
        if ($el.text() == 'Initiated') {
 
            cy.get('.edit_status > #cg-icon').click()
        } else {
             // Here You can click on another element.
        }
    })

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743711813a4494314.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信