What you are experiencing is one of the most common hurdles when transitioning from manual testing to automation, so don’t worry—your logic is sound, you’re just using the wrong tool for this specific HTML structure.
What is Going Wrong?
In web development, different HTML elements store their visible content differently:
-
Standard text elements (like <div>, <span>, <p>, or <td>) store their text as innerHTML (between the opening and closing tags). This is what Katalon’s WebUI.getText() is designed to read.
-
Form elements (like <input>, <textarea>, or text boxes) store their displayed text inside a hidden runtime property called value. Because the text isn’t actually between standard HTML tags, WebUI.getText() looks inside and finds absolutely nothing, returning a blank string.
The Simple Solution
To get text from an input field in Katalon Studio, you need to use WebUI.getAttribute() instead of WebUI.getText(), and specifically ask for the "value" attribute.
Instead of:
Groovy
String totalPrice = WebUI.getText(findTestObject('Page_Checkout/input_TotalPrice'))
Use this:
Groovy
String totalPrice = WebUI.getAttribute(findTestObject('Page_Checkout/input_TotalPrice'), 'value')
Reusable Custom Keyword Solution
To ensure your test scripts stay clean, scalable, and easy for anyone on your team to use without remembering this HTML quirk, you can create a Custom Keyword. This keyword acts as a smart wrapper that automatically falls back to fetching the input value if standard text returns empty.
Step 1: Create the Custom Keyword
Create a new Keyword package/class (e.g., com.helper.SmartWebUI) and paste the following code:
Groovy
package com.helper
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
public class SmartWebUI {
/**
* Dynamically fetches text from an element, automatically handling standard text tags and input fields.
* @param testObject The Katalon Test Object to extract text from.
* @return String containing the element's text or input value.
*/
@Keyword
def static String getElementText(TestObject testObject) {
// First, try the standard getText() method
String visibleText = WebUI.getText(testObject)
// If it's empty, check if it's an input field by grabbing the 'value' attribute
if (visibleText == null || visibleText.trim().isEmpty()) {
visibleText = WebUI.getAttribute(testObject, 'value')
}
return visibleText
}
}
Step 2: Use it in your Test Script (Script Mode)
Now, in your test cases, you can call this custom keyword seamlessly:
Groovy
// Call your custom keyword instead of standard WebUI
String totalPrice = CustomKeywords.'com.helper.SmartWebUI.getElementText'(findTestObject('Page_Checkout/input_TotalPrice'))
// Now you can safely print or verify the value!
WebUI.comment("The captured price is: " + totalPrice)