简体   繁体   English

单击按钮后,赛普拉斯等待 API

[英]Cypress wait for API after button click

I've made a React app, which all works perfectly and I'm now writing some end to end tests using Cypress.我制作了一个 React 应用程序,一切都运行良好,我现在正在使用 Cypress 编写一些端到端测试。

The React app all works on the same url, it's not got any routes, and api calls from inside the app are handled through button clicks. React 应用程序都在同一个 url 上工作,它没有任何路由,并且来自应用程序内部的 api 调用是通过单击按钮处理的。

The basis of the app is the end user selects some options, then presses filter to view some graphs that are dependant on the selected options.该应用程序的基础是最终用户选择一些选项,然后按过滤器查看一些依赖于所选选项的图表。

cy.get('button').contains('Filter').click()

When the button is pressed in cypress, it runs the 3 api calls which return as expected, but looking over the cypress docs there is no easy way unless I use inline cy.wait(15000) which isn't ideal, as sometimes they return a lot faster, and sometimes they return slower, depending on the selected options.当在柏树中按下按钮时,它会运行 3 api 调用,这些调用按预期返回,但是查看柏树文档没有简单的方法,除非我使用内联cy.wait(15000)这并不理想,因为有时它们会返回快很多,有时它们返回的速度较慢,具体取决于所选的选项。

Edit 1 I've tried using server and route:编辑 1我试过使用服务器和路由:

cy.server({ method: 'GET' });
cy.route('/endpoint1*').as('one')
cy.route('/endpoint2*').as('two')
cy.route('/endpoint3*').as('three')
cy.get('button').contains('Filter').click()
cy.wait(['@one', '@two', '@three'], { responseTimeout: 15000 }) 

Which gives me the error:这给了我错误:

CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'one'. No request ever occurred.

After further investigation经过进一步调查

Changing from responseTimeout to just timeout fixed the error.responseTimeout更改为 just timeout修复了错误。

cy.server({ method: 'GET' });
cy.route('/endpoint1*').as('one')
cy.route('/endpoint2*').as('two')
cy.route('/endpoint3*').as('three')
cy.get('button').contains('Filter').click()
cy.wait(['@one', '@two', '@three'], { timeout: 15000 }).then(xhr => {
  // Do what you want with the xhr object
}) 

Sounds like you'll want to wait for the routes .听起来你会想要等待路线 Something like this:像这样的东西:

cy.server();
cy.route('GET', '/api/route1').as('route1');
cy.route('GET', '/api/route2').as('route2');
cy.route('GET', '/api/route3').as('route3');

cy.get('button').contains('Filter').click();

// setting timeout because you mentioned it can take up to 15 seconds.
cy.wait(['@route1', '@route2', 'route3'], { responseTimeout: 15000 });

// This won't execute until all three API calls have returned
cy.get('#something').click();

Rather than using a .wait you can use a timeout parameter.您可以使用超时参数,而不是使用.wait That way if it finished faster, you don't have to wait.这样,如果它完成得更快,您就不必等待。

cy.get('button').contains('Filter', {timeout: 15000}).click()

This is mentioned as one of the options parameters in the official docs here .在此处的官方文档中被称为选项参数之一

cy.server() and cy.route() are deprecated in Cypress 6.0.0 In a future release, support for cy.server() and cy.route() will be removed. cy.server()cy.route()Cypress 6.0.0中被弃用 在未来的版本中,将删除对cy.server()cy.route()的支持。 Consider using cy.intercept() instead.考虑改用cy.intercept()

this worked for me...这对我有用...

ex:- see the screen shot例如:-看屏幕截图

    cy.intercept('GET', 'http://localhost:4001/meta').as('route');
    cy.get(':nth-child(2) > .nav-link').click();
    cy.contains('Filter');
    cy.wait(['@route'], { responseTimeout: 15000 });

Try this:尝试这个:

    cy.contains('button', 'Save').click();
    cy.get('[data-ng-show="user.manage"]', { timeout: 10000 }).should('be.visible').then(() => {
      `cy.get('[data-ng-show="user.manage"]').click();
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM