如何等待在赛普拉斯中加载需要很长时间的弹出窗口

-1 cypress cypress-component-test-runner

我正在测试一个带有弹出窗口的 Web 应用程序表单,需要花费大量时间来加载另一个需要填写的弹出表单

目前,我等待 20 秒,这主要有效,但有时弹出窗口需要 20 秒以上才能加载,这会使我的测试脚本失败

弹出窗口是使用按钮触发的,当单击按钮时,会发出 ajax 请求,当响应到来时,会加载(渲染)弹出窗口

弹出窗口通常需要这么多时间,因为 Ajax 请求需要很长时间,所以我想创建一个动态函数来等待该弹出窗口呈现

所以我想创建一个动态等待函数,它将等待弹出窗口呈现(注意:它是弹出窗口而不是另一个页面)

代码:

    cy.get("div[class='question-btn'] a[class='btn btn-primary btn-lg cat-new-question-sortable']").click() 
    // button which triggers the popup

    cy.wait(9000)

    //multiple input, types get filled in the poup

    cy.get("input[value='Submit']").click()    // button which closes or submits the popup
    cy.wait(9500)



    cy.get("div[class='question-btn'] a[class='btn btn-primary btn-lg cat-new-question-sortable']").click() 
    // button which triggers the next popup and cycle repeats
Run Code Online (Sandbox Code Playgroud)

Tes*_*ick 5

有几个选项

元素超时而不是 wait()

在弹出窗口上的元素上添加比您当前等待时间长得多的超时

超时比 wait() 更好,因为您可以将其设置得很长(在本例中可能是 60 秒),但它只会等到元素出现为止

cy.get('button').contains('Open my popup form').click()

// Now wait for an element
cy.get('input#element-on-the-popup-form', {timeout: 60_000})
Run Code Online (Sandbox Code Playgroud)

截距

为 ajax 请求添加拦截并等待。

cy.intercept('url-of-ajax-request').as('popup')

cy.get('button').contains('Open my popup form').click()

// Now wait for the intercept alias to fire - use same timeout strategy
cy.wait('@popup', {responseTimeout: 40_000})

cy.get('input#element-on-the-popup-form')
Run Code Online (Sandbox Code Playgroud)

嘲笑回应

使用模拟响应进行拦截以消除等待时间

cy.intercept('url-of-ajax-request', {some-mock-body-for-response})
  .as('popup')

cy.get('button').contains('Open my popup form').click()

cy.wait('@popup')

cy.get('input#element-on-the-popup-form')
Run Code Online (Sandbox Code Playgroud)