Extract number from a string

AppNumMP

Hi all, i doing an automation testing using katalon. I need to know how to extract ONLY NUMBER from a string. For instance i wanted to extract ‘1421’ from “Application number: 1421” from the above page. The application number changes for every single testing.

I think you could following solution to get the number: https://stackoverflow.com/questions/15572481/extract-numeric-data-from-string-in-groovy

1 Like

Hi Hmaraj,

Since it is a string and “Application Number:” is constant, you can try using subtring(starting char, ending char) or replaceAll("[^0-9.]", “”)

Something like this:

string extractInt = WebUI.getText(findTestObject(“your_Test_object”)).substring(20, 24)
println extractInt

OR

string extractInt = WebUI.getText(findTestObject(“your_Test_object”)).replaceAll("[^0-9.]", “”)
println extractInt

Hope that helps. . . :slight_smile:

6 Likes

I use copy your line, But not workiing,
string extractInt = WebUI.getText(findTestObject(‘myobject’)).substring(20, 24)
println extractInt

May I see your code?

It says no signature of method… did you include the “WebUI”??

1 Like

i did.

string extractInt = WebUI.getText(findTestObject(‘Mportal/Page_AEON Credit Service (Asia)/p_Application Number 1421’)).substring(20, 24)
println extractInt

I replicated your error…

make the first letter of string a capital one like this: String

Katalon doesnt recognize “string” with a small “s”

Try it. . . .

2 Likes

and BTW as you can see in your error logs, you can see the “values: [1428]” means it successfully eliminates the letters in your string.

you can also try to use the .replaceAll("[^0-9.]"), just in case the application number exceeds to 4 digits. . .

Sorry if I wrongly typed “string” earlier in my code. . . .

Hope that it works now on your side . . .

1 Like

Bro, you’re amazing!!! It’s working like a charm!

1 Like

Glad I helped!

CHEERS! :beers:

1 Like

Hi, similar scenario but this time i want to extract number from stings in .JSP page. (Which dont have objects!!). I tried use your previous code but failed. I just want get 4 DIGIT NUMBERs FROM the “TRANSACTION ID”. How can i do this??

Hi hmaraj,

What do you mean by this?

Could you send here the source of the page?

Thanks

i meant the (Object Repository). Normally when doing web recording we capture the object by GetObject right? But this page cant able capture anything. Thats why i said, “it dont have objects”.

Below is the page source.

------------------------ Getting Parameter for merchant return page using method POST ---------------------- <br>
<br>AMOUNT = 30.00<br>
<br>AUTH_DATE = <br>
<br>AUTH_ID = <br>
<br>CAPTURE_DATE = <br>
<br>CUSTOMER_ID = Cust001<br>
<br>FR_LEVEL = -1<br>
<br>FR_SCORE = -1.0<br>
<br>MERCHANT_TRANID = <br>
<br>RESPONSE_CODE = 1005<br>
<br>RESPONSE_DESC = Invalid MERCHANT_ACC_NO&#x21; &#xd;&#xa; Unable to find merchant with provided MERCHANT_ACC_NO.<br>
<br>TRANSACTION_ID = <br>
<br>TRAN_DATE = <br>
<br>TXN_STATUS = <br>
<br>SECURE_SIGNATURE = c09d1215df1789f962dbff9bc0fa6fd2cbd7f4e9b6cde5fa197548997dc1da110f9c44bc53665f4ed2455a0c03004c66e705fb07dc08d18e6c6411df7388c90f<br>
--------------- End of Getting Parameters --------------------

Moreover, when inspect the page, there is no Xpath, element & what so ever.

Actually, based on this link:

https://docs.katalon.com/katalon-studio/tutorials/detect_elements_xpath.html#how-to-identify-nested-elements

On the down most part of the page, you can follow the pattern there.

Like creating your own xpath:

.//br[text()='TRANSACTION_ID']

make that your object locator

Try it. . .

Tried but failed. Where did it went wrong?

String extractInt = WebUI.getText(findTestObject(‘pay_win_auth_2/Transaction_id’)) .substring(15, 18)
println(extractInt)

@Arnel any idea bro??

Hi hmaraj,

Sorry late response… Quite busy on my work.

You can try this approach:

Try to explore and play with it, do some refactoring something like:

import org.openqa.selenium.WebDriver as WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.By

WebDriver driver = DriverFactory.getWebDriver()
WebElement element = driver.findElements(By.tagName("<br>"))

I have a feeling that since it doesn’t have any objects, TagName is your key.

Try it.

Hope that helps. . .:slight_smile:

1 Like

Its works!!! Thank you again brother!!!

Oh, very nice! you’re welcome… Glad I helped. :slight_smile:

Cheers! :beers:

1 Like