在我的一项测试中,我想等待 WebSocket STOMP 消息。 Cypress.io 可以做到这一点吗?
最佳答案
如果您要访问的 websocket 是由您的应用程序建立的,您可以遵循以下基本流程:
WebSocket
的引用测试中的实例。 WebSocket
. WebSocket
时已解决收到消息。 在没有工作应用程序的情况下,这对我来说有点难以测试,但是这样的事情应该可以工作:
在您的应用程序代码中:
// 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)
})
})
关于websocket - 如何在 Cypress.io 中等待 WebSocket STOMP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50895181/