webdriverio/webdriverio

Mock websocket connection similar to http

Ouverte

#5 911 ouverte le 1 oct. 2020

 (5 commentaires) (14 réactions) (0 personne assignée)JavaScript (1 793 forks)batch import
Enhancementhelp wanted

Métriques du dépôt

Stars
 (6 029 étoiles)
Métriques de merge PR
 (Merge moyen 15j 15h) (48 PRs mergées en 30 j)

Description

Is your feature request related to a problem? Please describe. I'd like to be able to mock WebSocket communication in a similar way it's done for HTTP

Describe the solution you'd like

// mock should be defined before the page is opened!
const wsMock = browser.mockWs('wss://*/sockjs/**/websocket', { 
 message: (message) => message.includes('foobar')
})

wsMock.respond('rawDate')
// or
wsMock.respond((message) => JSON.stringify({ 
  ...JSON.parse(message),
  _id: 'myid'
}))

Add other available mock functions as well.

Describe alternatives you've considered Any ideas on how to mock and assert WebSocket messages are welcomed.

Additional context

I believe it should be better to have a separate function for WebSocket mocking not to overcomplicate things. Might be helpful https://stackoverflow.com/a/54110660/2475987

Guide contributeur