Need to verify match only part of the string in 2 strings

Hi,

I have 2 strings that need to be matched in 2 different pages.
1st string-Order Number #1699871
2nd string- Order Number: 1699871

I don’t mind just matching the numbers.

def string1 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on order confirmed’))

def string2 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on My orders page’))

WebUI.verifyMatch(string1, string2, false)

I get the error:
Test Cases/Orders/Order Tab/Verify Order displayed in Orders tab FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Actual text ‘’ and expected text ‘Order Number: 1699872’ are not matched.

Any help would be appreciated. Thanks.

Let me know what this does…

String string1 = WebUI.getText(findTestObject('Gift OR/Order no displayed on order confirmed'))
println "*********" + string1

String string2 = WebUI.getText(findTestObject('Gift OR/Order no displayed on My orders page'))
println "*********" + string2

WebUI.verifyEqual(string1, string2)

WebUI.verifyMatch(string1, string2, false)

Really appreciate you quick response. I tried your solution, but still gives the error
Reason:
com.kms.katalon.core.exception.StepFailedException: Actual object ‘’ and expected object ‘Order Number: 1699873’ are not equal

Is this because the actual object is in the previous page and not reading the object ID?

Yes. I didn’t realize that was what you meant.

To provide you with a solid solution, I will need to see the code.

Here is my code:
WebUI.click(findTestObject(‘Object Repository/Gift OR/Page_Online Ordering/button_SAVE CONTINUE_1_2’))

WebUI.click(findTestObject(‘Object Repository/Gift OR/Page_Online Ordering/button_PLACE MY ORDER’))
//This is where the first string will be displayed
not_run: WebUI.verifyElementText(findTestObject(‘Object Repository/Gift order with CC OR/Page_Online Ordering/div_VISA - 1111 33.49’),
‘VISA - 1111 $33.49’)

WebUI.click(findTestObject(‘Object Repository/Gift order displayed in Order Page OR/Page_Online Ordering/span_suhani_k-icon k-menu-expand-arrow k-i-_1b4290’))
//second string displayed(next page)

WebUI.click(findTestObject(‘Object Repository/Gift order displayed in Order Page OR/Page_Online Ordering/span_Orders’))

String string1 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on order confirmed’))

String string2 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on My orders page’))

WebUI.verifyMatch(string1, string2, false)

WebUI.verifyElementPresent(findTestObject(‘Object Repository/Gift order displayed in Order Page OR/Page_Online Ordering/button_CANCEL ORDER’),
0)

I am not sure what you mean by “on the previous page”. You cannot try to get information on a web page that is not visible. You would have to get the data when it is visible, store it and then compare when you have both.

However, if I am mistaken, then since you can enter text, it may be either an input or textarea. So, instead of WebUI.getText(), may I suggest to use,

String string1 = WebUI.getAttribute(findTestObject('Gift OR/Order no displayed on order confirmed'), "value")

String string2 = WebUI.getAttribute(findTestObject('Gift OR/Order no displayed on My orders page'), "value")

WebUI.verifyMatch(string1, string2, false)

What this does is look to the value attribute of the element to find the text. This is good for inputs and textareas.

You should get the data on the page when it is visible , store it and then compare when you have both.

So I create an order, an order no is displayed(Order Number #1699871) which I have to match with the order no displayed in an orders tab(Order Number: 1699871). These numbers keep changing dynamically when new orders are placed. I need to do a string compare of both strings.

I would try the following…

First create two string GlobalVariables in your default profile.

//0n the first order page use this:
GlobalVariable.Order1 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on order confirmed’))
println “*********” + GlobalVariable.Order1

//0n the second order page use this:
GlobalVariable.Order2 = WebUI.getText(findTestObject(‘Gift OR/Order no displayed on My orders page’))
println “*********” + GlobalVariable.Order2

/Next execute the case and check the console view for the order outputs, if they match great; otherwise you may need to use Groovy - subString() - Tutorialspoint to capture your data (or other string method)./

//0n the second order page use these:
WebUI.verifyEqual(GlobalVariable.Order1, GlobalVariable.Order2)

WebUI.verifyMatch(GlobalVariable.Order1, GlobalVariable.Order2 , false)

1 Like

@Russ_Thomas gave you a method to compare Strings.

WebUI.verifyMatch(String1, String2, false)

I use this regularly myself. The last item, false, indicates that you want to directly compare the two strings, as compared to using Regular Expressions to find similarities. You could read up on RegEx for using sometime later.

Where I was concerned was your stating “the actual object is in the previous page and not reading the object ID”. You need to “capture” the order number when it is on the screen and the object ID exists. So after you place your first order, get/collect the order number then. When you get the second order number on the screen, get the second then. After you have the two Strings, then you can do your comparison. Note, if your Test Suite goes over several TCs, then you should do as @Dave_Evers suggests and use GlobalVariables.

Also, if the first order number is in a textbox, you may use (although you have it as a div):
String string1 = WebUI.getAttribute(findTestObject('Gift order with CC OR/Page_Online Ordering/div_VISA - 1111 33.49'), "value")

The above gets the text of the textbox via the value attribute of the element.

Last note, I don’t use WebUI.verifyEqual() with Strings. It expects Objects and although Strings are supposed to be Objects, KS doesn’t treat variables defined like, “String string1 = …”, as such. However, using GlobalVariables within verifyEqual() will work because they are Objects. Just a little detail I have found out.

1 Like