279

我希望能够单击一个复选框并测试一个元素不再在赛普拉斯的 DOM 中。有人可以建议你怎么做吗?

//This is the Test when the check box is clicked and the element is there
cy.get('[type="checkbox"]').click();
cy.get('.check-box-sub-text').contains('Some text in this div.')

我想做与上面测试相反的事情。因此,当我再次单击它时,具有该类的 div 不应该在 DOM 中。

4

13 回答 13

374

好吧,这似乎有效,所以它告诉我我还有更多要了解的 .should()

cy.get('.check-box-sub-text').should('not.exist');
于 2018-02-21T21:48:02.717 回答
71

您还可以搜索不应该存在的文本:

cy.contains('test_invite_member@gmail.com').should('not.exist')

这是赛普拉斯的结果:0 matched elements

在此处输入图像描述

文档:https ://docs.cypress.io/guides/references/assertions.html#Existence

于 2019-11-03T19:23:41.760 回答
49

赛普拉斯 6.x+ 迁移

用于.should('not.exist')断言 DOM 中不存在元素。


不要使用断言not.visible。它会错误地传入 < 6.0,但现在正确失败:

// for element that was removed from the DOM
// assertions below pass in < 6.0, but properly fail in 6.0+
.should('not.be.visible')
.should('not.contain', 'Text')

此处的迁移文档:Migrating-to-Cypress-6-0

于 2020-11-24T09:03:53.423 回答
25

赛普拉斯 6.x+ 迁移

根据关于存在的赛普拉斯文档

有点幼稚的非常流行的尝试将一直有效,直到它不起作用,然后您将不得不再次重写它......一次又一次......

// retry until loading spinner no longer exists
cy.get('#loading').should('not.exist')

这实际上不适用于大多数人会寻找的标题问题。

这适用于它被删除的情况。但如果你希望它永远不存在......它会重试直到它消失。

但是,如果您想测试该元素在我们的案例中不存在。

是的哈哈。这是你真正想要的,除非你想改天再头痛一次。

// Goes through all the like elements, and says this object doesn't exist ever
cy.get(`img[src]`)
  .then(($imageSection) => {
    $imageSection.map((x, i) => { expect($imageSection[x].getAttribute('src')).to.not.equal(`${Cypress.config().baseUrl}/assets/images/imageName.jpg`) });
})
于 2020-05-22T14:52:49.337 回答
20
cy.get('[data-e2e="create-entity-field-relation-contact-name"]').should('not.exist');

可能会导致一些错误的结果,因为一些错误消息会被隐藏。使用可能会更好

.should('not.visible');

在这种情况下。

于 2019-04-11T12:26:17.077 回答
6

这对我有用:

cy.get('[data-cy=parent]').should('not.have.descendants', 'img')

我检查了一些<div data-cy="parent">里面没有图像。关于原始问题,您可以data-cy="something, i.e. child"在内部节点上设置属性并使用此断言:

cy.get('[data-cy=parent]').should('not.have.descendants', '[data-cy=child]')
于 2019-05-26T09:12:03.827 回答
5

您也可以使用getcontains来区分 HTML 元素。

<button type='button'>Text 1</button>
<button type='button'>Text 2</button>

假设您有 2 个带有不同文本的按钮,并且您想检查第一个按钮是否不存在,那么您可以使用;

cy.get('button').contains('Text 1').should('not.exist')
于 2021-02-19T17:00:33.050 回答
3

也可以在 cypress 中使用 jQuery 模式完成:

assert(Cypress.$('.check-box-sub-text').length==0)
于 2021-05-26T09:22:32.297 回答
2

cypress 中没有 try-catch 流

在 java-selenium 中,我们通常添加 NoSuchElementException 并执行我们的案例。如果 UI 未显示某些基于角色的访问案例的元素。

于 2021-09-01T16:23:28.537 回答
1

您也可以使用以下代码

expect(opportunitynametext.include("Addon")).to.be.false

或者

should('be.not.be.visible')

或者

should('have.attr','minlength','2')
于 2019-10-29T13:40:21.357 回答
1

我关闭了一个元素并检查了 should('not.exist') 但断言失败,因为它存在于 DOM 中。只是它不再可见。

在这种情况下, should('not.visible') 对我有用。我刚开始使用柏树。很多东西要学。

于 2021-04-13T07:11:53.580 回答
0

就我而言,赛普拉斯是如此之快,以至于.should('not.be.visible')通过了测试,然后,加载器出现并且测试失败了。

我已经成功地做到了这一点:

cy.get('.loader__wrapper')
  .should('be.visible')
cy.get('.loader__wrapper', { timeout: 10000 })
  .should('not.be.visible')

当您的应用程序加载超过 4 秒时,也可以将超时设置为 10 秒。

于 2022-01-28T17:31:36.373 回答
-1

我会使用:

cy.get('.check-box-sub-text').should('not.be.visible');

这比安全

cy.get('.check-box-sub-text').should('not.exist');

(该元素可以存在于 DOM 中,但在display: noneopacity: 0中不可见)

于 2022-02-08T08:08:24.670 回答