admin管理员组文章数量:1122846
This button has 2 states, "chevron_right" when disabled. And "expand_more" when enabled.
Its default state is "chevron_right"
The following code will click on the button the first time. But once it is enabled will error saying because it cannot find "chevron_right". Does anyone know what i am doing wrong?
cy.get('#TreeView mat-nested-tree-node').find('button').first().then(button => {
if (cy.wrap(button).contains('chevron_right')) {
cy.wrap(button).click()
} else{
cy.log("Do nothing")
}
})
There are multiple buttons on the page that will have these 2 states, so i am unable to just search for 1 instance of it.
This button has 2 states, "chevron_right" when disabled. And "expand_more" when enabled.
Its default state is "chevron_right"
The following code will click on the button the first time. But once it is enabled will error saying because it cannot find "chevron_right". Does anyone know what i am doing wrong?
cy.get('#TreeView mat-nested-tree-node').find('button').first().then(button => {
if (cy.wrap(button).contains('chevron_right')) {
cy.wrap(button).click()
} else{
cy.log("Do nothing")
}
})
There are multiple buttons on the page that will have these 2 states, so i am unable to just search for 1 instance of it.
Share Improve this question edited Nov 24, 2024 at 6:37 Wang.Miao 855 bronze badges asked Nov 22, 2024 at 9:27 Jessa BrandyJessa Brandy 1095 bronze badges1 Answer
Reset to default 5Don't cy.wrap(button)
use jQuery instead.
The text()
method from jQuery gets the innerText
and .includes()
is the javascript method to check for "contains".
cy.get('#TreeView mat-nested-tree-node').find('button').first()
.then($button => {
if ($button.text().includes('chevron_right')) {
cy.wrap($button).click()
} else{
cy.log("Do nothing")
}
})
The $
prefix on yielded parameter button
is convention, but it's not needed to make this work.
本文标签: CypressHow to only click button if it contains quotchevronrightquotStack Overflow
版权声明:本文标题:Cypress - How to only click button if it contains "chevron_right" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304828a1932373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论