Katalon | Clear Input Text

Priyank Shah
2 min readJul 8, 2019

Problem Statement:

I would like to clear Input Text/Value of an element. Is that sound easy? Don’t you believe it is a common usecase? What is the point to have blog on such problem statement. Hmmm.. It looks easy but practically it’s not. Let’s see what challenges are involved with respect to Katalon tool.

Quick Solution:

  • Select the text which we would like to delete (Ctl + A)
  • Press “Backspace” button (backspace)
Katalon Code (For Windows):--- WILL NOT WORK ON MAC ---WebUI.sendKeys(findTestObject(‘Object location’), Keys.chord(Keys.CONTROL, ‘a’))
WebUI.sendKeys(findTestObject(‘Object location’), Keys.chord(Keys.BACK_SPACE))

Is Above Solution Generic?

  • Yes — If we need to run automation test only on Windows environment, above snippet will work as expected.
  • No — Above code will NOT work as expected if we execute automation test on MAC machine.

What is Next? How to Fix this?

  • Ok, let’s understand why above script will not work on MAC.
  • For MAC, we have “COMMAND” button instead of “CONTROL” and “DELETE” button instead of “BACKSPACE” . So shall we just interchange “CONTROL” button with “COMMAND” and “BACKSPACE” with “DELETE” ?
Katalon Code (For MAC): --- WILL NOT WORK ON CHROME BROWSER ---@Keyword
def static clearElementText(TestObject to) {
try {
if(RunConfiguration.getOS().contains("Windows")) {
WebUI.sendKeys(to, Keys.chord(Keys.CONTROL, "a"))
WebUI.sendKeys(to, Keys.chord(Keys.BACK_SPACE))
}
else if (RunConfiguration.getOS().contains("Mac")) {
WebUI.sendKeys(to, Keys.chord(Keys.COMMAND, "a"))
WebUI.sendKeys(to, Keys.chord(Keys.BACK_SPACE))
}
} catch (WebElementNotFoundException e) {
KeywordUtil.markFailed("Element not found")
}
}
  • So we are again struck as above code will NOT work for Chrome browser on MAC. See the open issue here.

Proposed Solution:

  • We have seen the challenges involved just to clear the text of given field. Each above solution has some limitation.
  • At present (Till bug is not resolved by chromium), we need to go with JavaScript Executor. So steps would be -
  1. Get the locator of element.
  2. Set Value “Blank” through JavaScrip Executor.
class commonUtils {
/**
* Clear Input text of element
*/
@Keyword
def static clearElementText(TestObject to) {
WebElement element = WebUiCommonHelper.findWebElement(to,30)
WebUI.executeJavaScript("arguments[0].value=''", Arrays.asList(element))
}
}

Note: I have created custom keyword so I can utilize same method across the project. Interested to build Custom Keyword. Fore more info, Refer here:

Now, we can just call from script like below,

TestObject to = findTestObject('location')
commonUtils.clearElementText(to)

Conclusion:

  • Using JavaScript Executor, we are able to “Clear Text” and it will work for both Windows, Mac and also supports all browsers.

--

--

Priyank Shah
Priyank Shah

Written by Priyank Shah

Agile Product Leader | Delivery Manager | Design Thinker (PRINCE2, CSPO™, CSM™, SFC™, ISTQB)

Responses (5)