簡體   English   中英

如何獲取所有元素,直到柏樹中沒有任何元素

[英]How to get all elements until there isn't any element in cypress

A 在我的 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 作為參考來找到屏幕上的按鈕。

如何更改此命令,以便在以下兩種情況下都可以傳遞

  • 它會刪除屏幕上的所有現有條目。
  • 所有條目都已被刪除,它也應該通過。

看起來您需要先檢查元素是否存在

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM