diff --git a/src/tests/startRecording.js b/src/tests/startRecording.js index 4443232..c5aca95 100644 --- a/src/tests/startRecording.js +++ b/src/tests/startRecording.js @@ -1,4 +1,5 @@ const { validate } = require("doc-detective-common"); +const { log } = require("../utils"); const { instantiateCursor } = require("./moveTo"); const path = require("path"); const fs = require("fs"); @@ -99,8 +100,9 @@ async function startRecording({ config, context, step, driver }) { await driver.execute(() => (document.title = "RECORDER")); config.recording.tab = await driver.getWindowHandle(); - // Start recording - const recorder = await driver.execute((baseName) => { + // Start recording using executeAsync so we properly wait for + // getDisplayMedia() to resolve before switching tabs. + const recorderStarted = await driver.executeAsync((baseName, done) => { let stream; let recorder; const displayMediaOptions = { @@ -131,6 +133,8 @@ async function startRecording({ config, context, step, driver }) { stream = await startCapture(displayMediaOptions); if (stream) { await recordStream(stream); + } else { + done(false); } return stream; } @@ -141,6 +145,10 @@ async function startRecording({ config, context, step, driver }) { window.recorder.ondataavailable = (event) => data.push(event.data); window.recorder.start(); + // Signal that recording has started successfully. + // executeAsync resolves here; the rest continues in the browser. + done(true); + let stopped = new Promise((resolve, reject) => { window.recorder.onstop = resolve; window.recorder.onerror = (event) => reject(event.name); @@ -163,6 +171,24 @@ async function startRecording({ config, context, step, driver }) { } captureAndDownload(); }, baseName); + + if (!recorderStarted) { + config.recording = null; + result.status = "FAIL"; + result.description = + "Failed to start recording. getDisplayMedia may have been rejected. " + + "On macOS, ensure Chrome has screen recording permission in " + + "System Preferences > Privacy & Security > Screen Recording."; + log(config, "error", result.description); + // Clean up: close the recorder tab and switch back + await driver.closeWindow(); + await driver.switchToWindow(originalTab); + await driver.execute((documentTitle) => { + document.title = documentTitle; + }, documentTitle); + return result; + } + // Switch to original tab await driver.switchToWindow(originalTab); // Set document title back to original diff --git a/src/tests/stopRecording.js b/src/tests/stopRecording.js index 81e589a..7dc5473 100644 --- a/src/tests/stopRecording.js +++ b/src/tests/stopRecording.js @@ -36,6 +36,18 @@ async function stopRecording({ config, step, driver }) { // Switch to recording tab await driver.switchToWindow(config.recording.tab); + // Verify the recorder was properly initialized + const recorderExists = await driver.execute(() => { + return typeof window.recorder !== "undefined" && window.recorder !== null; + }); + if (!recorderExists) { + result.status = "FAIL"; + result.description = + "Recording was not properly started. The recorder object doesn't exist in the browser context."; + await driver.closeWindow(); + config.recording = null; + return result; + } // Stop recording await driver.execute(() => { window.recorder.stop();