Back to Blog

Testing WebSockets and Real-Time Features

By MarcoApril 1, 202411 min read

Strategies for testing WebSocket connections and real-time application features.

WebSocketsReal-TimeAdvanced

Testing real-time features requires different approaches than traditional HTTP-based testing. WebSockets present unique challenges.

test('websocket connection', async ({ page }) => {
  let messages: string[] = [];

  page.on('websocket', ws => {
    ws.on('framereceived', event => {
      messages.push(event.payload);
    });
  });

  await page.goto('/chat');
  await page.fill('[data-testid="message"]', 'Hello');
  await page.click('[data-testid="send"]');

  await expect.poll(() => messages).toContain('Hello');
});