在Cypress测试中,如果预期的XHR请求没有发出,我如何重试按钮单击:waitUntil()WITH CLICK XHR Condition?? [英] In Cypress tests, how do I retry a button click if an expected XHR request does not go out : waitUntil() with click XHR condition??

查看:52
本文介绍了在Cypress测试中,如果预期的XHR请求没有发出,我如何重试按钮单击:waitUntil()WITH CLICK XHR Condition??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在非常高的级别上,我们单击一个按钮来控制建筑控制点;打开或关闭一盏灯。点击应该向服务器发送POST请求。问题是有时,按钮被单击,POST请求不发出。该按钮没有指示是否已被单击的功能(次要增强)。

目前,我希望使用Cypress插件waitUntil()解决此问题。

    // define routes
    cy.server();
    cy.route('POST', '**/pointcommands').as('sendCommand');

    // setup checkFunction for cy.waitUntil()
    const waitForPost200 = () => cy.wait('@sendCommand', {timeout: 10000}).then(xhr => cy.wrap(xhr).its('status').should('eq', 200));

    // setup jQuery for cy.pipe()
    const click = $el => $el.click();

    // click the button
    cy.get('.marengo-ok')
      .should('be.visible')
      .pipe(click);
      // need to pass in a synchronous should() check so that the click is retried. How can we achieve this with waitUntil ?

    // wait until checkFunction waitForPost200() returns truthy
    cy.waitUntil( (): any => waitForPost200(), {
      timeout: 10000,
      interval: 1000,
      errorMsg: 'POST not sent `enter code here`within time limit'
    });

推荐答案

据我所知,无法在失败时利用cy.wait('@sendCommand')命令恢复测试(如果未进行xhr调用)。

我想到的最快的解决方案如下:

let requestStarted = false; // will be set to true when the XHR request starts
cy.route({
  method: 'POST',
  url: '**/pointcommands',
  onRequest: () => (requestStarted = true)
}).as('sendCommand');

cy.get(".marengo-ok").as("button");

cy.waitUntil(() =>
    cy
    .get("@button")
    .should('be.visible')
    .click()
    .then(() => requestStarted === true)
, {
  timeout: 10000,
  interval: 1000,
  errorMsg: 'POST not sent `enter code here`within time limit'
});

cy.wait("@sendCommand");

您可以玩a repo我为您准备的模拟您情况的地方。这是我回购中测试的最终结果。

如果您需要更多帮助,请告诉我😊

这篇关于在Cypress测试中,如果预期的XHR请求没有发出,我如何重试按钮单击:waitUntil()WITH CLICK XHR Condition??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆