testplane
Version:
Tests framework based on mocha and wdio
97 lines • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startSelectivity = exports.updateSelectivityHashes = void 0;
const css_selectivity_1 = require("./css-selectivity");
const js_selectivity_1 = require("./js-selectivity");
const test_dependencies_writer_1 = require("./test-dependencies-writer");
const utils_1 = require("./utils");
const hash_writer_1 = require("./hash-writer");
const types_1 = require("./types");
const testplane_selectivity_1 = require("./testplane-selectivity");
const hash_reader_1 = require("./hash-reader");
const events_1 = require("../../../events");
/**
* Called at the end of successfull testplane run
* Not using "Promise.all" here because all hashes are already calculated and cached at the start
*/
const updateSelectivityHashes = async (config) => {
const browserIds = config.getBrowserIds();
for (const browserId of browserIds) {
const browserConfig = config.forBrowser(browserId);
const { enabled, testDependenciesPath, compression, disableSelectivityPatterns } = browserConfig.selectivity;
if (!enabled) {
continue;
}
const hashReader = (0, hash_reader_1.getHashReader)(testDependenciesPath, compression);
const hashWriter = (0, hash_writer_1.getHashWriter)(testDependenciesPath, compression);
for (const pattern of disableSelectivityPatterns) {
const isChanged = await hashReader.patternHasChanged(pattern);
if (isChanged) {
hashWriter.addPatternDependencyHash(pattern);
}
}
await hashWriter.commit();
}
};
exports.updateSelectivityHashes = updateSelectivityHashes;
const startSelectivity = async (browser) => {
const { enabled, compression, sourceRoot, testDependenciesPath, mapDependencyRelativePath } = browser.config.selectivity;
if (!enabled || !browser.publicAPI.isChromium) {
return () => Promise.resolve();
}
if (compression === types_1.Compression.ZSTD && !process.versions.zstd) {
throw new Error("Selectivity: Compression 'zstd' is not supported in your node version. Please, upgrade the node version to 22");
}
if (!browser.cdp) {
throw new Error("Selectivity: Devtools connection is not established, couldn't record selectivity without it");
}
const cdpTaget = browser.cdp.target;
const handle = await browser.publicAPI.getWindowHandle();
const { targetInfos } = await cdpTaget.getTargets();
const cdpTargetId = targetInfos.find(t => handle.includes(t.targetId))?.targetId;
if (!cdpTargetId) {
throw new Error([
"Selectivity: Couldn't find current page;",
`\n\t- webdriver handle: ${handle}`,
`\n\t- cdp targets: ${targetInfos.map(t => `"${t.targetId}"`).join(", ")}`,
].join(""));
}
const sessionId = await cdpTaget.attachToTarget(cdpTargetId).then(r => r.sessionId);
const cssSelectivity = new css_selectivity_1.CSSSelectivity(browser.cdp, sessionId, sourceRoot);
const jsSelectivity = new js_selectivity_1.JSSelectivity(browser.cdp, sessionId, sourceRoot);
await Promise.all([cssSelectivity.start(), jsSelectivity.start()]);
/** @param drop only performs cleanup without writing anything. Should be "true" if test is failed */
return async function stopSelectivity(test, drop) {
const [cssDependencies, jsDependencies] = await Promise.all([
cssSelectivity.stop(drop),
jsSelectivity.stop(drop),
]);
cdpTaget.detachFromTarget(sessionId).catch(() => { });
if (drop || (!cssDependencies?.size && !jsDependencies?.size)) {
return;
}
const mapBrowserDepsRelativePath = mapDependencyRelativePath
? (relativePath) => mapDependencyRelativePath({ scope: "browser", relativePath })
: null;
const mapTestplaneDepsRelativePath = mapDependencyRelativePath
? (relativePath) => mapDependencyRelativePath({ scope: "testplane", relativePath })
: null;
const testDependencyWriter = (0, test_dependencies_writer_1.getTestDependenciesWriter)(testDependenciesPath, compression);
const browserDeps = (0, utils_1.transformSourceDependencies)(cssDependencies, jsDependencies, mapBrowserDepsRelativePath);
const testplaneDeps = (0, utils_1.transformSourceDependencies)(null, (0, testplane_selectivity_1.getCollectedTestplaneDependencies)(), mapTestplaneDepsRelativePath);
process.send?.({
event: events_1.MasterEvents.TEST_DEPENDENCIES,
context: {
testDependenciesPath,
compression,
testId: test.id,
fullTitle: test.fullTitle(),
browserId: test.browserId,
},
data: (0, utils_1.mergeSourceDependencies)(browserDeps, testplaneDeps),
});
await testDependencyWriter.saveFor(test, browserDeps, testplaneDeps);
};
};
exports.startSelectivity = startSelectivity;
//# sourceMappingURL=index.js.map