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

nik*_*nik 2 javascript cypress

我试图在我的测试中添加 else if /switch case ,但是 else if - 它仅在 case 时进入,如果 'if' 失败它不会进入 else 如果它也发生在 switch case 中。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;
Run Code Online (Sandbox Code Playgroud)

}

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

根据环境,它应该选择案例,但它没有

    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();
Run Code Online (Sandbox Code Playgroud)

失败信息

Jos*_*ica 7

您正在使用 Cypress 命令并期望它们立即生成结果。这不是赛普拉斯的工作方式。调用 Cypress 函数只是要求 Cypress 将命令添加到其最终运行的命令列表中的一种方式。

.then()是在考虑到这种情况的情况下创建的。它允许您在链中的上一个命令之后添加一些直接运行的代码:

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

我强烈建议阅读文档中 Cypress 的介绍。它写得很好,易于阅读。Cypress 与其他测试框架不同,要编写好的 Cypress 代码,必须基本了解 Cypress 的工作原理。