最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using Cypress how can intercept then wait for x amount of request to complete? - Stack Overflow

programmeradmin1浏览0评论

The site I'm working on makes multiple requests to api/v1/roles/{UUID}/permission-groups?includeUnlinked=true to load all the permissions that could be applied to a user.

If I use cy.intercept('api/v1/roles/*/permission-groups?includeUnlinked=true').as('permissions') then wait, Cypress continues as soon as the first request has completed. Unfortunately, not all the permissions have loaded by then, so my test fails. I'm having to hard code a 2-second wait just to make sure all the permissions have loaded, which obviously isn't ideal.

How can I make my test intercept and wait until all the permissions have loaded?

The site I'm working on makes multiple requests to api/v1/roles/{UUID}/permission-groups?includeUnlinked=true to load all the permissions that could be applied to a user.

If I use cy.intercept('api/v1/roles/*/permission-groups?includeUnlinked=true').as('permissions') then wait, Cypress continues as soon as the first request has completed. Unfortunately, not all the permissions have loaded by then, so my test fails. I'm having to hard code a 2-second wait just to make sure all the permissions have loaded, which obviously isn't ideal.

How can I make my test intercept and wait until all the permissions have loaded?

Share Improve this question asked Mar 12 at 17:13 AlexAlex 4571 gold badge6 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

Go to the browser devtools, open the Network tab, clear the display and run the test.

Count the number of permission-groups entries to figure out how many permissions there actually are loaded (it should be consistent each run), then apply the intercept wait() that many times.

Cypress._.times(5, (index) => cy.wait('@permissions'))  
// wait 5 times if there are 5 permissions

If you find that difficult, or the permissions are not applied consistently (which may be bug in itself), you can wait for network-idle.

cy.waitForNetworkIdle('/v1/api/roles/*/permission-groups?includeUnlinked=true', 1000)

although this would not appear to be necessary in this case.

发布评论

评论列表(0)

  1. 暂无评论