赛普拉斯:测试元素是否不存在

时间:2018-02-21 21:28:08

标签: cypress

我希望能够单击一个复选框并测试赛普拉斯中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中。

10 个答案:

答案 0 :(得分:57)

这似乎有用,所以它告诉我还有更多要了解的内容.should()

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

答案 1 :(得分:9)

根据https://docs.cypress.io/guides/references/assertions.html#Existence

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

这适用于被删除的情况。但是如果您希望它永远不存在... docs.cypress.io/guides/references/assertions.html#Existence它会重试直到消失。这实际上不适用于大多数人想要的标题问题。

但是,如果您要测试在我们的案例中该问题永远不存在。

// 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`);
            });
        })

答案 2 :(得分:7)

赛普拉斯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')

迁移文档:https://docs.cypress.io/guides/references/migration-guide.html#Migrating-to-Cypress-6-0

答案 3 :(得分:5)

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

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

在赛普拉斯中,您得到的结果是:0 matched elements

enter image description here

答案 4 :(得分:4)

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

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

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

cy.get('button').contains('Text 1').should('not.exist')

答案 5 :(得分:1)

cy.get(data-e2e="create-entity-field-relation-contact-name").should('not.exist')

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

.should('not.visible') 

在这种情况下。

答案 6 :(得分:0)

这对我有用:

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]')

答案 7 :(得分:0)

您还可以使用以下代码

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

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

should('have.attr','minlength','2')

答案 8 :(得分:0)

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

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

答案 9 :(得分:0)

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

data_chunks <- list()
i <- 1
rows_to_skip <- 0
rows_to_read <- 100000
bad_rows <- 0
file_name <- "my.csv"

while (TRUE) {
  
  out <- tryCatch(
    list(
      data = data.table::fread(file_name, nrows = rows_to_read, skip = rows_to_skip, header = FALSE),
      bad_row = FALSE
    ),
    error = function(e) {
      e
    },
    warning = function(w) {
      warn_msg <- conditionMessage(w)
      warn_matches <- regexec("line (\\d+)", warn_msg)
      rows_to_read <- as.numeric(regmatches(warn_msg, warn_matches)[[1]][2]) - 1
      if (!is.na(rows_to_read)) {
        list(
          data = data.table::fread(file_name, nrows = rows_to_read, skip = rows_to_skip, header = FALSE),
          bad_row = TRUE
        )
      } else {
        NULL
      }
    })
  
  if ("error" %in% class(out) || is.null(out)) {
    break
  } else {
    data_chunks[[i]] <- out[["data"]]
  }

  bad_rows <- bad_rows + out[["bad_row"]]
  rows_to_skip <- sum(sapply(data_chunks, nrow)) + bad_rows
  i <- i + 1
  
}

mydata <- data.table::rbindlist(data_chunks, use.names = FALSE)
相关问题