import { lmt } from "../Helpers/readI18NProp"; import { logger } from '../Logger/logger'; import { CommonKeyword } from "./commonKeyword"; import { DewLoader } from "./dewLoader"; // import { Wait } from './dewWait'; import { DewElement } from "./element"; import { TextField } from "./textfield"; const { I } = inject(); /** * Calendar works */ export class DatePicker { /** * To select today's date * * ```js * * DatePicker.selectToday("//input[@aria-label='date']") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {boolean}IEClick pass true if you want to perform z.click for IE Browser * */ static async selectToday(locator: string, IEClick?: boolean) { try { if (IEClick === undefined) { IEClick = false; } await CommonKeyword.clickElement(locator); await CommonKeyword.clickElement(`.currentDate`, IEClick); // verfication await verifyDateTime(locator, await CommonKeyword.getDate()); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting todays date`); throw error; } } /** * To select provided date * * ```js * * DatePicker.selectInNextMonth("//input[@aria-label='date']","15") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {string} date * @param {boolean} IEClick pass true if you want to perform z.click for IE Browser */ static async selectInNextMonth(locator: string, date: string = `15`, IEClick: boolean = false) { try { if (IEClick === undefined) { IEClick = false; } await CommonKeyword.clickElement(locator); await CommonKeyword.clickElement(`//button[@aria-label='show next month']`, IEClick); await CommonKeyword.clickElement(`//span[contains(@class, 'd-picker-body')]//button/div[text()='${date}']`, IEClick); // await CommonKeyword.clickLabel(date, IEClick); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selcting date in next month`); throw error; } } /** * To select date * * ```js * * DatePicker.selectDate("//input[@aria-label='date']","01/25/2020") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {String} date in MM/DD/YYYY date format * @param {boolean}IEClick pass true if you want to perform z.click for IE Browser */ static async selectDate(locator: string, date: string, IEClick?: boolean) { try { const detailsAPI: any = await lmt.getUserLangCode(); const lang: string = detailsAPI[`langCode`]; const langValue = lang.replace(`_`, `-`); const dateObj = new Date(Date.parse(date)); const month = dateObj.toLocaleString(`en-GB`, { month: `long` }); // months from 1-12 const day = dateObj.getDate().toLocaleString(langValue); const year = dateObj.getFullYear().toLocaleString(langValue) .replace(`,`, ``); if (IEClick === undefined) { IEClick = false; } await CommonKeyword.clickElement(locator); await selectYear(year.toString(), IEClick); await selectMonth(await lmt.getLabel(month), IEClick); await selectDay(day.toString(), IEClick); // verification await verifyDateTime(locator, date); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting date`); throw error; } } /** * The function is used to select time from calendar object. It accept time in 24 hr format (23:24) * * e.g. DatePicker.selectOnlyTime("//input[@aria-label='date']","20:20") * @param {string} locator * @param {string}time * @param {boolean}IEClick */ static async selectOnlyTime(locator: string, time: string, IEClick?: boolean) { try { if (IEClick === undefined) { IEClick = false; } await CommonKeyword.clickElement(locator, IEClick); await selectTime(time, IEClick); // verifyTime await verifyTime(locator, time); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting time in calendar`); throw error; } } /** * To select date and time * * ```js * * DatePicker.selectDateAndTime("//input[@aria-label='date']","02/20/2020","12:50") * ``` * * @param {String} locator located by CSS|XPath|strict locator. * @param {String} date In MM/DD/YYYY date format * @param {String} time In 24 hours format hh/mm * @param {boolean}IEClick pass true if you want to perform z.click for IE Browser */ static async selectDateAndTime(locator: string, date: string, time: string, IEClick?: boolean) { try { const detailsAPI: any = await lmt.getUserLangCode(); const lang: string = detailsAPI[`langCode`]; const langValue = lang.replace(`_`, `-`); const dateObj = new Date(Date.parse(date)); const month = dateObj.toLocaleString(langValue, { month: `long` }); // months from 1-12 const day = dateObj.getDate().toLocaleString(langValue); const year = dateObj.getFullYear().toLocaleString(langValue) .replace(`,`, ``); if (IEClick === undefined) { IEClick = false; } await CommonKeyword.clickElement(locator); await selectYear(year.toString(), IEClick); await selectMonth(await lmt.getLabel(month), IEClick); await selectDay(day.toString(), IEClick); // await Wait.waitForDefaultTimeout(5); await selectTime(time, IEClick); // Verifying the pop-up const popup = await DewElement.grabNumberOfVisibleElements(`//dew-modal[@class='show modal fade']//div[@class='modal-content']`); if (popup > 0) { const modal = await DewElement.grabNumberOfVisibleElements(`//div[contains(@class,'modal-header flex-column')]//h5[contains(text(),'Date')]|//div[contains(@class,'modal-header flex-column')]//h5[contains(text(),'date')]`); if (modal > 0) { await CommonKeyword.clickElement(`//div[contains(@class,'modal-header flex-column')]//h5[contains(text(),'Date')]|//div[contains(@class,'modal-header flex-column')]//h5[contains(text(),'date')]`); } else { /* const datetext = await DewElement.grabNumberOfVisibleElements(``) if () */ await TextField.enterTextUsingLocator(`(//div[@class='modal-content']//textarea)[1]`, `Test`); } /* const flag = await DewElement.grabNumberOfVisibleElements(`//div[contains(text(),'date and time')]|//div[contains(text(),'Date and Time')]`); if (flag>0){ await CommonKeyword.clickElement(`//div[contains(text(),'date and time')]|//div[contains(text(),'Date and Time')]`); } else{ await CommonKeyword.clickElement(`//dew-col[@class = 'col-2']`); } */ } else { await CommonKeyword.pressKey(`Escape`); } // Verification of date and time await verifyDateTime(locator, date, time); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting date and time`); } } } /** * Select Year from calender * * @param {string} year * @param {boolean} IEClick */ async function selectYear(year: string, IEClick?: boolean) { try { await CommonKeyword.clickElement(`//dew-btn[contains(@class,'year')]/button`, IEClick); await CommonKeyword.clickElement(`//div[contains(@class,'year-scroll')]//button/div[text()='${year}']`, IEClick); await DewElement.verifyIfISeeElement(`//dew-btn/button[@aria-label='${year}']`); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting Year`); throw error; } } /** * Select Month from calender * * @param {string} month * @param {boolean} IEClick */ async function selectMonth(month: string, IEClick?: boolean) { try { await CommonKeyword.clickElement(`//dew-btn[contains(@class,'month')]/button`, IEClick); await DewElement.verifyIfISeeElement(`//div[contains(@class,'month-header')]`); await CommonKeyword.clickElement(`//span[contains(@class,'month')]//button/div[text()='${month}']`, IEClick); await DewElement.verifyIfISeeElement(`//dew-btn/button[@aria-label='${month}']`); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting month`); throw error; } } /** * Select Day from calender * * @param {string} day * @param {boolean} IEClick */ async function selectDay(day: string, IEClick?: boolean) { try { await DewElement.verifyIfISeeElement(`//span[contains(@class, 'd-picker-body')]`); await CommonKeyword.clickElement(`//span[contains(@class, 'd-picker-body')]//button/div[text()='${day}']`, IEClick); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting day`); throw error; } } /** * The Function is used to verify date and time passed in parameter is matching with actual date and time selected. * @param {String} locator located by CSS|XPath|strict locator. * @param {String} date In MM/DD/YYYY date format * @param {String} time In 24 hours format hh/mm */ async function verifyDateTime(locator: string, date: string, time?: string) { await DewLoader.waitForSpinner(); const grabbedDateTime = (await DewElement.grabValueFrom(locator)).toString(); const grabbedDate = await grabbedDateTime.split(` `)[0]; // parses the grabbed date in MM/DD/YYYY format const grabbedDateObj = new Date(Date.parse(grabbedDate)); const grabbedDay = String(grabbedDateObj.getDate()).padStart(2, `0`); const grabbedMonth = String(grabbedDateObj.getMonth() + 1).padStart(2, `0`); // January is 0! const grabbedYear = grabbedDateObj.getFullYear(); const parsedGrabbedDate = `${grabbedMonth}/${grabbedDay}/${grabbedYear}`; // Parses the date passed in parameter in MM/DD/YYYY Format const dateObj = new Date(Date.parse(date)); const day = String(dateObj.getDate()).padStart(2, `0`); const month = String(dateObj.getMonth() + 1).padStart(2, `0`); // January is 0! const year = dateObj.getFullYear(); const parsedEnteredDate = `${month}/${day}/${year}`; if (await grabbedDate == date || parsedGrabbedDate == parsedEnteredDate) { logger.info(`Date passed in parameter ${date} matched with grabbed date ${grabbedDate}`); await I.say(`Date passed in parameter ${date} matched with grabbed date ${grabbedDate}`, `cyan`); if (time !== undefined) { const grabbedTime = await grabbedDateTime.split(` `)[1]; if (await grabbedTime == time) { await I.say(`Time passed in parameter is ${time} matched with grabbed time ${grabbedTime}`, `cyan`); } else { await I.say(`Time passed in parameter is ${time} not matched with grabbed time ${grabbedTime}`, `cyan`); } } } else { await I.say(`Date passed in parameter ${date} not matched with grabbed date ${grabbedDate}`, `cyan`); } } /** * The Function is used to verify time passed in parameter is matching with actual time selected. * @param {String} locator located by CSS|XPath|strict locator. * @param {String} time In 24 hours format hh/mm */ async function verifyTime(locator: string, time: string) { await DewLoader.waitForSpinner(); const grabbedDateTime = (await DewElement.grabValueFrom(locator)).toString(); let grabbedTime = await grabbedDateTime; if (await grabbedDateTime.includes(` `)) { grabbedTime = await grabbedDateTime.split(` `)[1]; } if (await grabbedTime == time) { await I.say(`Time passed in parameter is ${time} matched with entered time ${grabbedTime}`, `cyan`); } else { await I.say(`Time passed in parameter is ${time} not matched with entered time ${grabbedTime}`, `cyan`); } } /** * Select Time from calender * * @param {string} time * @param {boolean} IEClick */ async function selectTime(time: string, IEClick?: boolean) { try { const hour = time.split(`:`)[0]; const minute = time.split(`:`)[1]; await CommonKeyword.clickElement(`//p[@title='24 hrs'] | //p[@title='12 hrs']`, IEClick); await CommonKeyword.clickElement(`//div[@title='24 hrs']`, IEClick); await CommonKeyword.clickElement(`//dew-timepicker//input[@formcontrolname='hour']`, IEClick); await CommonKeyword.clickElement(`//div[contains(@class,'dropdown-menu show')]//a[contains(text(),'${hour}')]`, IEClick); await CommonKeyword.clickElement(`//dew-timepicker//input[@formcontrolname='minute']`, IEClick); await CommonKeyword.clickElement(`//div[contains(@class,'dropdown-menu show')]//a[contains(text(),'${minute}')]`, IEClick); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting time in calendar`); throw error; } }