В одном из моих тестов я хочу дождаться сообщений WebSocket STOMP. Возможно ли это с Cypress.io?
Как дождаться сообщений WebSocket STOMP в Cypress.io
Ответы (1)
Если веб-сокет, к которому вы хотите получить доступ, устанавливается вашим приложением, вы можете выполнить этот базовый процесс:
- Получите ссылку на экземпляр
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)
})
})
person
Zach Bloomquist
schedule
04.04.2019