@focusconsulting/auto-a11y
Version:
A powerful tool that combines AI with accessibility-first element selection for Playwright tests
110 lines (109 loc) • 4.25 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SnapshotManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
* Class to manage saving and loading locator snapshots
*/
class SnapshotManager {
constructor(snapshotFilePath) {
this.snapshotFilePath = snapshotFilePath;
}
/**
* Creates a snapshot file path based on test information
* @param testInfo Playwright TestInfo object
* @returns Path to the snapshot file
*/
static createSnapshotPath(testInfo) {
// Get the test file path and create a snapshot directory next to it
const testFilePath = testInfo.file;
const testDir = path.dirname(testFilePath);
const testFileName = path.basename(testFilePath, path.extname(testFilePath));
// Create snapshots directory if it doesn't exist
const snapshotsDir = path.join(testDir, `__${testFileName}-locator-snapshots__`);
if (!fs.existsSync(snapshotsDir)) {
fs.mkdirSync(snapshotsDir, { recursive: true });
}
// Use test name for snapshot file
return path.join(snapshotsDir, `${testInfo.title.replace(/\s+/g, "-")}.json`);
}
/**
* Reads locator snapshots from the snapshot file
* @returns Object containing saved locators or empty object if file doesn't exist
*/
readSnapshots() {
if (!this.snapshotFilePath)
return {};
try {
if (fs.existsSync(this.snapshotFilePath)) {
const data = fs.readFileSync(this.snapshotFilePath, "utf8");
return JSON.parse(data);
}
}
catch (error) {
console.warn(`Failed to read locator snapshots: ${error}`);
}
return {};
}
/**
* Saves a locator to the snapshot file
* @param description The element description
* @param queryInfo The query information to save
*/
saveSnapshot(description, queryInfo) {
if (!this.snapshotFilePath)
return;
try {
// Ensure directory exists
const dir = path.dirname(this.snapshotFilePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Read existing snapshots
const snapshots = this.readSnapshots();
// Add or update the snapshot
snapshots[description] = queryInfo;
// Write back to file with a replacer function to avoid escaping single quotes
const jsonString = JSON.stringify(snapshots, null, 2);
fs.writeFileSync(this.snapshotFilePath, jsonString, "utf8");
}
catch (error) {
console.warn(`Failed to save locator snapshot: ${error}`);
}
}
}
exports.SnapshotManager = SnapshotManager;