testplane
Version:
Tests framework based on mocha and wdio
130 lines • 5.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendFilteredEvents = exports.filterEvents = exports.installRrwebAndCollectEvents = void 0;
const fs_1 = __importDefault(require("fs"));
const events_1 = require("../../events");
const index_1 = require("./index");
const path_1 = __importDefault(require("path"));
// Built from branch https://github.com/gemini-testing/rrweb/tree/TESTPLANE-712.syntax_err
// PR: https://github.com/rrweb-io/rrweb/pull/1735
// Issue: https://github.com/rrweb-io/rrweb/issues/1734
const rrwebCode = fs_1.default.readFileSync(path_1.default.join(__dirname, "../client-scripts/rrweb-record.min.js"), "utf-8");
async function installRrwebAndCollectEvents(session, callstack) {
/* eslint-disable @typescript-eslint/ban-ts-comment */
return (0, index_1.runWithoutHistory)({ callstack }, () => session.execute((rrwebRecordFnCode, serverTime) => {
const getRealTimestamp = (fallbackTime = 0) => {
const nativeCode = "[native code]";
try {
if (Date.now.toString().includes(nativeCode)) {
return Date.now();
}
}
catch (e) {
/**/
}
try {
if (new Date().getTime.toString().includes(nativeCode)) {
return new Date().getTime();
}
}
catch (e) {
/**/
}
try {
if (new Date().valueOf.toString().includes(nativeCode)) {
return new Date().valueOf();
}
}
catch (e) {
/**/
}
try {
if (performance.now.toString().includes(nativeCode)) {
return Math.floor(performance.timeOrigin + performance.now());
}
}
catch (e) {
/**/
}
return fallbackTime;
};
try {
// @ts-expect-error
if (!window.rrweb) {
window.eval(rrwebRecordFnCode);
// @ts-expect-error
window.lastProcessedRrwebEvent = -1;
// @ts-expect-error
window.rrwebEvents = [];
// @ts-expect-error
window.rrweb.record({
// @ts-expect-error
emit(event) {
event.timestamp = getRealTimestamp(serverTime);
// @ts-expect-error
window.rrwebEvents.push(event);
},
});
// @ts-expect-error
window.rrweb.record.addCustomEvent("color-scheme-change", {
colorScheme: window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light",
});
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", event => {
// @ts-expect-error
window.rrweb.record.addCustomEvent("color-scheme-change", {
colorScheme: event.matches ? "dark" : "light",
});
});
}
}
catch (e) {
/**/
}
let result;
try {
// @ts-expect-error
result = window.rrwebEvents.slice(window.lastProcessedRrwebEvent + 1);
// @ts-expect-error
window.lastProcessedRrwebEvent = window.rrwebEvents.length - 1;
}
catch {
result = [];
}
return result;
}, rrwebCode, Date.now()));
/* eslint-enable @typescript-eslint/ban-ts-comment */
}
exports.installRrwebAndCollectEvents = installRrwebAndCollectEvents;
function filterEvents(rrwebEvents) {
return rrwebEvents.filter(e => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const dataAny = e?.data;
// cd_frame_id_ attribute is specific for chromedriver and in rare cases can be captured by rrweb
// It doesn't hold any value, and we want to get rid of it here.
return !dataAny?.attributes?.[0]?.attributes?.["cd_frame_id_"];
});
}
exports.filterEvents = filterEvents;
function sendFilteredEvents(session, rrwebEvents) {
const currentTest = session.executionContext?.ctx?.currentTest;
if (rrwebEvents.length > 0 && process.send && currentTest) {
process.send({
event: events_1.MasterEvents.DOM_SNAPSHOTS,
context: {
testPath: currentTest.titlePath(),
browserId: currentTest.browserId,
},
data: {
rrwebSnapshots: rrwebEvents,
},
});
}
}
exports.sendFilteredEvents = sendFilteredEvents;
//# sourceMappingURL=rrweb.js.map