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

时间:2019-06-28 15:24:32

标签: javascript reactjs cypress

我已经制作了一个React应用程序,该应用程序可以完美运行,现在我正在使用赛普拉斯编写一些端到端测试。

React应用程序都在相同的url上工作,没有任何路由,并且通过单击按钮处理了来自应用程序内部的api调用。

该应用程序的基础是最终用户选择一些选项,然后按过滤器以查看一些取决于所选选项的图形。

<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <distributable />
    <!-- ... -->
</web-app>

在cypress中按下该按钮时,它将运行3个api调用,这些调用将按预期返回,但是查看cypress文档没有简单的方法,除非我使用不太理想的内联cy.get('button').contains('Filter').click() ,有时它们返回的快很多,有时返回的慢一些,这取决于所选的选项。

编辑1 我尝试使用服务器和路由:

cy.wait(15000)

哪个给我错误:

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 }) 

经过进一步调查

CypressError: Timed out retrying: cy.wait() timed out waiting 5000ms for the 1st request to the route: 'one'. No request ever occurred. 更改为responseTimeout可以解决此错误。

timeout

3 个答案:

答案 0 :(得分:3)

可以使用timeout参数代替使用.wait。这样一来,如果完成速度更快,则无需等待。

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

这是选项参数in the docs here之一。

答案 1 :(得分:1)

听起来像wait for the routes。像这样:

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();

答案 2 :(得分:0)

试试这个:

    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();
   })