柏树中的条件陈述 [英] Conditional statement in cypress
问题描述
我正在尝试在cypress上执行条件语句,以检查标题中的登录链接是否为Sign in或Account或某个类,然后单击它。
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.
if条件不起作用。
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.
[始终执行最后一个其他条件] [1] [1]:< a href = https://i.stack.imgur.com/liEF9.png rel = nofollow noreferrer> https://i.stack.imgur.com/liEF9.png
[always doing the last else condition][1] [1]: https://i.stack.imgur.com/liEF9.png
[有帐户t ext并仍然应用了其他条件[2] [2]: https:// i .stack.imgur.com / sXYr8.png
[there is Account text and still it applied the last else condition][2] [2]: https://i.stack.imgur.com/sXYr8.png
推荐答案
以下代码对我有用。
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})
}
})
感谢您的帮助
这篇关于柏树中的条件陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!