gpt4 book ai didi

timeout - Cypress 访问和等待超时被忽略

转载 作者:行者123 更新时间:2023-12-04 21:04:36 29 4
gpt4 key购买 nike

我创建了一个测试,在其中设置了路由,尝试访问向路由发出 API 请求的页面,然后等待路由响应:

cy
.server()
.route('GET', '/api/testing')
.as('testing');
cy.visit('/index.html', { timeout: 60000 });
cy.wait('@testing', { timeout: 60000 });

这只等待 Cypress 全局默认值 responseTimeout 30 秒,然后 API 请求失败。

这是 Cypress 在控制台中记录的错误消息:

Cypress errored attempting to make an http request to this url: https://localhost:4200/api/testing

The error was:

ESOCKETTIMEDOUT

The stack trace was:

Error: ESOCKETTIMEDOUT
at ClientRequest. (…\node_modules\cypress\dist\Cypress\resources\app\packages\server\node_modules\request\request.js:778:19)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:105:13)
at ClientRequest.emit (events.js:207:7)
at TLSSocket.emitTimeout (_http_client.js:722:34)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:105:13)
at TLSSocket.emit (events.js:207:7)
at TLSSocket.Socket._onTimeout (net.js:402:8) at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5)



添加 responseTimeout到 Cypress 的全局配置会增加超时,但是为什么 visit 的超时不是或 wait发生?

最佳答案

请参阅此页面上的代码示例 commands - wait - Alias

// Wait for the route aliased as 'getAccount' to respond
// without changing or stubbing its response
cy.server()
cy.route('/accounts/*').as('getAccount')
cy.visit('/accounts/123')
cy.wait('@getAccount').then((xhr) => {
// we can now access the low level xhr
// that contains the request body,
// response body, status, etc
})

我会添加 then((xhr) =>到你的代码,看看有什么 react 。

逻辑上说,如果虚假路由等待完全超时,但“失败的合法路由”没有,则在超时期限内从服务器发回带有故障代码的响应。

request.js 中错误来自的代码块有一个有趣的注释。

self.req.on('socket', function(socket) {
var setReqTimeout = function() {
// This timeout sets the amount of time to wait *between* bytes sent
// from the server once connected.
//
// In particular, it's useful for erroring if the server fails to send
// data halfway through streaming a response.
self.req.setTimeout(timeout, function () {
if (self.req) {
self.abort()
var e = new Error('ESOCKETTIMEDOUT') <-- LINE 778 REFERENCED IN MESSAGE
e.code = 'ESOCKETTIMEDOUT'
e.connect = false
self.emit('error', e)
}
})
}

这可能是您要测试的条件(即连接中断中间响应)。
不幸的是,似乎没有语法 cy.wait().catch() ,见 Commands-Are-Not-Promises

You cannot add a .catch error handler to a failed command.



您可能想尝试对路由进行 stub ,而不是在服务器上设置断点,但我不确定假响应应该采用什么形式。 (引用 route with stubbing )

关于timeout - Cypress 访问和等待超时被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580314/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com