Cypress, cy.findByRole(‘textbox’, { name: /Seu e-mail/i }) not finding a text input

huangapple go评论62阅读模式
英文:

Cypress, cy.findByRole('textbox', { name: /Seu e-mail/i }) not finding a text input

问题

以下是您的翻译部分:

我有这段测试代码

describe('smoke', () => {
  it('should type e-mail', () => {
    cy.visit('/auth/login');
    cy.findByRole('textbox', { name: /Seu e-mail/i }).type(
      'analyst@open-cred.com'
    );
  });
});

这是HTML:

<div role="group" class="chakra-form-control css-1kxonj9">
<label for="email" id="field-:r1:-label" class="chakra-form__label css-1q3y7ys" data-invalid="">E-mail</label>
<input name="email" placeholder="Seu e-mail" type="text" id="email" class="chakra-input css-9m5lgx" value="" aria-invalid="true" aria-describedby="field-:r1:-feedback">
<div id="field-:r1:-feedback" aria-live="polite" class="chakra-form__error-message css-170ki1a">Um e-mail é necessário</div></div>

根据W3文档,文本输入具有role=textbox。但测试无法找到文本输入。

有人知道我做错了什么吗?

英文:

I have this test code

describe(&#39;smoke&#39;, () =&gt; {
  it(&#39;should type e-mail&#39;, () =&gt; {

    cy.visit(&#39;/auth/login&#39;);
    cy.findByRole(&#39;textbox&#39;, { name: /Seu e-mail/i }).type(
      &#39;analyst@open-cred.com&#39;
    );
  });
});

This is the HTML:

&lt;div role=&quot;group&quot; class=&quot;chakra-form-control css-1kxonj9&quot;&gt;
&lt;label for=&quot;email&quot; id=&quot;field-:r1:-label&quot; class=&quot;chakra-form__label css-1q3y7ys&quot; data-invalid=&quot;&quot;&gt;E-mail&lt;/label&gt;
&lt;input name=&quot;email&quot; placeholder=&quot;Seu e-mail&quot; type=&quot;text&quot; id=&quot;email&quot; class=&quot;chakra-input css-9m5lgx&quot; value=&quot;&quot; aria-invalid=&quot;true&quot; aria-describedby=&quot;field-:r1:-feedback&quot;&gt;
&lt;div id=&quot;field-:r1:-feedback&quot; aria-live=&quot;polite&quot; class=&quot;chakra-form__error-message css-170ki1a&quot;&gt;Um e-mail &#233; necess&#225;rio&lt;/div&gt;&lt;/div&gt;

By W3 documentation a text input has role=textbox.
But the test can't find the text input.

Does somebody know what I'm doing wrong?

答案1

得分: 2

"Testing Library" 中的角色查询中的 "name" 选项是指辅助树中元素的名称。对于输入元素,通常是指输入的标签(如果有的话,应该有),而不是占位符。

您可以在 DevTools 中看到它的辅助名称是什么:
Cypress, cy.findByRole(‘textbox’, { name: /Seu e-mail/i }) not finding a text input

这显示它的辅助名称来自标签,而不是占位符。您还可以确认该角色也是 "textbox"。

在这种情况下,使用以下代码应该可以找到输入元素:

cy.findByRole('textbox', { name: /e\-mail/i })

最后,Testing Playground 可以帮助排除为什么 Testing Library 无法识别元素并识别更好的查询方法。

英文:

The name option on role queries in Testing Library refers to the element's name in the accessibility tree. For input elements, that usually refers to the input's label (if it has one, which it should), rather than its placeholder.

You can see in DevTools what its accessible name is:
Cypress, cy.findByRole(‘textbox’, { name: /Seu e-mail/i }) not finding a text input

This shows that its accessible name is coming from the label, rather than the placeholder. You can also confirm that the role is "textbox" there as well.

In this case

cy.findByRole(&#39;textbox&#39;, { name: /e\-mail/i })

should work to find that input element.


Finally, the Testing Playground can help troubleshoot why Testing Library is not picking up on elements and to identify better queries.

huangapple
  • 本文由 发表于 2023年3月21日 00:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定