如果cypress中的else语句不起作用-我的代码有什么问题?

问题描述 投票:0回答:1

我开始使用赛普拉斯自动化测试工具。我要完成的工作:

如果页面包含“没有结果”,则“标记为已认证”按钮不应该存在。否则,如果页面不包含“没有结果”,则单击“全选”按钮。应存在“标记为已认证”按钮。

这里是我到目前为止的代码:

cy.get('body').then($body=> {
        if ($body.find('There are no results')) {
            //cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0');
            cy.get('Mark as Certified').should('not.exist');
        }
        else {
            cy.get('input[id="selectAll"]').click();
            cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
            cy.get('.hZDZBR').click();

        }
    })

赛普拉斯现在所做的是:它进入if循环,执行它,并说“找不到标记为已认证”。但是我的问题是,如果页面上不存在“没有结果”,也找不到它,那为什么还要执行if循环并进入else循环呢?因此它正在执行if循环,就好像它找到“已认证标记”一样,但我可以看到页面上不存在该标记。

非常感谢您的帮助!

所以我将上面的代码更新为:

       cy.wait(5000);
        if ($body.find('There are no results').length>0) {
            //cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0');
            cy.wait(5000);
            cy.get('Mark as Certified').should('not.exist');
            cy.get('input[id="selectAll"]').should('not.exist');
        }
        else if($body.find('There are no results').length==0) {

            cy.get('input[id="selectAll"]').click();
            cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
            cy.get('.hZDZBR').click();

        }
    })````
Now the control goes to else loop and does not execute: 
````cy.get('Mark as Certified').should('not.exist');
     cy.get('input[id="selectAll"]').should('not.exist');

我的代码怎么了?

嗨,我再次使用以下代码编辑了代码:

const $el=Cypress.$('.Typography__StyledTypography-sc-153d8g4-0 jhYDmS TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1 ihRiqU');
       if($el.length){
        cy.log($el.length);
        cy.get('Mark as Certified').should('not.exist');
       } else{
        cy.get('input[id="selectAll"]').click();
        cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
        cy.get('.hZDZBR').click();
       }

“页面上没有结果”,但是cypress仍然进入else循环,我不明白为什么。另外,我正在尝试使用cy.log($ el.length)记录$ el.length,但是我看不到它在任何地方记录此值。

javascript if-statement testing automation cypress
1个回答
0
投票

您不能在这样的cy.wait之后同步使用代码。您需要使用.then

cy.wait(5000).then(()=>{
  // if ($body.find(...
})


© www.soinside.com 2019 - 2024. All rights reserved.