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.
311 lines (310 loc) • 11.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Then_I_see_URL = Then_I_see_URL;
exports.Then_I_do_not_see_URL = Then_I_do_not_see_URL;
exports.Then_I_see_URL_contains = Then_I_see_URL_contains;
exports.Then_I_do_not_see_URL_contains = Then_I_do_not_see_URL_contains;
exports.Then_I_see_location = Then_I_see_location;
exports.Then_I_see_pathname = Then_I_see_pathname;
exports.Then_I_see_pathname_contains = Then_I_see_pathname_contains;
exports.Then_I_see_hash = Then_I_see_hash;
exports.Then_I_see_hash_contains = Then_I_see_hash_contains;
exports.Then_I_see_search = Then_I_see_search;
exports.Then_I_see_search_contains = Then_I_see_search_contains;
exports.Then_I_see_location_parts = Then_I_see_location_parts;
const cucumber_1 = require("@cucumber/cucumber");
const test_1 = require("@playwright/test");
// ===================================================================================
// ASSERTIONS: URL
// ===================================================================================
/**
* Asserts that the current page's full URL exactly matches the expected string.
*
* ```gherkin
* Then I see URL {string}
* ```
*
* @param expectedUrl - The exact URL string expected.
*
* @example
* Then I see URL "https://example.com/dashboard"
*
* @remarks
* This step retrieves the current URL from `this.page.url()` and performs a strict equality check.
* @category URL Assertion Steps
*/
async function Then_I_see_URL(expectedUrl) {
const currentUrl = this.page.url();
(0, test_1.expect)(currentUrl).toBe(expectedUrl);
this.log?.(`✅ Verified URL is exactly: "${expectedUrl}".`);
}
(0, cucumber_1.Then)("I see URL {string}", Then_I_see_URL);
/**
* Asserts that the current page's full URL does NOT match the given string.
*
* ```gherkin
* Then I do not see URL {string}
* ```
*
* @param notExpectedUrl - The URL string that is NOT expected.
*
* @example
* Then I do not see URL "https://example.com/login"
*
* @remarks
* This step retrieves the current URL from `this.page.url()` and performs a strict inequality check.
* @category URL Assertion Steps
*/
async function Then_I_do_not_see_URL(notExpectedUrl) {
const currentUrl = this.page.url();
(0, test_1.expect)(currentUrl).not.toBe(notExpectedUrl);
this.log?.(`✅ Verified URL is NOT: "${notExpectedUrl}".`);
}
(0, cucumber_1.Then)("I do not see URL {string}", Then_I_do_not_see_URL);
/**
* Asserts that the current page's full URL contains the expected substring.
*
* ```gherkin
* Then I see URL contains {string}
* ```
*
* @param expectedPart - The substring expected to be present in the URL.
*
* @example
* Then I see URL contains "/dashboard"
*
* @remarks
* This step retrieves the current URL from `this.page.url()` and checks if it includes the `expectedPart`.
* @category URL Assertion Steps
*/
async function Then_I_see_URL_contains(expectedPart) {
const currentUrl = this.page.url();
(0, test_1.expect)(currentUrl).toContain(expectedPart);
this.log?.(`✅ Verified URL contains: "${expectedPart}".`);
}
(0, cucumber_1.Then)("I see URL contains {string}", Then_I_see_URL_contains);
/**
* Asserts that the current page's full URL does NOT contain the given substring.
*
* ```gherkin
* Then I do not see URL contains {string}
* ```
*
* @param notExpectedPart - The substring that is NOT expected to be present in the URL.
*
* @example
* Then I do not see URL contains "/login"
*
* @remarks
* This step retrieves the current URL from `this.page.url()` and asserts that it does not include the `notExpectedPart`.
* @category URL Assertion Steps
*/
async function Then_I_do_not_see_URL_contains(notExpectedPart) {
const currentUrl = this.page.url();
(0, test_1.expect)(currentUrl).not.toContain(notExpectedPart);
this.log?.(`✅ Verified URL does NOT contain: "${notExpectedPart}".`);
}
(0, cucumber_1.Then)("I do not see URL contains {string}", Then_I_do_not_see_URL_contains);
// ===================================================================================
// ASSERTIONS: LOCATION PARTS (HREF, PATHNAME, HASH, SEARCH)
// ===================================================================================
/**
* Asserts that the current `window.location.href` (full URL) matches the expected string.
* This is an alias for "Then I see URL {string}".
*
* ```gherkin
* Then I see location {string}
* ```
*
* @param expectedHref - The exact `window.location.href` string expected.
*
* @example
* Then I see location "https://example.com/dashboard"
*
* @remarks
* This step executes `window.location.href` in the browser context and compares it strictly.
* It's functionally identical to {@link Then_I_see_URL | "Then I see URL {string}"}.
* @category Location Assertion Steps
*/
async function Then_I_see_location(expectedHref) {
const locationHref = await this.page.evaluate(() => window.location.href);
(0, test_1.expect)(locationHref).toBe(expectedHref);
this.log?.(`✅ Verified location href is exactly: "${expectedHref}".`);
}
(0, cucumber_1.Then)("I see location {string}", Then_I_see_location);
/**
* Asserts that the current `window.location.pathname` (path part of URL) matches the expected string.
*
* ```gherkin
* Then I see pathname {string}
* ```
*
* @param expectedPathname - The exact pathname string expected (e.g., "/dashboard").
*
* @example
* Then I see pathname "/dashboard"
*
* @remarks
* This step executes `window.location.pathname` in the browser context and compares it strictly.
* @category Location Assertion Steps
*/
async function Then_I_see_pathname(expectedPathname) {
const pathname = await this.page.evaluate(() => window.location.pathname);
(0, test_1.expect)(pathname).toBe(expectedPathname);
this.log?.(`✅ Verified pathname is exactly: "${expectedPathname}".`);
}
(0, cucumber_1.Then)("I see pathname {string}", Then_I_see_pathname);
/**
* Asserts that the current `window.location.pathname` contains the expected substring.
*
* ```gherkin
* Then I see pathname contains {string}
* ```
*
* @param expectedPart - The substring expected to be present in the pathname.
*
* @example
* Then I see pathname contains "/settings/"
*
* @remarks
* This step executes `window.location.pathname` in the browser context and checks for substring presence.
* @category Location Assertion Steps
*/
async function Then_I_see_pathname_contains(expectedPart) {
const pathname = await this.page.evaluate(() => window.location.pathname);
(0, test_1.expect)(pathname).toContain(expectedPart);
this.log?.(`✅ Verified pathname contains: "${expectedPart}".`);
}
(0, cucumber_1.Then)("I see pathname contains {string}", Then_I_see_pathname_contains);
/**
* Asserts that the current `window.location.hash` (fragment identifier) matches the expected string.
*
* ```gherkin
* Then I see hash {string}
* ```
*
* @param expectedHash - The exact hash string expected (e.g., "#section-1").
*
* @example
* Then I see hash "#section"
*
* @remarks
* This step executes `window.location.hash` in the browser context and compares it strictly.
* @category Location Assertion Steps
*/
async function Then_I_see_hash(expectedHash) {
const hash = await this.page.evaluate(() => window.location.hash);
(0, test_1.expect)(hash).toBe(expectedHash);
this.log?.(`✅ Verified hash is exactly: "${expectedHash}".`);
}
(0, cucumber_1.Then)("I see hash {string}", Then_I_see_hash);
/**
* Asserts that the current `window.location.hash` contains the expected substring.
*
* ```gherkin
* Then I see hash contains {string}
* ```
*
* @param expectedPart - The substring expected to be present in the hash.
*
* @example
* Then I see hash contains "details"
*
* @remarks
* This step executes `window.location.hash` in the browser context and checks for substring presence.
* @category Location Assertion Steps
*/
async function Then_I_see_hash_contains(expectedPart) {
const hash = await this.page.evaluate(() => window.location.hash);
(0, test_1.expect)(hash).toContain(expectedPart);
this.log?.(`✅ Verified hash contains: "${expectedPart}".`);
}
(0, cucumber_1.Then)("I see hash contains {string}", Then_I_see_hash_contains);
/**
* Asserts that the current `window.location.search` (query string) matches the expected string.
*
* ```gherkin
* Then I see search {string}
* ```
*
* @param expectedSearch - The exact search string expected (e.g., "?q=test&page=1").
*
* @example
* Then I see search "?q=test"
*
* @remarks
* This step executes `window.location.search` in the browser context and compares it strictly.
* @category Location Assertion Steps
*/
async function Then_I_see_search(expectedSearch) {
const search = await this.page.evaluate(() => window.location.search);
(0, test_1.expect)(search).toBe(expectedSearch);
this.log?.(`✅ Verified search is exactly: "${expectedSearch}".`);
}
(0, cucumber_1.Then)("I see search {string}", Then_I_see_search);
/**
* Asserts that the current `window.location.search` contains the expected substring.
*
* ```gherkin
* Then I see search contains {string}
* ```
*
* @param expectedPart - The substring expected to be present in the search query.
*
* @example
* Then I see search contains "q=test"
*
* @remarks
* This step executes `window.location.search` in the browser context and checks for substring presence.
* @category Location Assertion Steps
*/
async function Then_I_see_search_contains(expectedPart) {
const search = await this.page.evaluate(() => window.location.search);
(0, test_1.expect)(search).toContain(expectedPart);
this.log?.(`✅ Verified search contains: "${expectedPart}".`);
}
(0, cucumber_1.Then)("I see search contains {string}", Then_I_see_search_contains);
/**
* Asserts that multiple parts of the current `window.location` object match expected values from a data table.
*
* ```gherkin
* Then I see location
* | key | value |
* | pathname | /dashboard |
* | hash | #section |
* ```
*
* @param table - A Cucumber DataTable with `key` and `value` columns, where `key` is a `window.location` property (e.g., "pathname", "hash", "origin").
*
* @example
* Then I see location
* | key | value |
* | pathname | /profile |
* | protocol | https: |
* | hostname | myapp.com |
*
* @remarks
* This step retrieves various `window.location` properties as an object and then iterates
* through the data table, asserting that each specified `key` has its corresponding `value`.
* This allows for flexible and combined assertions on the URL components.
* @category Location Assertion Steps
*/
async function Then_I_see_location_parts(table) {
const location = await this.page.evaluate(() => ({
href: window.location.href,
origin: window.location.origin,
protocol: window.location.protocol,
host: window.location.host,
hostname: window.location.hostname,
port: window.location.port,
pathname: window.location.pathname,
search: window.location.search,
hash: window.location.hash,
}));
for (const [key, expected] of table.rows()) {
const actual = location[key];
(0, test_1.expect)(actual, `Location part "${key}" mismatch: Expected "${expected}", but got "${actual}".`).toBe(expected);
this.log?.(`✅ Verified location part "${key}" is "${expected}".`);
}
}
(0, cucumber_1.Then)("I see location", Then_I_see_location_parts);