const { I } = inject(); import { Helper, locator } from 'codeceptjs'; import { logger } from "../Logger/logger"; import { z, webDriver, puppeteer } from './z'; import { Wait } from './dewWait'; import { DewElement } from './element'; // import { DewElement } from './element'; const cssToXPath = require(`css-to-xpath`); import { CNS } from './CNS'; /** *Common Keyword class */ export class CommonKeyword extends Helper { static async clickElement(locator: string, isIEClick: any, scope?: string): Promise; static async clickElement(locator: string, scope?: string): Promise; /** * To click on an element matched by CSS|XPath|strict locator. * * ```js * *CommonKeyword.clickElement(".btn-text d-block") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {String} isIEClick Pass true if you want to preform z.click on IE browser * @param {String} scope located by CSS|XPath|strict locator. * */ static async clickElement(locator: any, isIEClick?: any, scope?: any) { const helper = await z.getHelper(); if (helper === webDriver) { await z.click(locator, scope); } else if (helper === puppeteer) { const page = this.prototype.helpers[`Puppeteer`].page; try { if (scope == undefined) { if (isIEClick == undefined) { await CommonKeyword.click(locator); } else { if (typeof isIEClick == `boolean`) { if (isIEClick == true) { if (await DewElement.checkIfElementVisible(locator)) { } else { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(locator, { behavior: `auto`, block: `nearest`, inline: `center` }); } await I.waitForVisible(locator); await I.seeElement(locator); await I.click(locator); } else { await CommonKeyword.click(locator); } } else if (typeof isIEClick == `string`) { await within(isIEClick, async () => { await CommonKeyword.click(locator); }); } } } else { if (isIEClick == true) { await within(scope, async () => { if (await DewElement.checkIfElementVisible(locator)) { } else { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(locator, { behavior: `auto`, block: `nearest`, inline: `center` }); } await I.waitForVisible(locator); await I.seeElement(locator); await I.click(locator); }); } else { await within(scope, async () => { await CommonKeyword.click(locator); }); } } await logger.info(`Element Clicked: ` + locator); } catch (err) { // await I.say("Issue while performing operation in DDCC: Unable to Click element " + locator); await logger.info(`Issue while performing operation in DDCC: Unable to Click element ` + locator); } } } static async clickLabel(locator: string, isIEClick: boolean, scope?: string): Promise; static async clickLabel(locator: string, scope?: string): Promise; /** * To click on an element matched by label * * ```js * *CommonKeyword.clickLabel("Apply") * ``` * * @param {String} label of an element * @param {String} isIEClick Pass true if you want to preform z.click on IE browser * @param {String} scope located by CSS|XPath|strict locator. * */ static async clickLabel(label: any, isIEClick?: any, scope?: any) { const newLabel = `.//*[text()[normalize-space()='${label}']]`; const helper = await z.getHelper(); if (helper === webDriver) { if (scope == undefined) { await z.click(newLabel); } else { await z.click(newLabel, scope); } } else if (helper === puppeteer) { const page = this.prototype.helpers[`Puppeteer`].page; try { I.wait(20); if (scope == undefined) { if (isIEClick == undefined) { await CommonKeyword.click(newLabel); } else { if (typeof isIEClick == `boolean`) { if (isIEClick == true) { if (await DewElement.checkIfElementVisible(newLabel)) { } else { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(newLabel, { behavior: `auto`, block: `nearest`, inline: `center` }); } await I.waitForVisible(newLabel); await I.seeElement(newLabel); // await I.waitForClickable(newLabel) await I.click(newLabel); } else { await CommonKeyword.click(newLabel); } } else if (typeof isIEClick == `string`) { await within(isIEClick, async () => { await CommonKeyword.click(newLabel); }); } } } else { if (isIEClick == true) { await within(scope, async () => { if (await DewElement.checkIfElementVisible(newLabel)) { } else { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(newLabel, { behavior: `auto`, block: `nearest`, inline: `center` }); } await I.waitForVisible(newLabel); await I.seeElement(newLabel); await I.click(newLabel); }); } else { await within(scope, async () => { await CommonKeyword.click(newLabel); }); } } await logger.info(`Element Clicked: ` + label); } catch (err) { await logger.info(`Issue while performing operation in DDCC: Unable to Click element ` + newLabel); } } } /** * Click right on an element * * ```js * *CommonKeyword.rightClick("//input[@name='Search']","action") * ``` * * @param {String} selector located by CSS|XPath|strict locator. * @param {String} actionToPerform * */ static async rightClick(selectors: string, actionToPerform: string) { try { let selector = await z.getElements(selectors); await this.clickElement(selector); await z.executeScript(function (selector: string) { const el = document.evaluate(selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; const ev = el.ownerDocument.createEvent(`MouseEvents`); ev.initMouseEvent(`contextmenu`, true, true, el.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 2, null); el.dispatchEvent(ev); }, selector); await this.clickLabel(actionToPerform); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while performing right click ` + selectors); throw error; } } /** * To enter text into textfield macthed by field label * * ```js * *CommonKeyword.enterText("Name","Rhea") * ``` * * @param {String} fieldLbl of an element * @param {String} dataToFill to enter */ static async enterText(fieldLbl: string, dataToFill: string) { try { // await z.clearField(`.//p[contains(.,'${fieldLbl}')]/following-sibling::p/input|.//*[label[div[contains(.,'${fieldLbl}')]]]//input`); await z.fillField(`.//p[contains(.,'${fieldLbl}')]/following-sibling::p/input|.//*[label[div[contains(.,'${fieldLbl}')]]]//input`, dataToFill); // z.seeInField(`.//p[contains(.,'${fieldLbl}')]/following-sibling::p/input|.//*[label[div[contains(.,'${fieldLbl}')]]]//input`, dataToFill); } catch (error) { const textLocator = `.//p[contains(.,'${fieldLbl}')]/following-sibling::p/input|.//*[label[div[contains(.,'${fieldLbl}')]]]//input`; logger.log(`Issue while performing operation in DDCC: Error while entering text ` + textLocator); throw error; } } /** * To enter text into textfield based on Placeholder * * ```js * *CommonKeyword.enterTextUsingPlaceHolder("Search","John") * ``` * * @param {String} placeholder * @param {String} dataToFill to enter */ static async enterTextUsingPlaceHolder(placeholder: string, dataToFill: string) { try { // await z.clearField(`.//input[@placeholder='${placeholder}']`); await z.fillField(`.//input[@placeholder='${placeholder}']`, dataToFill); // z.seeInField(`.//input[@placeholder='${placeholder}']`, dataToFill); } catch (error) { const textPlaceholder = `.//input[@placeholder='${placeholder}']`; logger.log(`Issue while performing operation in DDCC: Error while entering text ` + textPlaceholder); throw error; } } /** * Performs a double-click on an element matched by link|button|label|CSS or XPath * * ```js * *CommonKeyword.doubleClick(`.btn.edit`) * ``` * * @param {String} locator matched by link|button|label|CSS or XPath * */ static async doubleClick(locator: string) { await z.doubleClick(locator); } /** * Performs javascript click on an element matched by link|button|label|CSS or XPath * * ```js * *CommonKeyword.forceClick(`.btn.edit`) * ``` * * @param {String} locator matched by link|button|label|CSS or XPath * */ static async forceClick(locator: string) { await z.forceClick(locator); } /** * Perform Click Operation * * ```js * *CommonKeyword.clickWrapper(`.btn.edit`) * ``` * * @param {String} locator matched by link|button|label|CSS or XPath * */ static async clickWrapper(locator: string) { const helper = await z.getHelper(); if (helper === webDriver) { await z.click(locator); } else if (helper === puppeteer) { const page = this.prototype.helpers[`Puppeteer`].page; page.click(locator); I.wait(10); } } /** *Scroll element into viewport. * * ```js * *CommonKeyword.scrollIntoView('#submit') * * ``` * @param {String} locator located by CSS|XPath|strict locator. * @param {string} scrollOption * */ static async scrollIntoView(locator: string, scrollOption?: any) { const helper = await z.getHelper(); if (helper === webDriver) { if (scrollOption == null) { await z.scrollIntoView(locator, { behavior: `auto`, block: `center`, inline: `center` }); } else { await z.scrollIntoView(locator, scrollOption); } } else if (helper === puppeteer) { if (scrollOption == null) { await I.scrollTo(locator); // await I.scrollIntoView(locator, { behavior: `auto`, block: `center`, inline: `center` }); } else { await I.scrollTo(locator); // await I.scrollIntoView(locator, scrollOption); } } } /** * Moves cursor to element matched by locator * * ```js * *CommonKeyword.moveCursorTo("#input") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {String} offsetX (optional) number X-axis offset. * @param {String} offsetY (optional) number Y-axis offset. * */ static async moveCursorTo(locator: string, offsetX?: number, offsetY?: number) { try { if (typeof offsetX !== `undefined` && typeof offsetY !== `undefined`) { await z.moveCursorTo(locator, offsetX, offsetY); } else { await z.moveCursorTo(locator) } logger.info(`Mouse Over Done`); } catch (error) { logger.info(`Issue while performing operation in DDCC: Mouse Over ` + locator); throw error; } } /** * Presses a key on a focused element. Special keys like 'Enter', 'Control', etc will be replaced with corresponding unicode * * ```js *CommonKeyword.pressKey('Enter'); *CommonKeyword.pressKey(['Control','a']); * ``` * * @param {string | Array } key or array of keys to press. * */ static async pressKey(...key: Array) { await z.pressKey(key); } /** * Element is used to switch in and out of iFrame * @param {string}locator */ static async switchTo(locator?: string) { if (locator == undefined) { await z.switchTo(); } else { await z.switchTo(locator); } } /** * Perform Javascript command * * ```js *CommonKeyword.executeScript('Enter'); * ``` * * @param {string} script * @param {...any} args to be passed to function. */ static async executeScript(script: string, ...args: any[]) { return z.executeScript(script, args); } /** * Reload the current page. * * ```js * *CommonKeyword.refreshPage() * ``` * */ static async refreshPage() { await z.refreshPage(); } /** * Scroll Page To Top. * * ```js * *await CommonKeyword.scrollPageToTop() * ``` * */ static async scrollPageToTop() { await z.scrollPageToTop(); } /** * Scroll Page To Top. * * ```js * *await CommonKeyword.amOnPage(`www.google.com`) * ``` * * @param {any} url */ static async amOnPage(url: any) { await z.amOnPage(url); } /** * Scroll Page To Bottom. * * ```js * *await CommonKeyword.scrollPageToTop() * ``` * */ static async scrollPageToBottom() { await z.scrollPageToBottom(); } /** * Method is use to click on dismiss button of CNS notification */ static async dismissNotification() { const locator = `//dew-notification-tray//button[@aria-label='Dismiss']`; try { if (await DewElement.checkIfElementVisible(`//dew-notification-tray//button[@aria-label='Dismiss']`)) { await CommonKeyword.clickElement(`//dew-notification-tray//button[@aria-label='Dismiss']`); await CommonKeyword.clickElement(`//dew-notification-tray//button[@aria-label='Yes']`); } else { logger.info(`CNS Notification not present`); } } catch (err) { logger.info(`Issue while performing operation in DDCC: Unable to perform dismiss notification ` + locator); throw err; } } /** * Scroll To Specified locator * * ```js * await CommonKeyword.scrollTo(`\\div`); * * @param {string}locator */ static async scrollTo(locator: string) { try { await z.scrollTo(locator); } catch (err) { logger.info(`Issue while performing operation in DDCC: Unable to scroll to ` + locator); throw err; } } /** * Click on element by verifying its browser * *```js * *await CommonKeyword.click() *``` * @param {String} locator located by CSS|XPath|strict locator. * */ static async click(locator: string) { const helper = await z.getHelper(); if (helper === webDriver) { await z.click(locator); } else if (helper === puppeteer) { try { const browser = this.prototype.helpers[`Puppeteer`].browser; const page = this.prototype.helpers[`Puppeteer`].page; const browserName = await browser.userAgent(); if (browserName.includes(`Chrome`) | browserName.includes(`Edge`)) { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(locator, { behavior: `smooth`, block: `center`, inline: `center` }); await I.waitForVisible(locator); await I.seeElement(locator); const element = await DewElement.getVisibleElement(locator); try { /* const ele = await DewElement.getElement(element); await ele[0].waitForClickable({ timeout: 50 * 1000 }); */ await I.say(`I wait for clickable ` + locator, `cyan`); } catch (error) { await CNS.dismissNotification(); await I.say(`I wait for clickable ` + locator, `cyan`); // await I.waitForClickable(element); } await I.say(`I wait for clickable ` + locator, `cyan`); // await I.waitForClickable(element); await I.click(element); } else if (browserName.includes(`Firefox`)) { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(locator, { behavior: `auto`, block: `center`, inline: `center` }); await I.waitForVisible(locator); await I.seeElement(locator); const element = await DewElement.getVisibleElement(locator); await I.click(element); } else { if (await DewElement.checkIfElementVisible(locator)) { } else { await page.evaluate(async () => { window.scrollBy(0, window.innerHeight); }); // await I.scrollIntoView(locator, { behavior: `auto`, block: `nearest`, inline: `center` }); } await I.waitForVisible(locator); await I.seeElement(locator); await jsClick(locator); } } catch (error) { throw error; } } } /** * * Perform Drag Operation and verify if element is present in UI * * ```js * await CommonKeyword.dragSlider(`//span[contains(text(),'Supplier Awarded Cost')]`,10); * ``` * @param {string}elementToDrag * @param {number}offset */ static async dragSlider(elementToDrag: string, offset: number) { let browser: any; const helper = await z.getHelper(); if (helper === webDriver) { browser = await this.prototype.helpers.WebDriver.browser; } else if (helper === puppeteer) { browser = await this.prototype.helpers.Puppeteer.browser; } const xoffset = await z.grabElementBoundingRect(elementToDrag, `x`); const yoffset = await z.grabElementBoundingRect(elementToDrag, `y`); if (browser.isW3C) { await browser.performActions([{ type: `pointer`, id: `pointer1`, parameters: { pointerType: `mouse` }, actions: [ { type: `pointerMove`, origin: this, x: Math.floor(xoffset), y: Math.floor(yoffset) }, { type: `pointerDown`, button: 0 }, { type: `pointerMove`, origin: this, x: offset, y: 0 }, { type: `pointerUp`, button: 0 }, ], }]); } else { await CommonKeyword.moveCursorTo(elementToDrag); await browser.buttonDown(0); await browser.moveToElement(null, offset, 0); } } /** * Fetch Cookie from browser */ static async getCookie() { const cookie: any = await z.grabCookie(`SAAS_COMMON_BASE_TOKEN_ID`); return cookie.value; } /** * Get home url of the setup */ static async getHomeURl() { await Wait.waitForDefaultTimeout(5); return await z.executeScript(`return sessionStorage.ddHomeUrl`); } /** * Clear Cookie from browser */ static async clearCookie() { await z.clearCookie(); } /** * This method return today's date if no paramater is passed in mm/dd/yyyy format * * ```js * await CommonKeyword.getDate(); // Returns todays date * await CommonKeyword.getDate(1); // Returns tommorows date * ``` * * @param {number}daysToAdd number of days to add to current date */ static async getDate(daysToAdd: number = 0) { const today = new Date(); today.setDate(today.getDate() + daysToAdd); const day = String(today.getDate()).padStart(2, `0`); const month = String(today.getMonth() + 1).padStart(2, `0`); // January is 0! const year = today.getFullYear(); return [month, day, year].join(`/`); } /** * Performs right click on a clickable element matched by semantic locator, CSS or XPath. * * ```js * await CommonKeyword.rightClick('Click me'); * * ``` * @param { string } locator clickable element located by CSS|XPath|strict locator. */ static async performRightClick(locators: string) { try { let locator = await z.getElements(locators) await this.clickElement(locator); await z.executeScript(function (locator: string) { const el = document.evaluate(locator, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; const ev = el.ownerDocument.createEvent(`MouseEvents`); ev.initMouseEvent(`contextmenu`, true, true, el.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 2, null); el.dispatchEvent(ev); }, locator); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while performing right click ` + locator); throw error; } } /** * Get current URL from browser. Returns Promise current URL * ```js * await CommonKeyword.grabCurrentUrl(); */ static async grabCurrentUrl() { try { const url = await z.grabCurrentUrl(); return url; } catch (error) { logger.log(`Issue while performing operation in DDCC: Error whilegrabbing current URL`); throw error; } } /** *Restarts browser when endpoint is unreachable * ```js * await CommonKeyword.restartBrowser(); */ static async restartBrowser() { const helper = await z.getHelper(); try { console.log(`========================>Inside TRY`); if (helper === webDriver) { await this.prototype.helpers[`WebDriver`].grabCurrentUrl(); } else if (helper === puppeteer) { await this.prototype.helpers[`Puppeteer`].grabCurrentUrl(); } } catch (err) { console.log(`========================>ERROR` + err); console.log(`========================>Inside CATCH`); console.log(`========================>Browser is not working and retarting the browser...`); if (helper === webDriver) { await this.prototype.helpers[`WebDriver`]._startBrowser(); } else if (helper === puppeteer) { await this.prototype.helpers[`Puppeteer`]._startBrowser(); } console.log(`========================>Browser is restarted...`); } } } module.exports = new CommonKeyword(); module.exports.CommonKeyword = CommonKeyword; // for inheritance /** * Perfrom Javascript click on the xpath * @param {string}xpath */ async function jsClick(xpaths: string) { let xpath = await z.getElements(xpaths) if (await isCSS(xpath)) { xpath = await cssToXPath(xpath); } await z.executeScript(function (xpath: string) { (document).evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click(); }, xpath); } /** * check if the locator is css or not * @param {string}locator * @return {string} */ function isCSS(locator: string) { return locator[0] === `#` || locator[0] === `.` && locator[1] !== `/`; }