tm-playwright-framework
Version:
Playwright Cucumber TS framework - The easiest way to learn
243 lines (242 loc) • 13.7 kB
TypeScript
/**
* ASSERTIONS.TS
*
* This TypeScript file contains methods to perform various verifications (Assertions).
*
* @author Sasitharan, Govindharam
* @reviewer Sahoo, AshokKumar
* @version 1.0 - 1st-JUNE-2025
*
* @methods
* - `verifyRole`: Accepts an element locator as a `string` or Element and asserts the role attribute value.
* - `verifyAttribute`: Accepts an element locator as a `string` or Element and asserts the given attribute value.
* - `verifyElementToHave`: Accepts an element locator as a `string` or Element and asserts that the element has the given item in it.
* - `verifyPresence`: Accepts an element locator as a `string` or Element and asserts that the element is present as specified.
* - `isVisible`: Accepts an element locator as a `string` or Element and asserts that the element is visible.
* - `getText`: Accepts an element locator as a `string` or Element and returns its text.
* - `isGreaterThan`: Accepts an element locator as a `string` or Element and asserts that the value / count / Attribute value is greater than the given number.
* - `isGreaterThanOrEqual`: Accepts an element locator as a `string` or Element and asserts that the value / count / Attribute value is greater than or equal to the given number.
* - `isEqual`: Accepts an element locator as a `string` or Element and asserts that the value / count / Attribute value is equal to the given number.
* - `isLessThan`: Accepts an element locator as a `string` or Element and asserts that the value / count / Attribute value is less than the given number.
* - `isLessThanOrEqual`: Accepts an element locator as a `string` or Element and asserts that the value / count / Attribute value is less than or equal to the given number.
* - `assertTitle`: Accepts an element locator as a `string` and asserts the window title.
* - `assertTitleContains`: Accepts an element locator as a `string` and asserts the window title with partial text.
* - `assertURL`: Accepts an element locator as a `string` and asserts the URL of the current page.
* - `assertURLContains`: Accepts an element locator as a `string` and asserts the URL of the current page with partial text.
* - `assertText`: Accepts an element locator as a `string` or Element and asserts the element has the given text in it.
* - `assertTextContains`: Accepts an element locator as a `string` or Element and asserts the element contains the given text in it.
* - `isSelected`: Accepts an element locator as a `string` or Element and assert the element is selected
* - `isUnselected`: Accepts an element locator as a `string` or Element and assert the element is unselected
* - `verifySelectedOption` : Accepts an Select element locator as a `string` or Element and assert the selected option or multiple options
* - `assertNumericValue` : Accetps two different values along with the condition to be applied for assertion
* - `assertStringValue` : Accetps two different strings along with the condition to be applied for assertion
*/
import { Page } from "@playwright/test";
export default class Assertion {
private page;
constructor(page: Page);
private resolveLocator;
private handleNegation;
/**
* Verifies the role attribute of an element.
* @param locator - The element locator as a string or Element.
* @param expectedRole - The expected role value.
* @param negate - Optional parameter to negate the assertion.
*/
verifyRole(locator: any, expectedRole: "alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem" | string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Verifies the specified attribute of an element.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of attribute to assert (e.g., Attribute, CSS, JSProperty, Role).
* @param expectedAttribute - The name of the attribute to verify.
* @param expectedValue - The expected value of the attribute.
* @param negate - Optional parameter to negate the assertion.
*/
verifyAttribute(locator: any, assertWhat: "Attribute" | "CSS" | "JSProperty" | "Role", expectedAttribute: string, expectedValue: string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Verifies that an element contains the specified item.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of item to assert (e.g., Class, Count, Id, Text, Value, etc.).
* @param expectedValue - The expected value of the item.
* @param negate - Optional parameter to negate the assertion.
*/
verifyElementToHave(locator: any, assertWhat: "Class" | "Count" | "Id" | "Text" | "Value" | "Values", expectedValue: string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Verifies the presence of an element based on the specified condition.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The condition to verify (e.g., toBeAttached, toBeVisible, etc.).
* @param negate - Optional parameter to negate the assertion.
*/
verifyPresence(locator: any, assertWhat: "toBeAttached" | "toBeDisabled" | "toBeEditable" | "toBeEmpty" | "toBeEnabled" | "toBeFocused" | "toBeHidden" | "toBeInViewport" | "toBeVisible", negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Checks if an element is visible.
* @param locator - The element locator as a `string` or Element.
* @param negate - Optional parameter to negate the assertion.
* @param timeOut - Optional timeout in milliseconds to wait for the element to be visible.
*/
isVisible(locator: any, options?: {
isNegative?: boolean;
timeOut?: number;
}): Promise<void>;
/**
* Retrieves the text content of an element.
* @param locator - The element locator as a `string` or Element.
* @returns The text content of the element.
*/
getText(locator: any): Promise<string>;
/**
* Asserts that a value is greater than a specified number.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of value to assert (e.g., Text, Count, etc.).
* @param value - The number to compare against.
* @param negate - Optional parameter to negate the assertion.
*/
isGreaterThan(locator: any, assertWhat: "Text" | "Count" | string, value: number, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that a value is less than a specified number.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of value to assert (e.g., Text, Count, etc.).
* @param value - The number to compare against.
* @param negate - Optional parameter to negate the assertion.
*/
isLessThan(locator: any, assertWhat: "Text" | "Count" | string, value: number, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that a value is equal to a specified number.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of value to assert (e.g., Text, Count, etc.).
* @param value - The number to compare against.
* @param negate - Optional parameter to negate the assertion.
*/
isEqual(locator: any, assertWhat: "Text" | "Count" | string, value: number, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that a value is greater than or equal to a specified number.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of value to assert (e.g., Text, Count, etc.).
* @param value - The number to compare against.
* @param negate - Optional parameter to negate the assertion.
*/
isGreaterThanOrEqual(locator: any, assertWhat: "Text" | "Count" | string, value: number, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that a value is less than or equal to a specified number.
* @param locator - The element locator as a string or Element.
* @param assertWhat - The type of value to assert (e.g., Text, Count, etc.).
* @param value - The number to compare against.
* @param negate - Optional parameter to negate the assertion.
*/
isLessThanOrEqual(locator: any, assertWhat: "Text" | "Count" | string, value: number, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts the window title.
* @param expected - The expected title as a string or RegExp.
* @param negate - Optional parameter to negate the assertion.
*/
assertTitle(expected: string | RegExp, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that the window title contains a specific substring.
* @param expectedSubstring - The substring to check for in the title.
* @param negate - Optional parameter to negate the assertion.
*/
assertTitleContains(expectedSubstring: string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts the URL of the current page.
* @param url - The expected URL.
* @param negate - Optional parameter to negate the assertion.
*/
assertURL(url: string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that the URL of the current page contains a specific substring.
* @param url - The substring to check for in the URL.
* @param negate - Optional parameter to negate the assertion.
*/
assertURLContains(url: string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that an element has the specified text.
* @param locator - The element locator as a string or Element.
* @param expected - The expected text as a string or RegExp.
* @param negate - Optional parameter to negate the assertion.
*/
assertText(locator: any, expected: string | RegExp, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that an element contains the specified text.
* @param locator - The element locator as a string or Element.
* @param expected - The expected text as a string or RegExp.
* @param negate - Optional parameter to negate the assertion.
*/
assertTextContains(locator: any, expected: string | RegExp, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that an element is selected.
* @param locator - The element locator as a string or Element.
* @param negate - Optional parameter to negate the assertion.
*/
isSelected(locator: any, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Asserts that an element is unselected.
* @param locator - The element locator as a string or Element.
* @param negate - Optional parameter to negate the assertion.
*/
isUnselected(locator: any, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Verifies the selected option(s) of a dropdown element.
* @param locator - The dropdown element locator as a string or Element.
* @param options - The expected selected option(s).
* @param negate - Optional parameter to negate the assertion.
*/
verifySelectedOption(locator: string, options: string[] | string, negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Performs a numeric comparison between two values.
* @param expectedValue - The expected numeric value.
* @param actualValue - The actual numeric value.
* @param condition - The condition to apply (e.g., =, >=, <=, >, <, <>).
* @param negate - Optional parameter to negate the assertion.
*/
assertNumericValue(expectedValue: number | string, actualValue: number | string, condition: "=" | ">=" | "<=" | ">" | "<" | "<>", negate?: {
isNegative: boolean;
}): Promise<void>;
/**
* Performs a string comparison between two values.
* @param expectedValue - The expected string value.
* @param actualValue - The actual string value.
* @param condition - The condition to apply (e.g., EQUALS, CONTAINS, STARTSWITH, etc.).
* @param caseSensitive - Whether the comparison is case-sensitive.
* @param negate - Optional parameter to negate the assertion.
*/
assertStringValue(expectedValue: string, actualValue: string, condition: "EQUALS" | "NOTEQUALS" | "CONTAINS" | "NOTCONTAINS" | "STARTSWITH" | "NOTSTARTSWITH" | "ENDSWITH" | "NOTENDSWITH" | "REGEX", options?: {
isNegative?: boolean;
caseSensitive?: boolean;
}): Promise<void>;
}