gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:12:31 27 4
gpt4 key购买 nike

在我的一项测试中,我想等待 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/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com