如何在Cypress.io中等待WebSocket STOMP消息

问题描述 投票:4回答:1

在我的一个测试中,我想等待WebSocket STOMP消息。 Cypress.io有可能吗?

websocket stomp cypress
1个回答
1
投票

如果您要访问的websocket是由您的应用程序建立的,则可以遵循以下基本过程:

  1. 从测试中获取对WebSocket实例的引用。
  2. 将事件监听器附加到WebSocket
  3. 返回当你的Cypress Promise收到消息时解决的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
}

在您的赛普拉斯测试代码中:

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)
  })
})

© www.soinside.com 2019 - 2024. All rights reserved.