lensesio / cypress-websocket-testing

Test WebSocket connections with Cypress
Apache License 2.0
83 stars 11 forks source link

How to keep the connection open and send messages and verify responses #22

Closed mtafseerhaider closed 2 years ago

mtafseerhaider commented 3 years ago

Currently, takewhilefn option is there to do this but I have no success with it - actually it is taking a lot of and not completing any request. Please note that I doing websockets API testing - I've an endpoint and I need to first open the connection, then send a heartbeat to see if connection is alive and then send the another payload in JSON to log into the system and verify the response that I am logged in...and so on and on...

Also, I have no success currently sending a payload in JSON. Any help please?

dsebastian commented 3 years ago

Hi @mtafseerhaider , a small reproduction repository would help. From what I'm understanding you need to send multiple payloads to your server. As it stands now you can only send 1 initial message, see the property startUpMessage (it should be also present in the examples). The heartbeat is usually sent by the server to the client.

https://github.com/lensesio/cypress-websocket-testing/blob/f7e570c0ceee00ab8de445826e2338082520c965/examples/cypress/integration/examples/streamRequest.spec.ts#L41

mtafseerhaider commented 3 years ago

Hi @dsebastian Thanks for the reply. Actually, the problem I'm facing is that: I need to send a message to my server and verify the server response then again send another message ad verify the response and so on.....keeping the ws connection open. Currently , I cannot do it with this plugin as the connection closes on my first message. Is there any way please?

dsebastian commented 3 years ago

Hi @mtafseerhaider , the plugin will close the connection on first message only if you don't set the takeWhileFn. If you do, then it's probably your server which is expecting further messages from the client and closing as it's not receiving them. As mentioned, the plugin will only send a startupMessage, after it's just receiving, at least in it's current form.