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.
211 lines (210 loc) • 9.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Then_I_see_local_storage_item = Then_I_see_local_storage_item;
exports.Then_I_do_not_see_local_storage_item = Then_I_do_not_see_local_storage_item;
exports.Then_I_see_local_storage_item_equals = Then_I_see_local_storage_item_equals;
exports.Then_I_see_local_storage_item_contains = Then_I_see_local_storage_item_contains;
exports.Then_I_see_session_storage_item = Then_I_see_session_storage_item;
exports.Then_I_do_not_see_session_storage_item = Then_I_do_not_see_session_storage_item;
exports.Then_I_see_session_storage_item_equals = Then_I_see_session_storage_item_equals;
exports.Then_I_see_session_storage_item_contains = Then_I_see_session_storage_item_contains;
// import fs from "fs";
// import path from "path";
const cucumber_1 = require("@cucumber/cucumber");
const test_1 = require("@playwright/test"); // Import expect for assertions
// ===================================================================================
// ASSERTIONS: LOCAL STORAGE
// ===================================================================================
/**
* Asserts that a Local Storage item with the given key exists for the current page's origin.
*
* ```gherkin
* Then I see local storage item {string}
* ```
*
* @param key - The key of the Local Storage item expected to exist.
*
* @example
* Then I see local storage item "token"
*
* @remarks
* This step executes `localStorage.getItem(key)` in the browser's context.
* It asserts that the returned value is not `null`, indicating the item exists.
* @category Storage Assertion Steps
*/
async function Then_I_see_local_storage_item(key) {
const value = await this.page.evaluate((k) => localStorage.getItem(k), key);
(0, test_1.expect)(value, `Local storage item "${key}" should exist but was not found.`).not.toBeNull();
this.log?.(`✅ Verified local storage item "${key}" exists.`);
}
(0, cucumber_1.Then)("I see local storage item {string}", Then_I_see_local_storage_item);
/**
* Asserts that a Local Storage item with the given key does NOT exist for the current page's origin.
*
* ```gherkin
* Then I do not see local storage item {string}
* ```
*
* @param key - The key of the Local Storage item expected NOT to exist.
*
* @example
* Then I do not see local storage item "oldFeatureFlag"
*
* @remarks
* This step executes `localStorage.getItem(key)` in the browser's context.
* It asserts that the returned value is strictly `null`, indicating the item does not exist.
* @category Storage Assertion Steps
*/
async function Then_I_do_not_see_local_storage_item(key) {
const value = await this.page.evaluate((k) => localStorage.getItem(k), key);
(0, test_1.expect)(value, `Expected localStorage["${key}"] to be null, but it exists with value "${value}".`).toBeNull();
this.log?.(`✅ Verified local storage item "${key}" does NOT exist.`);
}
(0, cucumber_1.Then)("I do not see local storage item {string}", Then_I_do_not_see_local_storage_item);
/**
* Asserts that a Local Storage item with the given key exactly equals the expected value.
*
* ```gherkin
* Then I see local storage item {string} equals {string}
* ```
*
* @param key - The key of the Local Storage item.
* @param expectedValue - The exact value the item is expected to have.
*
* @example
* Then I see local storage item "token" equals "abc123"
*
* @remarks
* This step executes `localStorage.getItem(key)` in the browser's context.
* It asserts strict equality between the retrieved value and `expectedValue`.
* @category Storage Assertion Steps
*/
async function Then_I_see_local_storage_item_equals(key, expectedValue) {
const actualValue = await this.page.evaluate((k) => localStorage.getItem(k), key);
(0, test_1.expect)(actualValue, `Expected localStorage["${key}"] to be "${expectedValue}", but got "${actualValue}".`).toBe(expectedValue);
this.log?.(`✅ Verified local storage item "${key}" equals "${expectedValue}".`);
}
(0, cucumber_1.Then)("I see local storage item {string} equals {string}", Then_I_see_local_storage_item_equals);
/**
* Asserts that a Local Storage item with the given key contains the expected substring.
*
* ```gherkin
* Then I see local storage item {string} contains {string}
* ```
*
* @param key - The key of the Local Storage item.
* @param part - The substring expected to be contained within the item's value.
*
* @example
* Then I see local storage item "userSession" contains "loggedIn"
*
* @remarks
* This step executes `localStorage.getItem(key)` in the browser's context.
* It asserts that the retrieved value (if not null) includes the `part` substring.
* @category Storage Assertion Steps
*/
async function Then_I_see_local_storage_item_contains(key, part) {
const value = await this.page.evaluate((k) => localStorage.getItem(k), key);
(0, test_1.expect)(value, `Local storage item "${key}" not found or is null. Expected to contain "${part}".`).not.toBeNull();
(0, test_1.expect)(value, `localStorage["${key}"] ("${value}") does not contain "${part}".`).toContain(part);
this.log?.(`✅ Verified local storage item "${key}" contains "${part}".`);
}
(0, cucumber_1.Then)("I see local storage item {string} contains {string}", Then_I_see_local_storage_item_contains);
// ===================================================================================
// ASSERTIONS: SESSION STORAGE
// ===================================================================================
/**
* Asserts that a Session Storage item with the given key exists for the current page's origin.
*
* ```gherkin
* Then I see session storage item {string}
* ```
*
* @param key - The key of the Session Storage item expected to exist.
*
* @example
* Then I see session storage item "sessionId"
*
* @remarks
* This step executes `sessionStorage.getItem(key)` in the browser's context.
* It asserts that the returned value is not `null`, indicating the item exists.
* @category Storage Assertion Steps
*/
async function Then_I_see_session_storage_item(key) {
const value = await this.page.evaluate((k) => sessionStorage.getItem(k), key);
(0, test_1.expect)(value, `Session storage item "${key}" should exist but was not found.`).not.toBeNull();
this.log?.(`✅ Verified session storage item "${key}" exists.`);
}
(0, cucumber_1.Then)("I see session storage item {string}", Then_I_see_session_storage_item);
/**
* Asserts that a Session Storage item with the given key does NOT exist for the current page's origin.
*
* ```gherkin
* Then I do not see session storage item {string}
* ```
*
* @param key - The key of the Session Storage item expected NOT to exist.
*
* @example
* Then I do not see session storage item "tempData"
*
* @remarks
* This step executes `sessionStorage.getItem(key)` in the browser's context.
* It asserts that the returned value is strictly `null`, indicating the item does not exist.
* @category Storage Assertion Steps
*/
async function Then_I_do_not_see_session_storage_item(key) {
const value = await this.page.evaluate((k) => sessionStorage.getItem(k), key);
(0, test_1.expect)(value, `Expected sessionStorage["${key}"] to be null, but it exists with value "${value}".`).toBeNull();
this.log?.(`✅ Verified session storage item "${key}" does NOT exist.`);
}
(0, cucumber_1.Then)("I do not see session storage item {string}", Then_I_do_not_see_session_storage_item);
/**
* Asserts that a Session Storage item with the given key exactly equals the expected value.
*
* ```gherkin
* Then I see session storage item {string} equals {string}
* ```
*
* @param key - The key of the Session Storage item.
* @param expectedValue - The exact value the item is expected to have.
*
* @example
* Then I see session storage item "sessionId" equals "xyz789"
*
* @remarks
* This step executes `sessionStorage.getItem(key)` in the browser's context.
* It asserts strict equality between the retrieved value and `expectedValue`.
* @category Storage Assertion Steps
*/
async function Then_I_see_session_storage_item_equals(key, expectedValue) {
const actualValue = await this.page.evaluate((k) => sessionStorage.getItem(k), key);
(0, test_1.expect)(actualValue, `Expected sessionStorage["${key}"] to be "${expectedValue}", but got "${actualValue}".`).toBe(expectedValue);
this.log?.(`✅ Verified session storage item "${key}" equals "${expectedValue}".`);
}
(0, cucumber_1.Then)("I see session storage item {string} equals {string}", Then_I_see_session_storage_item_equals);
/**
* Asserts that a Session Storage item with the given key contains the expected substring.
*
* ```gherkin
* Then I see session storage item {string} contains {string}
* ```
*
* @param key - The key of the Session Storage item.
* @param part - The substring expected to be contained within the item's value.
*
* @example
* Then I see session storage item "userState" contains "authenticated"
*
* @remarks
* This step executes `sessionStorage.getItem(key)` in the browser's context.
* It asserts that the retrieved value (if not null) includes the `part` substring.
* @category Storage Assertion Steps
*/
async function Then_I_see_session_storage_item_contains(key, part) {
const value = await this.page.evaluate((k) => sessionStorage.getItem(k), key);
(0, test_1.expect)(value, `Session storage item "${key}" not found or is null. Expected to contain "${part}".`).not.toBeNull();
(0, test_1.expect)(value, `sessionStorage["${key}"] ("${value}") does not contain "${part}".`).toContain(part);
this.log?.(`✅ Verified session storage item "${key}" contains "${part}".`);
}
(0, cucumber_1.Then)("I see session storage item {string} contains {string}", Then_I_see_session_storage_item_contains);