import { CommonKeyword } from './commonKeyword'; import { DewElement } from './element'; const { I } = inject(); /** * Text field class */ export class TextField { /** * To enter text in text field * * ```js * * TextField.enterText("Project Name","Project_123"); * ``` * * @param {String} fieldLbl //Label of the input box * @param {String} valueToEnter // Value you want to enter into the text box * */ static async enterText(fieldLbl: string, valueToEnter: string | number) { await I.fillField(`.//p[contains(.,'${fieldLbl}')]/following-sibling::p/input| .//*[label[div[contains(.,'${fieldLbl}')]]]//input[not(contains(@class,'hideInput'))]`, valueToEnter.toString()); } /** * To enter text in text field using the placeholder attribute * * ``js * TextField.enterTextUsingPlaceHolder("Enter Project Name","Project_123"); * ``` * * @param {String} placeholder // input Box placeholder * @param {String} valueToEnter //Value to enter into the textbox * */ static async enterTextUsingPlaceHolder(placeholder: string, valueToEnter: string | number) { const element = await DewElement.getVisibleElement(`.//input[@placeholder[normalize-space()='${placeholder}']]`); await DewElement.verifyIfISeeElement(element); // await I.seeElement(element); await I.clearField(element); await I.fillField(element, valueToEnter.toString()); } /** * To enter text in text field by passing locator * * ``js * TextField.enterTextUsingLocator("//input[@id='emailID']","Project_123"); * ``` * * @param {any} locatorParam located by xpath | CSS * @param {String} dataToFill //Value to enter into the textbox * */ static async enterTextUsingLocator(locatorParam: any, dataToFill: string | number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await DewElement.verifyIfISeeElement(locator); const visibleElement = await DewElement.getVisibleElement(locator); await I.clearField(visibleElement); await I.fillField(visibleElement, dataToFill.toString()); // await I.seeInField(locator, dataToFill); } /** * To enter text in text field by passing locator * * ``js * TextField.enterTextUsingLocator("//input[@id='emailID']","Project_123"); * ``` * * @param {any} locatorParam located by xpath | CSS * @param {String} dataToFill //Value to enter into the textbox * */ static async clearFieldenterTextUsingLocator(locatorParam: any, dataToFill: string | number) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await DewElement.verifyIfISeeElement(locator); const visibleElement = await DewElement.getVisibleElement(locator); await I.executeScript(` var elFrom = document.evaluate("${visibleElement}", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue elFrom.value=""; `, visibleElement); await I.fillField(visibleElement, dataToFill.toString()); // await I.seeInField(locator, dataToFill); } /** * Types out the given string or the array of keys provided. * * ```js * When passing in a string * await Textfield.type('//div/input','Type this out.'); * * When passing in an array * await Textfield.type('//div/input',['T', 'E', 'X', 'T']); * ``` * * @param { any } locatorParam xpath of the input box * @param {string | Array } key or array of keys to type. Type out given array of keys or a string of text * */ static async type(locatorParam: any, key: string | Array) { let locator: string; if (Array.isArray(locatorParam)) { locator = locatorParam[0]; } else { locator = locatorParam; } await CommonKeyword.clickWrapper(locator); I.type(key); } /** * Types out the given text into an active field. * * ```js * When passing in a string * await Textfield.typeText('Type this out.'); * * When passing in an array * await Textfield.typeText(['T', 'E', 'X', 'T']); * ``` * * @param {string | Array } key or array of keys to type. Type out given array of keys or a string of text * */ static async typeText(key: string | Array) { await I.type(key); } } module.exports = new TextField(); module.exports.TextField = TextField;