diff --git a/src/main/browser/cdp-screenshot.test.ts b/src/main/browser/cdp-screenshot.test.ts index 367842a7234..97859f7297f 100644 --- a/src/main/browser/cdp-screenshot.test.ts +++ b/src/main/browser/cdp-screenshot.test.ts @@ -190,6 +190,28 @@ describe('captureScreenshot', () => { ) }) + it('reports the original timeout when fallback encoding fails', async () => { + vi.useFakeTimers() + + const webContents = createMockWebContents() + webContents.debugger.sendCommand.mockImplementation(() => new Promise(() => {})) + webContents.capturePage.mockResolvedValueOnce({ + isEmpty: () => { + throw new Error('native image unavailable') + } + }) + const onResult = vi.fn() + const onError = vi.fn() + + captureScreenshot(webContents as never, { format: 'png' }, onResult, onError) + await vi.advanceTimersByTimeAsync(8000) + + expect(onResult).not.toHaveBeenCalled() + expect(onError).toHaveBeenCalledWith( + 'Screenshot timed out — the browser tab may not be visible or the window may not have focus.' + ) + }) + it('reports the timeout when both CDP and fallback capture stall', async () => { vi.useFakeTimers() diff --git a/src/main/browser/cdp-screenshot.ts b/src/main/browser/cdp-screenshot.ts index aeb0ea57338..639db887289 100644 --- a/src/main/browser/cdp-screenshot.ts +++ b/src/main/browser/cdp-screenshot.ts @@ -268,7 +268,13 @@ export function captureScreenshot( clearTimeout(fallbackTimer) fallbackTimer = null } - const fallback = encodeNativeImageScreenshot(image, params) + let fallback: { data: string } | null = null + try { + fallback = encodeNativeImageScreenshot(image, params) + } catch { + settleError(SCREENSHOT_TIMEOUT_MESSAGE) + return + } if (fallback) { settleResult(fallback) return