英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论