一直单击"刷新"按钮,直到出现数据为止



我有一个页面用于上传文件,该文件将由服务器在后台处理。然后我有第二个页面,它只显示已处理的文件,这可能需要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并检查具有我的文件名的行是否存在。

如果是,则继续。否则

  1. 暂停1秒
  2. 单击特定按钮(在页面上重新加载数据(
  3. 等待.businy微调器不存在(数据已重新加载(
  4. 检查行是否存在

如果通过,则循环,但最长30秒。

这种模式将在许多地方重复,实现这一点的最佳方式是什么?

你能等一下文件名吗?

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?

您可以创建一个递归函数,在该函数中,您可以通过重新加载页面最多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()

最新更新