无论如何,柏树禁用的按钮无论如何都单击而无需等待

发布于 2025-02-09 17:47:37 字数 1643 浏览 1 评论 0原文

这不是我期望柏树的行为: 我有一个仅在下表中选中复选框的按钮。 在检查2行之前,请按下按钮:

button[data-e2e-button][disabled=true]

现在,柏树检查2行,并启用了按钮。即“ [disabled = true]”消失。我的代码看起来像这样:

非工作代码:

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

单击此元素时,似乎没有等到启用按钮。

只有当我单独等待残疾人= the true以消失时,它起作用了:

工作代码:

cy.contains('[data-e2e-button][aria-disabled="true"]', buttonLabel, BUTTON_OPTS).should('not.exist');
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

这对我来说似乎很丑陋,因为它可以确保两次残疾消失。

这是HTML代码: 启用:

<span role="tooltip">
<a class="MuiMenuItem-root MuiMenuItem-dense MuiMenuItem-gutters MuiButtonBase-root css-brjsa3" tabindex="-1" role="menuitem"><span>
<span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
</span>
<span class="MuiTouchRipple-root css-w0pj6f"></span>
</a>
</span>

禁用

<span role="tooltip">
<a class="MuiMenuItem-root MuiMenuItem-dense Mui-disabled MuiMenuItem-gutters MuiButtonBase-root Mui-disabled css-brjsa3" tabindex="-1" aria-disabled="true" role="menuitem"><span>
<span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
</span></a></span>

This is not how I would expect cypress to behave:
I have a button which is only enabled when to checkboxes are checked in the table below.
Before the checking of the 2 rows, the button is greyed out with:

button[data-e2e-button][disabled=true]

Now cypress checks the 2 rows and the button becomes enabled. i.e. the "[disabled=true]" disappears. My code looks like this:

Non Working Code:

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

When it clicks on this element, it seems like it did not wait until the button is enabled.

Only if I have a separate wait for the disabled=true to disappear it works:

Working Code:

cy.contains('[data-e2e-button][aria-disabled="true"]', buttonLabel, BUTTON_OPTS).should('not.exist');
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

This seems kind of ugly to me, as it ensures twice for disabled to disappear.

Here is the html code:
ENABLED:

<span role="tooltip">
<a class="MuiMenuItem-root MuiMenuItem-dense MuiMenuItem-gutters MuiButtonBase-root css-brjsa3" tabindex="-1" role="menuitem"><span>
<span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
</span>
<span class="MuiTouchRipple-root css-w0pj6f"></span>
</a>
</span>

DISABLED

<span role="tooltip">
<a class="MuiMenuItem-root MuiMenuItem-dense Mui-disabled MuiMenuItem-gutters MuiButtonBase-root Mui-disabled css-brjsa3" tabindex="-1" aria-disabled="true" role="menuitem"><span>
<span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
</span></a></span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2025-02-16 17:47:37

如果您的属性为aria-disabled =“ true”如下所示

<a class="MuiMenuItem-root MuiMenuItem-dense Mui-disabled MuiMenuItem-gutters MuiButtonBase-root Mui-disabled css-brjsa3" 
  tabindex="-1" 
  aria-disabled="true"                 // this is the attribute being checked
  role="menuitem">
  <span>
    <span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
  </span>
</a>

,则。禁用')不使用该属性。

他们只能使用属性disabled =“ true”,似乎没有存在。

它解释了为什么它起作用

cy.contains('[data-e2e-button][aria-disabled="true"]', buttonLabel, BUTTON_OPTS).should('not.exist');
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

,因为在第一行中确实检查了该属性。

尝试其中一个

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('not.have.attr', 'aria-disabled')
  .click()

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('have.attr', 'aria-disabled', 'false')
  .click()

If your attribute is aria-disabled="true" as shown here

<a class="MuiMenuItem-root MuiMenuItem-dense Mui-disabled MuiMenuItem-gutters MuiButtonBase-root Mui-disabled css-brjsa3" 
  tabindex="-1" 
  aria-disabled="true"                 // this is the attribute being checked
  role="menuitem">
  <span>
    <span class="MuiBox-root css-1jjwf1f">Two Checkbox button</span>
  </span>
</a>

then .should('be.disabled') and .should('not.be.disabled') do not work with that attribute.

They would only work with attribute disabled="true" which does not seem to be present.

It explains why this works

cy.contains('[data-e2e-button][aria-disabled="true"]', buttonLabel, BUTTON_OPTS).should('not.exist');
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS).should('not.be.disabled').should('be.visible').click...

because in the first line does check that attribute.

Try either of these

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('not.have.attr', 'aria-disabled')
  .click()

or

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('have.attr', 'aria-disabled', 'false')
  .click()
再可℃爱ぅ一点好了 2025-02-16 17:47:37

在使用负面断言时要小心,它们通常不太可靠。而不是使用

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('be.visible')
  .and('be.disabled')
// actions to meet enabled condition
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('be.visible')
  .and('be.enabled')
  // now we know for sure the button is enabled and click on it
  .click()

Be careful when using negative assertions, they are often less reliable. Instead of using .should('not.be.disabled') before clicking the element, make sure the element is disabled before meeting the enabled condition.

cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('be.visible')
  .and('be.disabled')
// actions to meet enabled condition
cy.contains(`[data-e2e-button]`, buttonLabel, BUTTON_OPTS)
  .should('be.visible')
  .and('be.enabled')
  // now we know for sure the button is enabled and click on it
  .click()
夏有森光若流苏 2025-02-16 17:47:37

您可以做这样的事情。您也可以提出其他选择器,如果这不起作用,这个想法仍然相同。

cy.get('a.MuiMenuItem-root').should('exist').and('be.disabled')
//code to click the checkboxes
cy.get('a.MuiMenuItem-root').should('exist').and('be.enabled').click()

如果cy.get()不起作用,则可以与选择器和按钮标签一起使用,例如:

cy.contains('a', 'button label').should('exist').and('be.disabled')
//code to click the checkboxes
cy.contains('a', 'button label').should('exist').and('be.enabled').click()

You can do something like this. You can come up with a different selector as well, if this doesn't work, the idea remains the same.

cy.get('a.MuiMenuItem-root').should('exist').and('be.disabled')
//code to click the checkboxes
cy.get('a.MuiMenuItem-root').should('exist').and('be.enabled').click()

In case cy.get() doesn't work, you can use contains with selector and button label, something like:

cy.contains('a', 'button label').should('exist').and('be.disabled')
//code to click the checkboxes
cy.contains('a', 'button label').should('exist').and('be.enabled').click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文