Cypress - 选择包含文本的按钮

Rya*_*rde 11 button reactjs material-ui cypress

我使用 Material-ui 作为我的 CSS 框架,我想选择包含特定文本的按钮。我当前的代码:

cy.get('button').contains('Edit Claim').should('be.disabled');
Run Code Online (Sandbox Code Playgroud)

它失败了,因为 Material ui 的按钮组件输出了 div 内的文本,因此 cypress 断言了 div 的禁用属性。我希望它断言到按钮。

编辑: 在此输入图像描述

Rya*_*rde 9

我用它解决了cy.contains('button', 'Edit Claim').should('be.disabled');


小智 4

Material-UI 演示页面上,标签位于子级中<span>(不是<div>),

<button class="MuiButtonBase-root MuiButton-root MuiButton-contained Mui-disabled Mui-disabled" tabindex="-1" type="button" disabled="">
  <span class="MuiButton-label">Disabled</span>
</button>
Run Code Online (Sandbox Code Playgroud)

但可能会有变化 - 在任何情况下,您都可以通过添加.parent()到测试来引用该按钮

cy.visit('https://material-ui.com/components/buttons/');
cy.get('button')
  .contains('Disabled')      // span is the subject
  .parent()                  // move up to the button
  .should('be.disabled');
Run Code Online (Sandbox Code Playgroud)