如何在Cypress中等待一个请求只是被发送出去,而不是等到它完成?

huangapple go评论63阅读模式
英文:

How to wait for a request just to be sent, not until it's completed in cypress?

问题

我只想等待请求被发送,不想等到请求收到响应。

截止目前,cy.intercept()cy.wait() 都会等待请求收到响应。
是否有任何方法只等待请求被发送?

例如

cy.intercept(url).as('alias')

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

cy.wait('@alias')   // 只会在响应返回时捕获
英文:

I want to wait only until a request is sent.
I don't want to wait until the request receives a response.

As of now cy.intercept() and cy.wait() wait until the request receives a response.
Is there any way to wait just until the request is sent?

For example

cy.intercept(url).as('alias')  
                                      
cy.get('button').click()

cy.wait('@alias')   // only catches when the response returns 

答案1

得分: 2

以下是翻译好的部分:

"The easiest way is to send a mock response, which by-passes waiting for the server. It's feasible if the app does not use the response."

"最简单的方法是发送一个模拟响应,这样可以绕过等待服务器的过程。如果应用程序不使用响应,则是可行的。"

"If you find the app does complain about the mock response, a stub function could be used instead."

"如果您发现应用程序对模拟响应有异议,可以改用存根函数。"

"You cannot cy.wait('@stub') on a stub alias, but you can use cy.get('@stub').should('be.called') to do the same thing."

"您不能在存根别名上使用cy.wait('@stub'),但您可以使用cy.get('@stub').should('be.called')来执行相同的操作。"

"Here's a working example."

"以下是一个可行的示例。"

"In your test you can use a shorter version, since you don't need to artificially delay the response."

"在您的测试中,您可以使用一个更简洁的版本,因为您不需要人为延迟响应。"

"cy.intercept(url, cy.stub().as('stub'))"

"cy.intercept(url, cy.stub().as('stub'))"

"trigger request"

"触发请求"

"cy.get('@stub').should('be.called')"

"cy.get('@stub').should('be.called')"

英文:

The easiest way is to send a mock response, which by-passes waiting for the server. It's feasible if the app does not use the response.

cy.intercept(url, {}).as('my-alias')  // 2nd argument is a very simple mock response 
                                      // request never gets to the server
// trigger request in app 

cy.wait('@my-alias')                  // should fire on outbound request

If you find the app does complain about the mock response, a stub function could be used instead.

You cannot cy.wait('@stub') on a stub alias, but you can use cy.get('@stub').should('be.called') to do the same thing.

Here's a working example.

const stub = cy.stub().as('stub')     // set up the stub

cy.intercept('**/my/api/items?1', (req) => {

  stub()         // call the stub as soon as the request is made 

  req.reply({
    delay: 60_000, // simulate response after 1 minute
  })
})
.as('response')    // this is the response alias

cy.window().then(win => {
  setTimeout(() => {
    win.fetch('my/api/items?1')       // fire the request after 2 seconds
  }, 2_000)
})

cy.get('@stub').should('be.called')   // wait for stub call up to 4 seconds
                                      // or add a timeout if required

// cy.wait('@response')       // response alias fails after 30 seconds

In your test you can use a shorter version, since you don't need to artificially delay the response

cy.intercept(url, cy.stub().as('stub'))  

// trigger request

cy.get('@stub').should('be.called')

huangapple
  • 本文由 发表于 2023年3月3日 22:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定