如何获取所有元素,直到柏树中没有任何元素

时间:2020-08-06 14:40:09

标签: javascript reactjs google-chrome cypress e2e-testing

在我的cypress项目中有一个命令,该命令应该从用户的帐户中删除某个实体(文件)的所有注册表,并在每次测试之前运行。

主要步骤如下:

  • 等待页面加载完成
  • 单击每个删除按钮。
  • 将显示带有文本的模式。
  • 复制其中的删除文本。
  • 在输入上粘贴。
  • 单击删除按钮。
  • 查询DOM以查找仅在删除所有条目时才会显示的文本。
  // Await loading
  cy.contains('Carregando...').should('not.be.visible')

  // Find all delete buttons by its svg path
  cy.get(
    `[d='M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z']`,
  )
    .parent()
    .parent()
    .parent()
    .each((delete_button, index, list) => {
      cy.wrap(delete_button).click({ force: true })
      // Fills in modal using the correct deletion phrase.
      cy.contains(/^Escreva "(.*)" para confirmar/).then(el => {
        const reg = RegExp(/^Escreva "(.*)" para confirmar/)
        const confirmation = reg.exec(el[0].innerText)[1]

        cy.get(`[placeholder='${confirmation}']`).type(`${confirmation}{enter}`)
      })
      cy.contains('Carregando...', { timeout: 60000 }).should('not.be.visible')
    })

  // Asserts that table section is all clear
  cy.get('span').should(
    'contains.text',
    'Ao inserir uma tabela no sistema você poderá criar fluxos e treinar seus modelos a partir da sua base de dados.',
  )

在第一次测试中,由于项目中包含一些条目,因此此命令可以正常通过,但是在接下来的测试中,它将失败,因为get命令找不到任何svg作为在屏幕上查找按钮的参考。

如何更改此命令,以便在出现以下两种情况时都可以通过

  • 它删除屏幕上所有现有的条目。
  • 所有条目已被删除,它也应该通过。

1 个答案:

答案 0 :(得分:0)

您似乎需要首先检查element existence

cy.get('body').then(($body) => {
  if ($body.find('[d=...]').length) {
    // svg was found, find the buttons
    cy.get('[d=...]')
    ...the rest of your code...
  }

}