【发布时间】:2019-09-09 20:25:38
【问题描述】:
我想测试我的“等待服务器回复”用户界面行为。如何可靠地做到这一点不会因硬编码延迟而暂停测试?
比如说,我有一个触发 http 请求的按钮,并且必须显示某些动画/动作,直到响应到达。
一个愚蠢的工作方法是:
cy.route({
delay: 1000,
response: "blah blah",
})
// triggger submission
cy.get('#my-submit-button').click()
// will disappear upon receiving response
cy.contains('Waiting for response...')
我很确定“等待”文本会在响应暂停时在一秒钟内出现,但随后我提交了the sin of pausing the test for a whole second。
如果我开始缩短或删除delay,那么我将面临创建片状测试的风险,因为在我检查是否存在“等待...”文本之前,有可能会处理响应,这到那时就会被删除。
有没有办法确保只有在检查“Waiting...”文本之后才产生响应,而不会出现硬延迟?
我天真地尝试从路由的 onResponse 中进行 cypress 断言,但 cypress 对此并不满意:
cy.route({
onResponse: xfr => {
cy.contains('Waiting for response...')
return xfr
},
response: "blah blah",
})
cy.get('#my-submit-button').click()
产生https://on.cypress.io/returning-promise-and-commands-in-another-command 错误:
Error: Uncaught CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> cy.click()
The cy command you invoked inside the promise was:
> cy.contains()
Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.
Cypress will resolve your command with whatever the final Cypress command yields.
The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.
【问题讨论】:
标签: cypress