通过单击另一个按钮上的操作等待表格行更改[重复]

问题描述 投票:0回答:1

我必须等待一个表行需要 0 到 n 秒才能显示。要显示该行,您必须单击搜索按钮。我尝试通过 do..while 循环来完成此操作,但这是不可能的。

使用的代码:

let blnFound = false
do {
cy.get(pageCommonID.btnSearch(),{timeout: Cypress.env('GLOBAL_TIMEOUT_ms'),log: True}).scrollIntoView().click({force: true,multiple: true});                
cy.get(pageWorkloadCaseID.tblResult()).find('tr').then((rows) => {
    if (rows.length != 0) {
        blnFound = true
        return true
    } else {
        actualTime = new Date().getTime()
        cy.wait('BIT_GLOBAL_TIMEOUT_MEDIUM')
        let elapsedTime = actualTime - startTime
        console.log('info', strID, 'startTime: ' + startTime)
        console.log('info', strID, 'actualTime: ' + actualTime)
        console.log('info', strID, 'Case not found in Table. Waiting...\n
                               Elapsed Time: ' + (Math.ceil(elapsedTime / 1000)) / 60 + ' Min')
        if ((Math.ceil(elapsedTime / 1000)) / 60 >= 45) {
            console.log('info', strID, 'Case not found in Table. Waiting Time was bigger than 45Min')
            throw new Error('Case not found in Table. Waiting Time was 45Min\'')
        }
    }
})
console.log('blnFound out of Then: ' + blnFound)
    if (intCount >= 360) {
        // interrupt do While
        break;
    }
    intCount++
} while (!blnFound)`

脚本不会中断,因为 blnFound 始终为 false。

当时间未预定义并且仅对按钮单击事件做出反应时,如何等待显示表格行?

我尝试使用包装、别名、将所有功能转移到其他功能、构建子方法来封装相关命令。

cypress do-while
1个回答
2
投票

看起来你想发明自己的测试框架,但实际上你可以省去很多混乱。

这是一个示例,我使用了 id 作为搜索按钮和结果表,但您可以根据需要替换其他选择器。

测试是在您输入将产生一些结果的搜索文本的基础上编写的。如果您错过了这一步,那么这并不是真正的测试。

通过定义预期结果,无需重复单击 - 您只需允许 Cypress 在预期超时内等待结果即可。

it('tests the search feature', {timeout:Cypress.env('GLOBAL_TIMEOUT_ms')}, ()=> {
  cy.get('input#search-input').type('search for me')
  cy.get('button#search-button').click()         
  cy.get('table#results tr')
    .its('length')
    .should('be.gt', 0)
})
© www.soinside.com 2019 - 2024. All rights reserved.