简体   繁体   English

赛普拉斯,否则 /switch case 不起作用

[英]Cypress, If else /switch case doesn't work

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.我试图在我的测试中添加 else if /switch case ,但是 else if - 它只在 case 中添加,如果 'if' 失败,它不会进入 else 如果它也发生在 switch case 中。 module.exports.selectEnviroment = function(env) { module.exports.selectEnviroment = 函数(环境){

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失败信息

You are using Cypress commands and expecting them to generate results right away.您正在使用 Cypress 命令并期望它们立即生成结果。 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.调用 Cypress 函数只是要求 Cypress 将命令添加到其最终运行的命令列表中的一种方式。

.then() was created with this type of situation in mind. .then()是针对这种情况创建的。 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 .我强烈建议阅读文档中 Cypress 的介绍 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. Cypress 与其他测试框架不同,要编写好的 Cypress 代码,必须基本了解 Cypress 的工作原理。

it might not be related but for switching between environment, follow below steps它可能不相关,但要在环境之间切换,请按照以下步骤操作

1. In Cypress/plugin/index.js , add below code 1. 在Cypress/plugin/index.js ,添加以下代码

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 codecypress/support ,创建一个名为"config.js"的文件并添加以下代码

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

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

  2. under cypress/commands , use this method for logincypress/commands ,使用此方法登录

    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 command 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:如果您的元素有任何随机文本,例如有时它是Initiated有时它是Completed那么你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM