不断单击刷新按钮,直到出现数据

k5ifujac  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(335)

我有一个页面用于上传文件,该文件将由服务器在后台处理。然后我有了第二个页面,其中只显示已处理的文件,这可能需要5秒钟的时间。
目前我的代码就是这样的

cy.visit('/')
        cy.get('.busy-spinner').should('not.exist', { timeout: 10000 })
        cy.contains('Submit file').click()
        cy.get('[id="requestinputFile"]').attachFile('test-file.txt');
        cy.contains('Upload').click()
        cy.contains('.notifications', 'Your file has been uploaded', { timeout: 10000 })
        cy.wait(5000)
        cy.visit('/processed-files')
        cy.get('[data-render-row-index="1"] > [data-col-index="1"]').contains(filename)

有时等待的时间太长,有时不够长。我想做的是去 /processed-files 立即检查文件名为my的行是否存在。
如果确实如此,则继续。否则
暂停1秒钟
单击特定按钮(重新加载页面上的数据)
等待。忙碌微调器不存在(数据已重新加载)
检查该行是否存在
如果通过,则循环-但最长持续30秒。
这种模式将在许多地方重复,实现这一点的最佳方式是什么?

70gysomp

70gysomp1#

你能等一下文件名吗?

cy.contains('[data-render-row-index="1"] > [data-col-index="1"]', filename, 
  { timeout: 30_000 }
)

如果需要重新加载以获得正确的行条目,则可以使用重复函数

function refreshForData(filename, attempt = 0) {

  if (attempt > 30 ) {            // 30 seconds with a 1s wait below
    throw 'File did not appear'
  }

  // Synchronous check so as not to fail
  const found = Cypress.$(`[data-render-row-index="1"] > [data-col-index="1"]:contains('${filename}')`)

  if (!found) {
    cy.wait(1_000)
    cy.get('Reload button').click()
    cy.get('Spinner').should('not.be.visible')
    refreshForData(filename, ++attempt)
  }
}

refreshForData(filename)  // pass in filename, function can be globalized
                          // maybe also pass in selector?
ecfsfe2w

ecfsfe2w2#

您可以创建一个递归函数,在该函数中,您可以通过重新加载页面最多30秒来检查文件名的存在。如果找到文件名,则退出该函数。

let retry = 0

function isElementVisible() {
    if (retry < 15 && Cypress.$('[data-render-row-index="1"] > [data-col-index="1"]'):not(:contains('filename'))) {

        //Increment retry
        retry++

        //wait 2 seconds
        cy.wait(2000)

        //Reload Page by clicking button
        cy.click('button')

        //Check busy spinner is first visible and then not visible
        cy.get('.busy-spinner').should('be.visible')
        cy.get('.busy-spinner').should('not.be.visible')

        //Call the recursive function again
        isElementVisible()

    } else if (retry < 15 && Cypress.$('[data-render-row-index="1"] > [data-col-index="1"]'):contains('filename')) {
        //Row found Do something
        return

    } else {
        //It exceeded the required no. of retries and a max of 30 seconds wait
        return
    }
}

//Trigger the recursive function
isElementVisible()

相关问题