How to pass variables in a Text String to be validated on the UI

A new Katalon user. Appreciate any help I can get on this issue.
I have to verify a message which comes after clicking a Save button in my app, which has the highlighted values in my code screenshot below, say ‘FMC’ in a variable’.


On each iteration of my code those values change. Copying the code again:
WebUI.verifyElementText(findTestObject(‘AddStateRoutingRule/Page_WellDyne QuickLook/p_Your new Routing Rule was successfully im_328405’),
‘Your new Routing Rule was successfully implemented. – Partner: FMC – Type: Direct – Lane: FL-RDS – State: MA. Click to View’)

On the UI

Which other validation I could use or if I use verifyElementText what is the correct syntax to pass variables? Thanks for any suggestions.

String partner = ...
String type = ...
String lane = ...
String state = ...
Webui.verifyElementText(findTestObject(...), 'Your new Routing Rule was successfully implemented. – Partner:  ' + partner + '  – Type:  ' + type + '  – Lane:  ' + lane + '  – State:  ' + state + ' . Click to View')

Do you know how to get the text of the actual validation label? Like Brandon_Hein has above, you can do some String manipulation to get the various parts. As an example,

def routMsg = WebUI.getText(findTestObject('validation Label object'));
def temp = routMsg.split("Partner:")[1];
def partner = temp.split("-")[0];

temp = routMsg.split("Type:")[1];
def type = temp.split("-")[0];

temp = routMsg.split("Lane:")[1];
def lane = temp.split("-")[0];

temp = routMsg.split("State:")[1];
def lane = temp.split(".")[0];

Thankyou so much!!! It worked :slightly_smiling_face:

Thanks for explaining how to do this. Right now I am comparing to the exact text which appears in the message. In each iteration I have in my ‘for’ loop the values after say ‘Partner’ change, so I need to capture those values. With the suggestion by Brandon I was able to retrieve the values in the variables. Just one issue I am having now, I am not able to find a function which can change the text I am getting in one variable to a mixed case. For ex my variable has ‘DIRECT’ and I need to convert it to ‘Direct’ to match the text which appears in the UI. I could see UpperCase and Lowercase functions to do this but not a Initial capitilized function. Appreciate your help…

You can use the combo String functions below to get your text from “DIRECT” to become “Direct”.

type.toLowerCase().capitalize()

Thankyou so much!!! That worked but then other issue has come up with comparing strings. The string after test execution is exactly what is needed, but still a not matched error appears. I tried to copy both Expected and Actual strings to Notepad to compare. They look the same. My code is as below
WebUI.verifyElementText(findTestObject(‘AddStateRoutingRule/Page_WellDyne QuickLook/p_Your new Routing Rule was successfully im_328405’),
((((((('Your new Routing Rule was successfully implemented. –- Partner: ’ + vPartners) + ’ –- Type: ') + vOrderType1) +
’ –- Lane: ') + vLane) + ’ -– State: ') + vState) + ‘. Click to View.’)

Message from Katalon:
=============== ROOT CAUSE =====================
Caused by: com.kms.katalon.core.exception.StepFailedException: Actual text ‘Your new Routing Rule was successfully implemented. – Partner: PILLPACK – Type: Direct – Lane: FL-RDS – State: PA. Click to View.’ and expected text ‘Your new Routing Rule was successfully implemented. –- Partner: PILLPACK –- Type: Direct –- Lane: FL-RDS -– State: PA. Click to View.’

@pasthana Notepad is not the correct app to use to compare text. Notepad is too simple an app–I use it when I want to remove smart quotes or unprintable characters. Check out Beyond Compare to see differences in text (I use it quite regularly) or Notepad++ to see the unprintable characters like CR or LF (carriage returns (char13) or line feeds (char10)).

Here is your comparison below (I used Notepad++ to visualize the text):

I guess the actual uses a single dash and your text is using double dash.

I also cleaned up your statement removing all the non essential parenthesises:

WebUI.verifyElementText(findTestObject('AddStateRoutingRule/Page_WellDyne QuickLook/p_Your new Routing Rule was successfully im_328405'), 'Your new Routing Rule was successfully implemented. – Partner: ' + vPartners + ' – Type: ' + vOrderType1 +
' – Lane: ' + vLane + ' - State: ' + vState + '. Click to View.')

The app is displaying double dashes. I used Notepad ++ to compare before too in addition to Notepad

I modified my code to Expect one dash. It errored

Well, that’s interesting. Perhaps it could be the difference between an EN Dash to an EM Dash? Can you copy the text out from the actual HTML and paste it in your statement. Is there an ASCII difference between a dash and a minus sign? Or, maybe change the dashes to question marks and then use Regular Expression to compare.

Sure will try out your suggestion. There was another test where I had hardcoded the exact values in my test for the variable values, with dashes in front and that test had passed.

WebUI.verifyElementText(findTestObject(‘AddSameRuletwice/Page_WellDyne QuickLook/p_Route for this Partner, Order Type, and S_7f662e’),
‘Route for this Partner, Order Type, and State already exists. – Partner: FMC – Type: Direct – Lane: FL-RDS – State: MA’)

Interesting problem.

According to wikipedia there are many UNICODE characters that look - or , very similar each other but have different UNICODE code point.

U+002D - HYPHEN-MINUS
U+2010 ‐ HYPHEN
U+2212 − MINUS SIGN
U+2013 – EN DASH
U+2014 — EM DASH

In your target HTML, which - like character is used? — it is difficult to know.

How can you verify a message with those ambiguous - like characters? My bet is to disregard them. You need a trick. I will post my solution later.

I have made a sample Test Case TC1:

import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import my.StringUtils

// String text = WebUI.getText(findTestObject('AddStateRoutingRule/Page_WellDyne QuickLook/p_Your new Routing Rule was successfully im_328405'))
String text = "Your new Routing Rule was successfully implemented. \u2010 Partner: FMC \u2212 Type: Direct \u2013 Lane: FL-RDS \u2014 State: MA . Click to View"
println("text: " + text)

String escp = StringUtils.escapeNonAsciiChars(text)
println("escp: " + escp)

String partner = 'FMC'
String type = 'Direct'
String lane = 'FL-RDS'
String state = 'MA'

String pattern = ".*Partner.*" + partner +
					".*Type.*" + type +
					".*Lane.*" + lane +
					".*State.*" + state +
					".*"

WebUI.verifyMatch(text, pattern, true, FailureHandling.CONTINUE_ON_FAILURE)

Also I have made a custom keyword my.StringUtils:

package my

public class StringUtils {
	
	/**
	 * convert the input string 
	 * while escaping all non ASCII characters of which UNICODE code point is larger than 128
	 * 
	 * E.g.,
	 * String s = "Hello\u2010world"
	 * println(s)                                    // --> "Hello‐world"
	 * println(StringUtils.escapeNonAsciiChars(s))   // --> "Hello\u2010world"
	 */
	static String escapeNonAsciiChars(String str) {
		StringBuilder sb = new StringBuilder()
		for (int i = 0; i < str.length(); i++) {
			int codepoint = str.codePointAt(i)
			if (codepoint < 128) {
				sb.append(str.charAt(i))
			} else {
				sb.append("\\u").append(String.format("%04X", codepoint))
			}
		}
		return sb.toString()
	}
}

(1) How to check the type of - like character

When I ran the test case TC1, it first prints a message in the console:

text: Your new Routing Rule was successfully implemented. ‐ Partner: FMC − Type: Direct – Lane: FL-RDS — State: MA . Click to View
escp: Your new Routing Rule was successfully implemented. \u2010 Partner: FMC \u2212 Type: Direct \u2013 Lane: FL-RDS \u2014 State: MA . Click to View

In the text you find a few - like characters. They look so similar that you can not differentiate them.
In the escp you find those - like characters are converted into UNICODE-escape fomart (\uxxxx). You can find U+2010 ‐ HYPHEN, U+2212 − MINUS SIGN, U+2013 – EN DASH, U+2014 — EM DASH.

@pasthana
I would recommend to you to apply my.StringUtils.escapeNonAsciiChars(String str) method to your HTML text. You will find and be sure which of - like character is used there.

(2) How to verify a text in HTML while disregarding ambiguous - characters

As for text verification, I suppose that it is not significant for you which type of - like character is used in the text. You rather want to disregard them, don’t you?

You can use Regular Expression here in order to implement such a-bit-sophisticated verification. In the TC1 I used WebUI.verifyMatch(String actual, String expected, Boolean isRegex, FailureHandling). WebUI.verifyElementText() is not capable of performing matches with Regular Expression.

When I ran the TC1, it passes.

When I change String state = 'foo' and ran the TC1, it fails (as I expected) with the following message.

2021-01-12 14:55:40.319 ERROR c.k.k.core.keyword.internal.KeywordMain  - ❌ 
    Unable to verify match between actual text 
    'Your new Routing Rule was successfully implemented. ‐ Partner: FMC − Type: Direct – Lane: FL-RDS — State: MA . Click to View'
    and expected text 
    '.*Partner.*FMC.*Type.*Direct.*Lane.*FL-RDS.*State.*foo.*'
     using regular expression (
         Root cause: com.kms.katalon.core.exception.StepFailedException: Actual text 'Your new Routing Rule was successfully implemented. ‐ Partner: FMC − Type: Direct – Lane: FL-RDS — State: MA . Click to View' and expected text '.*Partner.*FMC.*Type.*Direct.*Lane.*FL-RDS.*State.*foo.*' are not matched using regular expression
1 Like

Thankyou so much for your suggestions. I’ll try them out… When I had my text hardcoded for say FMC, Direct I did not get any errors- actual and expected strings matched perfectly(including the dashes). Only when I had those values FMC, Direct in variables and passed a variable in my string I started having ‘not matched’ errors and test failed.

Then probably you have some mistakes in your test case script.

Sorry for a delayed reply. VerifyMatch worked for string comparisons in my script. Thankyou so much. Appreciate all the help. Happy Katalon user now :grinning: