/* eslint-disable */ const { I } = inject(); import { Helper } from 'codeceptjs'; import { logger } from "../Logger/logger"; import { z } from './z'; /** * Radio Button class */ export class DewElement extends Helper { /** * Give you the list of all the elements present in the Dom * * ```js * DewElement.getListOfElements("/div"); * ``` * * @param {String} locator located by xpath|CSS * */ static async getListOfElements(locators: string) { let locator = await z.getElements(locators); const browser = this.prototype.helpers[`WebDriver`]; // let res = z._locateFields(locator); const res = await browser._locateFields(locator); return res; } /** * Give you the element present in the Dom * * ```js * DewElement.getElement("/div"); * ``` * * @param {String} locator located by xpath|CSS * */ static async getElement(locators: string) { let locator = await z.getElements(locators); const browser = await this.prototype.helpers[`WebDriver`]; const res = await browser._locate(locator); return res; } /** * Give you the visible element from the Dom * * ```js * DewElement.getVisibleElement("/div"); * ``` * * @param {String} locator located by xpath|CSS * */ static async getVisibleElement(locators: string) { let locator = await z.getElements(locators); const totalNumberOfElement = await DewElement.getNumberOfElementsPresentInDom(locator); let element: any = locator; for (let index = 1; index <= totalNumberOfElement; index++) { const visibleElement: any = locate(locator).at(index); if (await DewElement.checkIfElementVisible(visibleElement)) { element = visibleElement.value; break; } } return element; } /** * Give you the list of all the elements present in the Dom * * ```js * DewElement.getNumberOfElementsPresentInDom("//div"); * ``` * * @param {String} locator {String} locator located by xpath|CSS * */ static async getNumberOfElementsPresentInDom(locators: string) { let locator = await z.getElements(locators); const browser = this.prototype.helpers[`WebDriver`]; const res = await browser._locateFields(locator); return res.length; } /** * check whether element is present in the Dom * * ```js * DewElement.checkIfElementPresent("//div"); * ``` * * @param {String} locator located by xpath|CSS * */ static async checkIfElementPresent(locator: string) { const isPresent = await this.getListOfElements(locator); if (await isPresent && isPresent.length) { await logger.info(`Element is present`); return true; } else { await logger.info(`Issue while performing operation in DDCC: Element is not present`); return false; } } /** * check whether element is visible or not * * ```js * DewElement.checkIfElementVisible("//div"); * ``` * * @param {String} locator located by xpath|CSS * */ static async checkIfElementVisible(locator: string) { const isVisible: number = await z.grabNumberOfVisibleElements(locator); if (await isVisible > 0) { await logger.info(` Element is visible`); return true; } else { await logger.info(`Element is not visible`); return false; } } /** * replace value from xpath and return newly formed xpath * * ```js * DewElement.getDynamicLocator("//div/<>","abc","button"); * * Result : "//div//button * ``` * * @param {String} locator located by xpath|CSS * @param {String} oldValue value in xpath|CSS that needs to be replaced * @param {String} newValue value that will be replaced in xpath|CSS * @param {String} oldValue1 value in xpath|CSS that needs to be replaced * @param {String} newValue1 value that will be replaced in xpath|CSS * */ static async getDynamicLocator(locator: string, oldValue: string, newValue: string, oldValue1?: string, newValue1?: string) { let newLocator: string; if (oldValue1 && newValue1) { newLocator = locator.replace(oldValue, newValue).replace(oldValue1, newValue1); } else { newLocator = locator.replace(oldValue, newValue); } return newLocator; } /** * Verifies if element is visible * * ```js * DewElement.verifyIfISeeElement("//div[contains(@class,'welcome-message')]"); * ``` * * @param {String} locator located by xpath|CSS * */ static async verifyIfISeeElement(locator: string) { await z.seeElement(locator); } /** * Verifies if element is not visible * * ```js * DewElement.verifyIfIDontSeeElement("//div[contains(@class,'welcome-message')]"); * ``` * * @param {String} locator located by xpath|CSS * */ static async verifyIfIDontSeeElement(locator: string) { await z.dontSeeElement(locator); } /** * Verifies if text is visible * * ```js * DewElement.verifyIfISeeText("Apple"); * ``` * * @param {String} text to be visible * */ static async verifyIfISeeText(text: string) { await z.see(text); } /** * Verifies if text is not visible * * ```js * DewElement.verifyIfIDontSeeText("Apple"); * ``` * * @param {String} text to be not visible * */ static async verifyIfIDontSeeText(text: string) { await z.dontSee(text); } /** * Verifies if element is present in DOM * * ```js * DewElement.verifyIfISeeElementInDOM("//input[@id='login']"); * ``` * * @param {String} locator located by xpath|CSS * */ static async verifyIfISeeElementInDOM(locator: string) { await z.seeElementInDOM(locator); } /** * Verifies if all elements have given attributes * * ```js * DewElement.verifyIfISeeAttributeOnElements('//form', { method: "post"}"); * ``` * * @param {String} locator located by xpath|CSS * @param {any} attribute * */ static async verifyIfISeeAttributeOnElements(locator: string, attribute: any) { await z.seeAttributesOnElements(locator, attribute); } /** * Verifies if specified checbox is checked * * ```js * DewElement.verifyIfISeeCheckboxIsChecked("//input[@id='purchaseType']"); * ``` * * @param {String} locator located by xpath|CSS * */ static async verifyIfISeeCheckboxIsChecked(locator: string) { await z.seeCheckboxIsChecked(locator); } /** * Verify if current URL does not contain a fragment provided by user * @param {string} locator */ static async verifyIDontSeeInCurrentUrl(locator: string) { await z.dontSeeInCurrentUrl(locator); } /** * Verifies if current URL equals to provided one * * ```js * DewElement.verifyIfISeeElementInDOM("./register"); * ``` * * @param {String} URLPart * */ static async verifyIfISeeCurrentURLEquals(URLPart: string) { await z.seeCurrentUrlEquals(URLPart); } /** * Verifies if current URL contains the part provided by user * * DewElement.verifyIfISeeInCurrentUrl("author") * @param {string} URLPart */ static async verifyIfISeeInCurrentUrl(URLPart: string) { await z.seeInCurrentUrl(URLPart); } /** * Verifies if input field equals to given value * * ```js * DewElement.verifyIfISeeElementInDOM("Username","John"); * ``` * * @param {String} locator located by xpath|CSS * @param {String} value */ static async verifyIfISeeInField(locator: string, value: string) { await z.seeInField(locator, value); } /** * Verifies if active popup contains given string * * ```js * DewElement.verifyIfISeeInPopup("Home"); * ``` * * @param {String} text * */ static async verifyIfISeeInPopup(text: string) { await z.seeInPopup(text); } /** * Verifies if title contains text * * ```js * DewElement.verifyIfISeeElementInDOM("Home Page"); * ``` * * @param {string} title * */ static async verifyIfISeeInTitle(title: string) { await z.seeInTitle(title); } /** * Verifies if element applear number of times in DOM * * ```js * DewElement.verifyIfISeeElementInDOM('.buttons', 3); * ``` * @param {string} locator element located by CSS|XPath|strict locator. * @param {number} numberOfElements number of elements. * */ static async verifyIfISeeNumberOfElements(locator: string, numberOfElements: number) { await z.seeNumberOfElements(locator, numberOfElements); } /** * Verifies if an element is present given number of times * * ```js * DewElement.verifyIfISeeNumberOfVisibleElements('.buttons', 3); * ``` * * @param {string} locator element located by CSS|XPath|strict locator. * @param {number} numberOfElements number of elements. */ static async verifyIfISeeNumberOfVisibleElements(locator: string, numberOfElements: number) { await z.seeNumberOfVisibleElements(locator, numberOfElements); } /** * Verifies if text is equal to provided one * * ```js * DewElement.verifyIfTextEquals("text", "h1"); * ``` * * @param {string} actualText element value to check. * @param {string?} expectedText element located by CSS|XPath|strict locator. * */ static async verifyIfTextEquals(actualText: string, expectedText: string) { await z.seeTextEquals(actualText, expectedText); } /** * Verifies if title is equal to provided one * * ```js * DewElement.verifyIfTitleEquals("Test title"); * ``` * * @param {String} title to check * */ static async verifyIfTitleEquals(title: string) { await z.seeTitleEquals(title); } /** *Grab number of visible elements by locator. * * ```js *let hint = await DewElement.grabNumberOfVisibleElements('#tooltip'); * ``` * * @param {String} locator by CSS|XPath|strict locator. * */ static async grabNumberOfVisibleElements(locator: string) { const noOfElements = await z.grabNumberOfVisibleElements(locator); return noOfElements; } /** * Retrieves a text from an element located by CSS or XPath and returns it to test. Resumes test execution, so should be used inside async with await operator. * * ```js * let pin = await DewElement.grabTextFrom('#pin'); * ``` * * @param {String} locator by CSS|XPath|strict locator. * */ static async grabTextFrom(locator: string) { const text = await z.grabTextFrom(locator); return text; } /** * Retrieves an attribute from an element located by CSS or XPath and returns it to test. * * ```js *let hint = await DewElement.grabAttributeFrom('#tooltip', 'title'); * ``` * * @param {String} locator by CSS|XPath|strict locator. * @param {String} attribute string attribute name. * */ static async grabAttributeFrom(locator: string, attribute: string) { const attrValue = await z.grabAttributeFrom(locator, attribute); return attrValue; } /** * Retrieves a value from a form element located by CSS or XPath and returns it to test. * * ```js *let email = await DewElement.grabValueFrom('input[name=email]'); * ``` * * @param {String} locator by CSS|XPath|strict locator. * */ static async grabValueFrom(locator: string) { const value = await z.grabValueFrom(locator); return value; } /** * Checks that cookie with given name does not exist * * ```js * DewElement.verifyIfIDontSeeCookie('auth'); * ``` * @param {string} cookieName * */ static async verifyIfIDontSeeCookie(cookieName: string) { await z.dontSeeCookie(cookieName); } /** * Checks that current url is not equal to provided one. If a relative url provided, a configured url will be prepended to it. * * ```js * DewElement.verifyIfIDontSeeCurrentUrlEquals('http://mysite.com/login'); * ``` * @param {string} url * */ static async verifyIfIDontSeeCurrentUrlEquals(url: string) { await z.dontSeeCurrentUrlEquals(url); } /** * Checks that element is not on page. * * ```js * DewElement.verifyIfIDontSeeElementInDOM('.nav'); * ``` * @param {string} locator located by CSS|XPath|Strict locator. * */ static async verifyIfIDontSeeElementInDOM(locator: string) { await z.dontSeeElementInDOM(locator); } /** * Checks that title does not contain text. * * ```js * DewElement.verifyIfIDontSeeInTitle('Error'); * ``` * @param {string} text value to check. * */ static async verifyIfIDontSeeInTitle(text: string) { await z.dontSeeInTitle(text); } /** * Checks that value of input field or textarea doesn't equal to given value * * ```js * DewElement.verifyIfIDontSeeInField({ css: 'form input.email' }, 'user@user.com'); * ``` * @param {string} locator located by label|name|CSS|XPath|strict locator. * @param {string} value string value to check * */ static async verifyIfIDontSeeInField(locator: string, value: string) { await z.dontSeeInField(locator, value); } /** * Checks that the current page does not contains the given string in its raw source code. * * ```js * DewElement.verifyIfIDontSeeInSource('