Conditional statement in cypress

╄→尐↘猪︶ㄣ 提交于 2020-01-06 07:13:11

问题


I am trying to do a conditional statement on cypress to check if the login link in the header is Sign in or Account or a class and then click on it.

The if condition is not working.

cy.get('header').then((header) => { 

if (header.find('Sign in').length > 0) { 
    cy.contains('Sign In')
    .click({force:true})  

} else if (header.find('Account').length > 0) {
    cy.contains('Account')
    .click()

} else {
    cy.get('.navUser-item--account .navUser-action').click()

}

})

I expect if Sign in found then it will click else if the Account is available then it will click else it will check by the class.

[always doing the last else condition][1] [1]: https://i.stack.imgur.com/liEF9.png

[there is Account text and still it applied the last else condition][2] [2]: https://i.stack.imgur.com/sXYr8.png

Another code structure and now it always apply the first condition no matter what


回答1:


The following code worked for me.

cy.get('header').then(($a) => { 
        if ($a.text().includes('Account')) {
            cy.contains('Account')
            .click({force:true})
        } else if ($a.text().includes('Sign')) { 
            cy.contains('Sign In')
            .click({force:true})  
        } else {
            cy.get('.navUser-item--account .navUser-action').click({force:true})
        }
    })

Thank you for helping,



来源:https://stackoverflow.com/questions/55541171/conditional-statement-in-cypress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!