WebSocket
Connection Close Codes
SubscribeValidateEasy
Description
Handle different WebSocket close codes (1000, 1001, 1006) and verify proper cleanup.
Test Scenario
// Test: Graceful close with code 1000
const ws = new WebSocket('wss://echo.websocket.org');
await waitFor(() => ws.readyState === WebSocket.OPEN);
let closeCode = 0;
ws.onclose = (e) => { closeCode = e.code; };
ws.close(1000, 'Normal closure');
await waitFor(() => ws.readyState === WebSocket.CLOSED);
expect(closeCode).toBe(1000);Testing Tips
- •Close code 1000 = normal closure
- •Close code 1001 = going away
- •Close code 1006 = abnormal closure
// Playwright API Testing - WebSocketimport { test, expect } from '@playwright/test';test('WebSocket connection', async ({ page }) => {// Listen for WebSocket connectionsconst wsPromise = page.waitForEvent('websocket');await page.goto('/websocket-demo');const ws = await wsPromise;expect(ws.url()).toContain('ws://');// Wait for messageconst framePromise = ws.waitForEvent('framereceived');const frame = await framePromise;expect(frame.payload).toBeDefined();});
Challenge ID: ws-close-codes
2 frameworks available