【问题标题】:How do you wait on multiple XHR in Cypress that match the same intercept您如何等待赛普拉斯中匹配相同截距的多个 XHR
【发布时间】:2021-10-14 13:46:46
【问题描述】:

在我的应用程序中,从干净状态登录后,会触发一系列同步查询以确保更新本地数据。发生这种情况时会出现一个加载屏幕。在执行测试之前,我只需要 cypress 等待所有这些调用完成。

cy.intercept() 正在识别呼叫,但 cy.wait() 只等待第一个呼叫完成。

有没有办法动态创建别名或让应用程序等待微调器消失?

describe('Navigation', function () {
beforeEach(function () {
    // Programmatically login via Amazon Cognito API
    cy.intercept('POST', '**/graphql').as('dataStore');
    cy.loginByCognitoApi(Cypress.env('cognito_username'), Cypress.env('cognito_password'));
    cy.wait(['@dataStore']);
});

it('shows logged in', function () {
    cy.get('[data-test=logo]').should('be.visible');
});

【问题讨论】:

标签: cypress


【解决方案1】:

您可以在单个拦截时重复等待,因此计算橙色 dataStore 标记的数量(看起来像 11)并等待该次数

cy.intercept('POST', '**/graphql').as('dataStore');
cy.loginByCognitoApi(Cypress.env('cognito_username'), Cypress.env('cognito_password'));
Cypress._.times(11, () => {
  cy.wait('@dataStore')
})

或者它可能是 10 - 查看路线定义。无论如何,实验。应用程序的调用应保持一致。

【讨论】:

  • 有趣的解决方案。在我的特定情况下,呼叫数量并不总是相同。我确实在 cypress GitHub github.com/cypress-io/cypress/issues/17819 中遇到过这个未解决的问题
  • 日志末尾的 (new url) http://localhost/snashots 是否标志着 XHR 调用的结束?
  • http://localhost/snapshots 在加载完成后标记重定向,我最终等待该重定向。它有 90% 的效率,但如果碰巧连接速度较慢,则测试会失败。 cy.url({ timeout: 20000 }).should('contain', '/snapshots');
【解决方案2】:

我也遇到过类似的情况。我所做的是将一组对象存储在不同的文件中,每个对象代表一个特定的测试场景。这样您就可以遍历测试用例并动态分配别名。

所以你可以这样做:

beforeEach(function () {
   yourArray.forEach((testcase) => {
    cy.intercept('POST', '**/graphql').as(`${testcase.testname}datastore`);
    cy.loginByCognitoApi(Cypress.env('cognito_username'), 
    Cypress.env('cognito_password'));
    cy.wait(`@${testcase.testname}datastore`);
}
    
});

【讨论】:

    【解决方案3】:

    如果请求的数量不一致,我做了以下事情(我已经把它放在一个命令中以在多个地方使用):

    cy.intercept('POST', '**/graphql').as('dataStore');
    cy.loginByCognitoApi(Cypress.env('cognito_username'),Cypress.env('cognito_password'));
    cy.get('@dataStore.all').then(xhrs => cy.wait(Array(xhrs.length).fill('@dataStore')));
    

    使用“all”对别名进行等待会返回赛普拉斯在创建别名后看到的对别名路由的所有调用。

    【讨论】:

    • 注意 - 可以在所有请求完成之前调用cy.get('@dataStore.all')
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 2020-05-30
    相关资源
    最近更新 更多