import { lmt } from "../Helpers/readI18NProp"; import { logger } from '../Logger/logger'; import { CommonKeyword } from './commonKeyword'; import { DewButton } from "./dewButton"; import { DewElement } from "./element"; import { TextField } from './textfield'; import { z } from "./z"; // import { Wait } from "./dewWait"; const { I } = inject(); /** * Checkbox class */ export class Checkbox { /** * Returns if checkbox is selected or not * ```js * Checkbox.iSCheckBoxSelected("#checkbox") * ``` * @param {String} locator located by CSS|XPath|strict locator. */ static async iSCheckBoxSelected(locator: string) { try { await DewElement.verifyIfISeeCheckboxIsChecked(locator); return true; } catch (err) { return false; } } /** * Returns true if checkbox is selected, else returns false * ```js * Checkbox.isCheckboxChecked("//input[@type='checkbox']") * ``` * @param {String} locator located by CSS|XPath|strict locator. */ static async isCheckboxChecked(locator: string) { const ifChecked = await DewElement.grabAttributeFrom(locator, `checked`); if (ifChecked.includes(`true`)) { return true; } else { return false; } } /** * Verifies that the specified checkbox is not checked. * ```js * Checkbox.dontSeeCheckboxIsChecked("//input[@type='checkbox']") * ``` * @param {String} locator located by label|name|CSS|XPath|strict locator. */ static async dontSeeCheckboxIsChecked(locator: string) { await z.dontSeeCheckboxIsChecked(locator); } /** * To select all checkboxes * * ```js * * Checkbox.selectAll("Invoice Status") * ``` * * @param {String} checkboxCollectionHeader header label * */ static async selectAll(checkboxCollectionHeader: string) { try { await CommonKeyword.clickElement(`.//div[label[contains(text(),'${checkboxCollectionHeader}')]]/following-sibling::div//label[contains(text(),'${await lmt.getLabel(`SelectAll`)}')]`); await DewButton.click(await lmt.getLabel(`Apply`)); } catch (error) { logger.log(`Issue while performing operation in DDCC: Unable to click on Select All checkbox`); throw error; } } /** *  To Search and select a checkbox * ```js * await Checkbox.searchSelect("Status","Invoice Status","Approved") * await Checkbox.searchSelect("Status","Invoice Status") * ``` * @param {string}checkboxCollectionHeader * @param {string[]}checkboxToSelect */ static async searchSelect(checkboxCollectionHeader: string, ...checkboxToSelect: string[]) { // Enter name of checkbox to select in the search field try { for (const checkboxValue of checkboxToSelect) { await TextField.enterTextUsingLocator(`.//dew-checklist//input[@placeholder[normalize-space()='${await lmt.getLabel(`Search`)}']]`, checkboxValue); /* Wait for 5 seconds to reduce the listed checkboxes list to checkbox(es) as per entered criteria TODO - verify listed checkboxes list reduces to checkbox(es) as per entered criteria */ await CommonKeyword.clickElement(`.//dew-checklist-filter[div[label[contains(.,'${checkboxCollectionHeader}')]]]//dew-checkbox/label[contains(.,'${checkboxValue}')]`); await DewElement.verifyIfISeeCheckboxIsChecked(checkboxValue); } await DewButton.click(await lmt.getLabel(`Apply`)); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while doing search and select on checkbox`); throw error; } } /** * To select a particular checkbox directly without searching * ```js * await Checkbox.selectDirectly("Invoice Status","Not Invoiced","Approved") * await Checkbox.selectDirectly("Invoice Status","Not Invoiced") * ``` * @param {string}checkboxCollectionHeader * @param {string[]}checkboxToSelect */ static async selectDirectly(checkboxCollectionHeader: string, ...checkboxToSelect: string[]) { // Select Direct Type try { for (const checkBoxValue of checkboxToSelect) { await CommonKeyword.clickElement(`.//dew-checklist-filter[div[label[contains(.,'${checkboxCollectionHeader}')]]]//dew-checkbox/label[contains(.,'${checkBoxValue}')]`); await DewElement.verifyIfISeeCheckboxIsChecked(checkBoxValue); } await DewButton.click(await lmt.getLabel(`Apply`)); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error while selecting checkbox directly`); throw error; } } /** * To select multilevel checkbox * * ```js * * Checkbox.selectMultilevelCheckbox("In Process","In Approval") * ``` * * @param {*} param for checkbox to select * @param {...any} multiple Values for checkbox to select (array) */ static async selectMultilevelCheckbox(param: string, ...params: any) { try { let level = 1; const toggleCountParam = await DewElement.grabNumberOfVisibleElements(`.//span[contains(@class,'toggle-children-wrapper')][../../div//dew-checkbox/input[@type='checkbox']/../label[contains(text(),'${param}')]]`); if (toggleCountParam > 0) { await CommonKeyword.click(`.//div[contains(@class,'level-${level}')]//span[contains(@class,'toggle-children-wrapper')][../../div//dew-checkbox//label[contains(text(),'${param}')]]`); if (params.length > 0) { for (const status of params) { ++level; const toggleCountParams = await DewElement.grabNumberOfVisibleElements(`.//span[contains(@class,'toggle-children-wrapper')][../../div//dew-checkbox/input[@type='checkbox']/../label[contains(text(),'${status}')]]`); if (toggleCountParams > 0) { /* await Wait.waitForDefaultTimeout(5); await Wait.waitUntilVisibilityOfElement(`.//div[contains(@class,'level-${level}')]//span[contains(@class,'toggle-children-wrapper')][../../div//dew-checkbox//label[contains(text(),'${status}')]]`); */ await CommonKeyword.click(`.//div[contains(@class,'level-${level}')]//span[contains(@class,'toggle-children-wrapper')][../../div//dew-checkbox//label[contains(text(),'${status}')]]`); } else { /* await Wait.waitForDefaultTimeout(5); await Wait.waitUntilVisibilityOfElement(`.//div[contains(@class,'level-${level}')]/tree-node-wrapper//dew-checkbox//label[contains(text(),'${status}')]`); */ await CommonKeyword.click(`.//div[contains(@class,'level-${level}')]/tree-node-wrapper//dew-checkbox//label[contains(text(),'${status}')]`); await DewElement.verifyIfISeeCheckboxIsChecked(status); } } } } else { await CommonKeyword.click(`.//dew-checkbox/input[@type='checkbox']/../label[contains(text(),'${param}')]`); await DewElement.verifyIfISeeCheckboxIsChecked(param); } await DewButton.click(await lmt.getLabel(`Apply`)); } catch (error) { logger.log(`Issue while performing operation in DDCC: Error While Selecting multilevel checkbox`); throw error; } } }