const { I } = inject(); const Helper = require(`@codeceptjs/helper`); import { logger } from "../Logger/logger"; /** * 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 {any} locatorParam located by xpath|CSS * */ static async getListOfElements(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const browser = await this.prototype.helpers[`WebDriver`]; // let res = I._locateFields(locator); const res = await browser._locateFields(locator); return res; } /** * Give you the element present in the Dom * * ```js * DewElement.getElement("/div"); * ``` * * @param {any} locatorParam located by xpath|CSS * */ static async getElement(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } 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 {any} locatorParam located by xpath|CSS * */ static async getVisibleElement(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } 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 {any} locatorParam {String} locator located by xpath|CSS * */ static async getNumberOfElementsPresentInDom(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const browser = await 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 {any} locatorParam located by xpath|CSS * */ static async checkIfElementPresent(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } 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 {any} locatorParam located by xpath|CSS * */ static async checkIfElementVisible(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const isVisible: number = await I.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 {any} locatorParam 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(locatorParam: any, oldValue: string, newValue: string, oldValue1?: string, newValue1?: string) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } 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 {any} locatorParam located by xpath|CSS * */ static async verifyIfISeeElement(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeElement(locator); } /** * Verifies if element is not visible * * ```js * DewElement.verifyIfIDontSeeElement("//div[contains(@class,'welcome-message')]"); * ``` * * @param {any} locatorParam located by xpath|CSS * */ static async verifyIfIDontSeeElement(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.dontSeeElement(locator); } /** * Verifies if text is visible * * ```js * DewElement.verifyIfISeeText("Apple"); * ``` * * @param {String} text to be visible * */ static async verifyIfISeeText(text: string) { await I.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 I.dontSee(text); } /** * Verifies if element is present in DOM * * ```js * DewElement.verifyIfISeeElementInDOM("//input[@id='login']"); * ``` * * @param {any} locatorParam located by xpath|CSS * */ static async verifyIfISeeElementInDOM(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeElementInDOM(locator); } /** * Verifies if all elements have given attributes * * ```js * DewElement.verifyIfISeeAttributeOnElements('//form', { method: "post"}"); * ``` * * @param {any} locatorParam located by xpath|CSS * @param {any} attribute * */ static async verifyIfISeeAttributeOnElements(locatorParam: any, attribute: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeAttributesOnElements(locator, attribute); } /** * Verifies if specified checbox is checked * * ```js * DewElement.verifyIfISeeCheckboxIsChecked("//input[@id='purchaseType']"); * ``` * * @param {any} locatorParam located by xpath|CSS * */ static async verifyIfISeeCheckboxIsChecked(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeCheckboxIsChecked(locator); } /** * Verify if current URL does not contain a fragment provided by user * @param {any} locatorParam */ static async verifyIDontSeeInCurrentUrl(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.dontSeeInCurrentUrl(locator); } /** * Verifies if current URL equals to provided one * * ```js * DewElement.verifyIfISeeElementInDOM("./register"); * ``` * * @param {String} URLPart * */ static async verifyIfISeeCurrentURLEquals(URLPart: string) { await I.seeCurrentUrlEquals(URLPart); } /** * Verifies if current URL contains the part provided by user * * DewElement.verifyIfISeeInCurrentUrl("author") * @param {string} URLPart */ static async verifyIfISeeInCurrentUrl(URLPart: string) { await I.seeInCurrentUrl(URLPart); } /** * Verifies if input field equals to given value * * ```js * DewElement.verifyIfISeeElementInDOM("Username","John"); * ``` * * @param {any} locatorParam located by xpath|CSS * @param {String} value */ static async verifyIfISeeInField(locatorParam: any, value: string) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeInField(locator, value); } /** * Verifies if active popup contains given string * * ```js * DewElement.verifyIfISeeInPopup("Home"); * ``` * * @param {String} text * */ static async verifyIfISeeInPopup(text: string) { await I.seeInPopup(text); } /** * Verifies if title contains text * * ```js * DewElement.verifyIfISeeElementInDOM("Home Page"); * ``` * * @param {string} title * */ static async verifyIfISeeInTitle(title: string) { await I.seeInTitle(title); } /** * Verifies if element applear number of times in DOM * * ```js * DewElement.verifyIfISeeElementInDOM('.buttons', 3); * ``` * @param {any} locatorParam element located by CSS|XPath|strict locator. * @param {number} numberOfElements number of elements. * */ static async verifyIfISeeNumberOfElements(locatorParam: any, numberOfElements: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.seeNumberOfElements(locator, numberOfElements); } /** * Verifies if an element is present given number of times * * ```js * DewElement.verifyIfISeeNumberOfVisibleElements('.buttons', 3); * ``` * * @param {any} locatorParam element located by CSS|XPath|strict locator. * @param {number} numberOfElements number of elements. */ static async verifyIfISeeNumberOfVisibleElements(locatorParam: any, numberOfElements: number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.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 I.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 I.seeTitleEquals(title); } /** *Grab number of visible elements by locator. * * ```js *let hint = await DewElement.grabNumberOfVisibleElements('#tooltip'); * ``` * * @param {any} locatorParam by CSS|XPath|strict locator. * */ static async grabNumberOfVisibleElements(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const noOfElements = await I.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 {any} locatorParam by CSS|XPath|strict locator. * */ static async grabTextFrom(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const text = await I.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 {any} locatorParam by CSS|XPath|strict locator. * @param {String} attribute string attribute name. * */ static async grabAttributeFrom(locatorParam: any, attribute: string) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } /* const attrValue = await I.grabAttributeFrom(locator, attribute); return attrValue; */ let value = ``; return new Promise(async (resolve, reject) => { try { const browser = await this.prototype.helpers[`WebDriver`].browser; const elementRef = await browser.findElement(`xpath`, locator); Object.keys(elementRef) .forEach(async function eachKey(key) { value = await browser.getElementAttribute(elementRef[key], attribute); if (value == null) { value = await browser.getElementProperty(elementRef[key], attribute); } resolve(value); }); } catch (error) { reject(error); } }); } /** * 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 {any} locatorParam by CSS|XPath|strict locator. * */ static async grabValueFrom(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } const value = await I.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 I.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 I.dontSeeCurrentUrlEquals(url); } /** * Checks that element is not on page. * * ```js * DewElement.verifyIfIDontSeeElementInDOM('.nav'); * ``` * @param {any} locatorParam located by CSS|XPath|Strict locator. * */ static async verifyIfIDontSeeElementInDOM(locatorParam: any) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.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 I.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 {any} locatorParam located by label|name|CSS|XPath|strict locator. * @param {string} value string value to check * */ static async verifyIfIDontSeeInField(locatorParam: any, value: string) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await I.dontSeeInField(locator, value); } /** * Checks that the current page does not contains the given string in its raw source code. * * ```js * DewElement.verifyIfIDontSeeInSource('