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

标签 websocket stomp cypress

在我的一项测试中,我想等待 WebSocket STOMP 消息。 Cypress.io 可以做到这一点吗?

最佳答案

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

  • 获取对 WebSocket 的引用测试中的实例。
  • 将事件监听器附加到 WebSocket .
  • 返回 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
    }
    

    在您的 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/

    相关文章:

    javascript - 有福服务器(Node.js)通过 websocket 到浏览器中的 Xterm.js 客户端

    javascript - 使用 Socket.io 的套接字的多个实例

    c++ - Boost膨胀算法解压缩

    http - 净/http : concurrency and message passing between coroutines

    java - 无法在 JMS 中正确创建消息选择器

    java - 我需要一个使用 ssl 的 java stomp 客户端库

    c# - 开始了解 Web 套接字和 http 请求(和 STOMP)

    arrays - Cypress - 查找多个同名元素

    javascript - Cypress 并不总是执行点击元素

    javascript - 如何验证 Cypress 中的错误消息?