chr*_*ris 6 timeout e2e-testing cypress
我创建了一个设置路由的测试,尝试访问一个向路由发出 API 请求的页面,然后等待路由响应:
cy
.server()
.route('GET', '/api/testing')
.as('testing');
cy.visit('/index.html', { timeout: 60000 });
cy.wait('@testing', { timeout: 60000 });
Run Code Online (Sandbox Code Playgroud)
这只会等待responseTimeout30 秒的赛普拉斯全局默认值,然后 API 请求失败。
以下是赛普拉斯在控制台中记录的错误消息:
赛普拉斯尝试向此 url 发出 http 请求时出错: https://localhost:4200/api/testing
错误是:
ESOCKET超时
堆栈跟踪是:
错误:ClientRequest
处的ESOCKETTIMEDOUT 。(...\node_modules\cypress\dist\Cypress\resources\app\packages\server\node_modules\request\request.js:778:19)
在 Object.onceWrapper (events.js:314:30)
在 emitNone (events.js :105:13)
在 ClientRequest.emit (events.js:207:7)
在 TLSSocket.emitTimeout (_http_client.js:722:34)
在 Object.onceWrapper (events.js:314:30)
在 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)
在 Timer.listOnTimeout (timers.js:264:5)
将 a 添加responseTimeout到 Cypress 的全局配置会增加超时,但为什么不是 thevisit或wait发生的超时?
请参阅本页上的代码示例命令 - 等待 - 别名
// 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
})
Run Code Online (Sandbox Code Playgroud)
我会将其添加then((xhr) =>到您的代码中,看看会收到什么响应。
从逻辑上讲,如果虚假路由等待完整超时,但“失败的合法路由”则没有,则在超时期限内从服务器发回带有失败代码的响应。
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)
}
})
}
Run Code Online (Sandbox Code Playgroud)
这可能是您想要测试的条件(即连接在响应中中断)。
不幸的是,似乎没有语法cy.wait().catch(),请参阅Commands-Are-Not-Promises
您无法将 .catch 错误处理程序添加到失败的命令中。
您可能想尝试对路由进行存根,而不是在服务器上设置断点,但我不确定假响应应该采取什么形式。(带有存根的参考路线)