{"record":{"id":"3de4b64f4cf02de1","repo":"microsoft/playwright","slug":"cannot-send-a-message-before-connecting-to-the-ser","errorCode":null,"errorMessage":"Cannot send a message before connecting to the server","messagePattern":"Cannot send a message before connecting to the server","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/injected/src/webSocketMock.ts","lineNumber":258,"sourceCode":"    _apiEnsureOpened() {\n      // This is called at the end of the route handler. If we did not connect to the server,\n      // assume that websocket will be fully mocked. In this case, pretend that server\n      // connection is established right away.\n      if (!this._ws)\n        this._ensureOpened();\n    }\n\n    _apiSendToPage(message: WebSocketMessage) {\n      // Calling \"sendToPage()\" from the route handler. Allow this for easier testing.\n      this._ensureOpened();\n      if (this.readyState !== WebSocketMock.OPEN)\n        throw new DOMException(`WebSocket is already in CLOSING or CLOSED state.`);\n      this.dispatchEvent(new MessageEvent('message', { data: message, origin: this._origin, cancelable: true }));\n    }\n\n    _apiSendToServer(message: WebSocketMessage) {\n      if (!this._ws)\n        throw new Error('Cannot send a message before connecting to the server');\n      if (this._ws.readyState === WebSocketMock.CONNECTING)\n        this._wsBufferedMessages.push(message);\n      else\n        this._ws.send(message);\n    }\n\n    _apiConnect() {\n      if (this._ws)\n        throw new Error('Can only connect to the server once');\n\n      this._ws = new NativeWebSocket(this.url, this._protocols);\n      this._ws.binaryType = this._binaryType;\n\n      this._ws.onopen = () => {\n        for (const message of this._wsBufferedMessages)\n          this._ws!.send(message);\n        this._wsBufferedMessages = [];\n        this._ensureOpened();","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/injected/src/webSocketMock.ts#L240-L276","documentation":"Error \"Cannot send a message before connecting to the server\" thrown in microsoft/playwright.","triggerScenarios":"Thrown at packages/injected/src/webSocketMock.ts:258 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}