如何在Cypress.io中等待WebSocket Stopp消息 [英] How to wait for WebSocket STOMP messages in Cypress.io
本文介绍了如何在Cypress.io中等待WebSocket Stopp消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的一个测试中,我想等待WebSocket Stopp消息。Cypress.io是否可以做到这一点?
推荐答案
如果您要访问的WebSocket是由您的应用程序建立的,则可以遵循以下基本过程:
- 从测试内部获取对
WebSocket
实例的引用。 - 将事件侦听器附加到
WebSocket
。 - 返回
WebSocket
收到消息时解析的Cypress Promise。
这对我来说有点困难,因为没有一个可以工作的应用程序,但这样的东西应该可以工作:
在应用程序代码中:
// assuming you're using stomp-websocket: https://github.com/jmesnil/stomp-websocket
const Stomp = require('stompjs');
// bunch of app code here...
const client = Stomp.client(url);
if (window.Cypress) {
// running inside of a Cypress test, so expose this websocket globally
// so that the tests can access it
window.stompClient = client
}
在您的Cypress测试代码中:
cy.window() // yields Window of application under test
.its('stompClient') // will automatically retry until `window.stompClient` exists
.then(stompClient => {
// Cypress will wait for this Promise to resolve before continuing
return new Cypress.Promise(resolve => {
const onReceive = () => {
subscription.unsubscribe() // clean up our subscription
resolve() // resolve so Cypress continues
}
// create a new subscription on the stompClient
const subscription = stompClient.subscribe("/something/you're/waiting/for", onReceive)
})
})
这篇关于如何在Cypress.io中等待WebSocket Stopp消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文