Lay*_*yup 7 reactjs testing-library
当元素确实存在时,什么可能导致getByLabelText从测试库到\xc2\xa0报告找不到该元素?我得到的确切错误是
\n\nTestingLibraryElementError:无法找到带有以下文本的标签:删除项目:项目 C
\n
它是在我运行测试后出现的,报告线路失败
\nconst removeItemButton = screen.getByLabelText(\'Remove Item: Item C\');\nRun Code Online (Sandbox Code Playgroud)\n测试输出包含\xc2\xa0included 的一大块页面源
\n<span\n class="my-company-span"\n data-testid="custom-react-tag"\n>\n Item: Item C\n <button\n aria-label="Remove Item: Item C"\n class="myco-chp__button"\n type="button"\n >\n ...\nRun Code Online (Sandbox Code Playgroud)\n为了进一步证明该元素确实存在,我最终使用不同的函数 ( getAllByRole) 来获取它,但我认为getByLabelText会更干净。
该文档表示aria-label\xc2\xa0attributes 是getByLabelText. aria-label为了防止其中出现异常字符,我将错误输出中的名称直接复制粘贴到getByLabelText参数中,但没有任何变化。我getByLabelText()在同一文件中的其他元素上成功使用了该函数,因此这不是一个糟糕的导入。
我在网上看到了很多类似但不一样的错误“无法找到带有文本的元素”的问题,但我发现针对我的情况的唯一问题是使用 iframe 的人,它不不适用于我。
\n小智 5
不确定这是否是这里发生的情况,但我在测试 Material UI Textfields 时遇到了类似的问题。我最终所做的就是将确切的参数传递为 false。从输出来看,并不清楚可能会添加哪些额外字符。
对于您的查询,它看起来像这样:
const removeItemButton = screen.getByLabelText('Remove Item: Item C', {exact:false});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6425 次 |
| 最近记录: |