playwright-cucumber-ts-steps
Version:
A collection of reusable Playwright step definitions for Cucumber in TypeScript, designed to streamline end-to-end testing across web, API, and mobile applications.
131 lines (130 loc) • 5.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const cucumber_1 = require("@cucumber/cucumber");
const resolveUtils_1 = require("../helpers/utils/resolveUtils");
// Step 1: Check and load existing session if valid
(0, cucumber_1.When)("I login with a session data {string}", async function (sessionName) {
const sessionPath = (0, resolveUtils_1.resolveSessionPath)(this, sessionName);
this.data.sessionFile = sessionPath;
if (fs_1.default.existsSync(sessionPath)) {
try {
await this.context?.addCookies(JSON.parse(fs_1.default.readFileSync(sessionPath, "utf-8")).cookies || []);
this.log?.(`✅ Loaded session from ${sessionPath}`);
}
catch (err) {
this.log?.(`⚠️ Failed to apply session: ${err.message}`);
}
}
else {
this.log?.(`⚠️ Session file not found: ${sessionPath}`);
}
});
/**
* @step
* @description Saves the current browser context (cookies, localStorage, sessionStorage) as a session file.
* @example
* When I save session as "my-session"
*/
(0, cucumber_1.When)(/^I save session as "([^"]+)"$/, async function (sessionName) {
const sessionPath = (0, resolveUtils_1.resolveSessionPath)(this, sessionName);
const cookies = await this.context.cookies();
const [localStorageData, sessionStorageData] = await this.page.evaluate(() => {
const toPairs = (store) => Object.entries(store);
return [
[{ origin: location.origin, values: toPairs(localStorage) }],
[{ origin: location.origin, values: toPairs(sessionStorage) }],
];
});
const sessionData = {
cookies,
localStorage: localStorageData,
sessionStorage: sessionStorageData,
};
try {
fs_1.default.writeFileSync(sessionPath, JSON.stringify(sessionData, null, 2));
this.log?.(`💾 Saved session as "${sessionName}"`);
}
catch (err) {
this.log?.(`❌ Failed to save session "${sessionName}": ${err.message}`);
}
});
/**
* @step
* @description Removes a session file with the given name.
* @example
* When I clear session "my-session"
*/
(0, cucumber_1.When)("I clear session {string}", function (sessionName) {
const sessionPath = (0, resolveUtils_1.resolveSessionPath)(this, sessionName);
if (fs_1.default.existsSync(sessionPath)) {
fs_1.default.unlinkSync(sessionPath);
this.log?.(`🧹 Cleared session: ${sessionPath}`);
}
else {
this.log?.(`⚠️ Session not found: ${sessionPath}`);
}
});
/**
* @step
* @description Restores cookies, localStorage, and sessionStorage from a session file. Optionally reloads the page.
* @example
* When I restore session cookies "my-session"
* When I restore session cookies "my-session" with reload "false"
*/
(0, cucumber_1.When)(/^I restore session cookies "([^"]+)"(?: with reload "(true|false)")?$/, async function (sessionName, reload = "true") {
const sessionPath = (0, resolveUtils_1.resolveSessionPath)(this, sessionName);
if (!fs_1.default.existsSync(sessionPath)) {
this.log?.(`❌ Session file not found: ${sessionPath}`);
return;
}
const sessionData = JSON.parse(fs_1.default.readFileSync(sessionPath, "utf-8"));
const { cookies = [], localStorage = [], sessionStorage = [] } = sessionData;
try {
// Clear & set cookies
if (cookies.length) {
const existing = await this.context.cookies();
if (existing.length)
await this.context.clearCookies();
await this.context.addCookies(cookies);
this.log?.(`🍪 Cookies restored from "${sessionName}"`);
}
// Apply storage into page context
await this.page.goto("about:blank");
if (localStorage.length > 0) {
for (const entry of localStorage) {
await this.page.addInitScript(([origin, values]) => {
if (window.origin === origin) {
for (const [key, val] of values) {
localStorage.setItem(key, val);
}
}
}, [entry.origin, entry.values]);
}
this.log?.("📦 localStorage restored");
}
if (sessionStorage.length > 0) {
for (const entry of sessionStorage) {
await this.page.addInitScript(([origin, values]) => {
if (window.origin === origin) {
for (const [key, val] of values) {
sessionStorage.setItem(key, val);
}
}
}, [entry.origin, entry.values]);
}
this.log?.("🗄️ sessionStorage restored");
}
// Final reload to apply context if requested
if (reload !== "false") {
await this.page.reload();
this.log?.("🔄 Page reloaded to apply restored session");
}
}
catch (err) {
this.log?.(`❌ Error restoring session: ${err.message}`);
}
});