我正在尝试使用 Cypress 和 testing-library/cypress 检查 DOM 树中是否不存在元素。
如果我尝试做 cy.getByTestId("my-button").should("not.exist")
测试失败,因为它找不到元素。
如果我这样做 cy.findByTestId("my-button").should("not.exist")
它也因超时而失败。
如果我执行 cy.queryByTestId("my-button").should("not.exist")
,测试确实有效或者cy.get('[data-testid="my-button"]').should("not.exist")
.
有人可以解释一下这4个有什么区别吗?
谢谢
最佳答案
https://testing-library.com/docs/dom-testing-library/api-queries
getBy
如果找不到元素 findBy
如果没有找到元素 queryBy
如果没有找到元素,将返回 null:This is useful for asserting an element that is not present.
看起来像
queryBy
是您解决此问题的最佳选择
关于javascript - Cypress getByTestId、queryByTestId、findByTestId 检查元素是否不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995950/