Reading the Dynamic Number

Hi,
Quick question, I want to read the number alone which is middle of the text in web page.
For example…“Quote # 39281 created successfully”. How I can read only the 39281 number from the web page.

can you please suggest with the syntax i.e,
WebUI.getText(findTestObject(name of property)) !!

Hello,

you can do it using a little bit of regex.

String txt = WebUI.getText(testObjectWithMessage)

Pattern pattern = Pattern.compile("[0-9]+")
Matcher matcher = pattern.matcher(txt)
String s = ""
if (matcher.find()) {
	s = matcher.group()
} else {
	KeywordUtil.markWarning("No number was found in a text.")
}

Please note, that current code takes the first number occurrence in the message, so if there are 2 or more numbers, only first one is stored.

1 Like

Thanks for your reply, Marek.
I observed now that “testObjectWithMessage” is coming along with the number which number changes every time. So, I cannot store that in object repository. how do I store it ?

String txt = WebUI.getText(testObjectWithMessage)

So you have some dynamic ID in your XPath, right? Can you share the HTML source code to see how we can identify your element?

Thank you. Please find the below code.
The text displays along with the dispute number is “The dispute for system tracking # 1004100 has been submitted successfully”.

<td width="100%" class="grid">
<!--- BeginOptional name="TaskConfirmation" --->
<!-- InstanceBeginEditable name="TaskConfirmation" -->
<!--************ uitmplbegin: mTC ************-->
<table cellspacing="0" cellpadding="2" border="0" class="mTC">
<tr class="msgConfirm">
<td> <img src="?appId=gmpdispute&amp;flName=/uitmpl/en/img/confirmation.gif" width="29" height="29"
		border="0" alt="Confirmation." /></td><td>The dispute for system tracking # 1004100 has been submitted successfully. </td>
	</tr>
</table>

What about this XPath - //tr[@class='msgConfirm']/td[2]

Are you able to get the text you want using this selector?
Or you can be even more specific - if the rest of the message is always the same.

//tr[@class='msgConfirm']/td[contains(text(), 'The dispute for system tracking')]

@vdavuluri2

I have created a keyword which seperated out the digits

public class TakeOutNumbersOnly {
@Keyword
def String numberFromString(String str) {
String numbers= str.replaceAll("[^0-9]", “”)
}
}

Hope this helps!

Thank You, Marek & Kumar for your clarifications.

Kumar - @Keyword - where do we need to use this ?

@vdavuluri2 -
You can use the keyword in your test script
Eg:
CustomKeywords.‘seperateDigits.TakeOutNumbersOnly.numberFromString’(“check123”)

hi,

or as a method in a test case
def String numberFromString(String str) {
	String numbers= str.replaceAll("[^0-9]", "")
	}

println numberFromString("Quote # 39281 created successfully")

check this page

Hi,
Thanks for above clarifications. To get the above unique number, I have to read the text first. But I am unable to read the text from the page.

Cucumber Code:
Then it should displays create inquiry pages

Step Definition to read the text
@Then(“it should displays create inquiry pages”)
def it_should_displays_create_inquiry_page2() {
inquirytext = WebUI.getText(findTestObject(By.xpath(’//td/table/tbody/tr/td’)))
println " TRACKING NUMBER inquirytext " + inquirytext
}

Log:
2019-06-25 16:05:26.554 ERROR c.k.k.c.c.keyword.CucumberReporter - :x: it should displays create inquiry pages FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.testobject.ObjectRepository.findTestObject()
is applicable for argument types: (org.openqa.selenium.By$ByXPath) values: [By.xpath: //td/table/tbody/tr/td]

Possible solutions: findTestObject(java.lang.String), findTestObject(java.lang.String, java.util.Map)
	at CreateInquiry001.it_should_displays_create_inquiry_page2(CreateInquiry001.groovy:307)
	at ✽.it should displays create inquiry pages(C:/Users/vdavuluri2/Katalon Studio/Govt Inquiry/Include/features/Create Inquiry-001.feature:55)

2019-06-25 16:05:26.560 WARN  c.k.k.c.c.keyword.CucumberReporter       - SKIP user capture the tracking number
2019-06-25 16:05:26.561 WARN  c.k.k.c.c.keyword.CucumberReporter       - SKIP user click on the close button at right side
2019-06-25 16:05:26.562 WARN  c.k.k.c.c.keyword.CucumberReporter       - SKIP user successfully logged off

HTML Page
I wanted to extract the text …
"Create Inquiry Tracking # 596" & “The inquiry for system tracking # 596 has been submitted successfully” from the below

&lt;!--***** begin:grid *****--&gt;
&lt;table width="100%" cellspacing="0" border="0" class="wrap"&gt;
&lt;tr&gt;
&lt;td width="100%" class="grid"&gt;
&lt;!-- InstanceBeginEditable name="PageHeader" --&gt;
&lt;!--************ uitmplbegin: tPH ************--&gt;
&lt;!--****** begin:titles ******--&gt;
&lt;table cellspacing="0" border="0" class="tPH"&gt;
&lt;!--****** begin:page title ******--&gt;
&lt;tr&gt;
&lt;td class="pageTitle"&gt;Create Inquiry Tracking # 596&lt;/td&gt;
&lt;/tr&gt;
&lt;!--****** end:page title ******--&gt;
&lt;/table&gt;
&lt;!--****** end:titles ******--&gt;
&lt;!--************ uitmplend: tPH ************--&gt;
&lt;!-- InstanceEndEditable --&gt;
&lt;/td&gt;
&lt;td width="182" class="grid"&gt;&lt;img src="[?appId=gmpinquiry&amp;flName=/uitmpl/en/img/pixel.gif](https://cstz0115.sldc.sbc.com:8201/servlet/GMPGate?appId=gmpinquiry&amp;flName=/uitmpl/en/img/pixel.gif)" width="182" height="1" alt="" border="0" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;!--***** end:grid *****--&gt;

&lt;!--***** begin:grid *****--&gt;
&lt;table width="100%" cellspacing="0" border="0" class="wrap"&gt;
&lt;tr&gt;

&lt;td width="100%" class="grid"&gt;
&lt;!--- BeginOptional name="TaskConfirmation" ---&gt;
&lt;!-- InstanceBeginEditable name="TaskConfirmation" --&gt;
&lt;!--************ uitmplbegin: mTC ************--&gt;
&lt;table cellspacing="0" cellpadding="2" border="0" class="mTC"&gt;
&lt;tr class="msgConfirm"&gt;
&lt;td&gt; &lt;img src="[?appId=gmpinquiry&amp;flName=/uitmpl/en/img/confirmation.gif](https://cstz0115.sldc.sbc.com:8201/servlet/GMPGate?appId=gmpinquiry&amp;flName=/uitmpl/en/img/confirmation.gif)" width="29" height="29"

border=“0” alt=“Confirmation.” /></td><td>The inquiry for system tracking # 596 has been submitted successfully. </td>
</tr>
</table>

You need to pass the TestObject to the findTestObject() function, as was explained here:

and here: