Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/tests/startRecording.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/tests/stopRecording.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down