如何单击带有ARIA标签的按钮?

发布于 2025-01-29 18:51:17 字数 682 浏览 5 评论 0原文

我正在尝试使用柏树测试框架自动单击按钮。

源代码用React编写,我要单击的元素是带有伪代码的按钮。

这是用反应代码编写的按钮的HTML。

<form class="search" action="/search?q=somevalue">
    <button type="button" aria-label="Submit search" class="ms-search__form-submitSearch msc-btn">
    ::before
    </button>
</form>

这是我试图用来使元素点击的测试代码,

cy.get('[aria-label="Submit search"]').click()

但是点击不起作用。

我也尝试过

cy.get('[aria-label="Submit search"]').click('{force:true}')
Cy.get('[aria-label="Submit search"]').trigger('mousedown')

任何操作,在单击按钮时没有发射任何事件。当我手动单击该按钮时,它可以正常工作。

I am trying to automate a button click using the Cypress testing framework.

The source code is written in React, and the element I am trying to click is a button with pseudo code.

Here is the HTML of the button written in react code.

<form class="search" action="/search?q=somevalue">
    <button type="button" aria-label="Submit search" class="ms-search__form-submitSearch msc-btn">
    ::before
    </button>
</form>

Here is the test code I am trying to use to get the element to click

cy.get('[aria-label="Submit search"]').click()

However the click did not work.

I also tried

cy.get('[aria-label="Submit search"]').click('{force:true}')
Cy.get('[aria-label="Submit search"]').trigger('mousedown')

None of this worked, no events were fired when the button is clicked. When I manually click on the button it works fine.

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

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

发布评论

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

评论(3

_失温 2025-02-05 18:51:18

在测试点击时,可能不会附加事件侦听器。

请参阅求解第一个单击

添加 cypress-cdp 软件包,并使用cy.haseventlisteners()

const selector = '[aria-label="Submit search"]';
cy.hasEventListeners(selector, { type: 'click' });
cy.get(selector).click();

作为快速检查,而无需上述软件包,请添加(暂时)硬CY.WAIT(300)单击之前。

我还看到了React钩子未完成的情况,因为测试具有JS线程,在这种情况下,您可能需要的只是cy.wait(0)以使Cypress释放线程。

It may be that the event listener isn't attached at the time the test clicks.

See Solve The First Click

Add the cypress-cdp package and try with cy.hasEventListeners()

const selector = '[aria-label="Submit search"]';
cy.hasEventListeners(selector, { type: 'click' });
cy.get(selector).click();

As a quick check without the above package, add (temporarily) a hard cy.wait(300) before the click.

I have also seen situations where React hooks are not completing because the test has the JS thread, in which case all you might need is cy.wait(0) to make Cypress release the thread.

旧话新听 2025-02-05 18:51:18

假设您的按钮位于特定元素内:

<div id="external-wrapper">
    <button type="button" aria-label="Submit search" class="ms-search__form-submitSearch msc-btn">
    ::before
    </button>
</div>

您可以先确定容器,然后找到“ Aria-Label”:

cy.get('#external-wrapper').find('[aria-label="Submit search"]').click('{force:true}');

Assuming that your button is inside a specific element:

<div id="external-wrapper">
    <button type="button" aria-label="Submit search" class="ms-search__form-submitSearch msc-btn">
    ::before
    </button>
</div>

you could do first identify the container and then find the "aria-label":

cy.get('#external-wrapper').find('[aria-label="Submit search"]').click('{force:true}');
殤城〤 2025-02-05 18:51:18
cy.get('form').find('[aria-label="Submit search"]').click();

cy.get('form'):

此命令选择页面上的元素。该表格通常用于分组输入元素,允许用户将数据提交到服务器,例如搜索栏或登录表单。

.find('['[aria-label =“ crist search'']'):

查找命令用于搜索所选元素中的后代元素。
在这里,它正在寻找具有带有“提交搜索”值的ARIA-LABEL属性的元素。 ARIA-LABEL属性通常用于通过为屏幕阅读器提供文本标签来改善可访问性。在这种情况下,它将该元素标记为用于提交搜索的元素。

.click():

此命令单击上一步中发现的元素。此操作模拟用户单击“提交搜索”按钮。

说明:

在上述代码中,他从表单标签中提到
我已将HTML表单标签代替类或ID
对于柏树,使用唯一的ID或类来触发元素以“ [aria-label =“ crist search”]”,然后单击该元素。

cy.get('form').find('[aria-label="Submit search"]').click();

cy.get('form'):

This command selects the element on the page. The form is typically used to group input elements that allow users to submit data to a server, like a search bar or a login form.

.find('[aria-label="Submit search"]'):

The find command is used to search for a descendant element within the selected element.
Here, it's looking for an element that has an aria-label attribute with the value "Submit search". The aria-label attribute is often used to improve accessibility by providing a text label for screen readers. In this case, it labels the element as the one used to submit a search.

.click():

This command clicks on the element that was found in the previous step. This action simulates a user clicking on the "Submit search" button.

Explanation:

In the above code he mention from the form tag
I have take the html form tag in place of the class or Id
for the cypress used unique id or class to trigger the elements took the "[aria-label="Submit search"]" then I click that element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文