@dhlab/e2e-autogen
Version:
Google 스프레드시트 기반 시나리오를 Playwright 테스트 스텁 코드로 자동 생성하고, 테스트 실행 결과를 다시 시트에 업데이트하는 CLI 도구
82 lines (80 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordingTest = void 0;
const tslib_1 = require("tslib");
/// <reference lib="dom" />
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const test_1 = require("@playwright/test");
const recordingTest = test_1.test.extend({
// 로컬 오디오 파일 경로 (없으면 사인파)
audioFilePath: [undefined, { option: true }],
setupRecording: async ({ page, audioFilePath }, use) => {
await use(async () => {
if (audioFilePath) {
const fileUrl = `/_e2e-audio_/${node_path_1.default.basename(audioFilePath)}`;
await mockMediaDevices(page, { fileUrl, filePath: audioFilePath });
}
else {
await mockMediaDevices(page);
}
});
},
});
exports.recordingTest = recordingTest;
const mockMediaDevices = async (page, { enabled = true, fileUrl, filePath, } = {}) => {
// 옵션에 따라 모킹 스킵
if (!enabled)
return;
// 마이크 권한 허용 - Chrome/Chromium인 경우만
const browserName = page.context().browser()?.browserType().name();
if (browserName === "chromium") {
await page.context().grantPermissions(["microphone"]);
}
// 오디오 파일 라우팅 설정 (요청된 URL을 로컬 파일로 응답)
if (fileUrl && filePath) {
await page.route("**/_e2e-audio_/*", async (route) => {
try {
const requested = new URL(route.request().url());
const requestedBase = node_path_1.default.basename(requested.pathname);
const expectedBase = node_path_1.default.basename(fileUrl);
if (requestedBase === expectedBase) {
await route.fulfill({
path: filePath,
headers: { "content-type": "audio/wav" },
});
return;
}
}
catch { }
await route.fallback();
});
}
await page.evaluate(async (url) => {
navigator.mediaDevices.getUserMedia = async () => {
const audioContext = new AudioContext();
const destination = audioContext.createMediaStreamDestination();
if (url) {
// 외부 WAV 파일을 fetch 후 단발 재생
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
const bufferSource = audioContext.createBufferSource();
bufferSource.buffer = audioBuffer;
bufferSource.connect(destination);
bufferSource.start(0); // loop 기본 false -> 1회 재생
}
else {
// 사인파 모킹 (기존 로직)
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.frequency.setValueAtTime(440, audioContext.currentTime);
oscillator.type = "sine";
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime);
oscillator.connect(gainNode);
gainNode.connect(destination);
oscillator.start();
}
return destination.stream;
};
}, fileUrl);
};