using Regex to verify number in string

Hi all,

I have no coding experience, nor Regular expression experience, ( or testautomation in general) but am willing to learn. So I’m quite a noob.
I searched the web for what is seems at first glance a simple problem. I can continue writing tests, but I want to be the test robust and can handle changing testdata.

I have an Object with a Text property “Daisy-boeken (25)” (ignore the quotes)

I want to check if the number is greater than 25.
I did some research but didn’t find a way via a regular expression to verify a match. I found some answers with simple expressions but they do not cover all the situations.

Like: Daisy-boeken \([2-9][6-9]\)
The number Daisy-boeken (30) will not match the regex, which it should.

Can someone point me out here how to improve the Regex?

Second question: Can this be solved programmaticaly in Groovy, without using regular expressions?

Any answer will be greatly appreciated, as it will improve my insights in Katalon,Groovy and Regex.

Kind regards,

This particular problem is not one I’d use regex for, but still…

Try

2[5-9]|[3-9]\d+|[1-9]\d{2,}

I’d much prefer to extract the text and use Groovy but you don’t make clear exactly what HTML elements you’re dealing with – what (where) is your “text property”?

Provided with a HTML which contains:

Daisy-boeken (25)

make a Test Case with following lines, and run it.

WebUI.openBrowser('')
WebUI.navigateToUrl(  the URL of your AUT  )
String pContent = WebUI.getText(findTestObject('Page_Discussion 6366/p_Daisy-boeken (25)'))
// pContent=='Daisy-boeken (25)'
WebUI.verifyMatch(pContent, '^Daisy.*', true)
WebUI.verifyMatch(pContent, '^Daisy\\-.*', true)    // - needs to be escaped
WebUI.verifyMatch(pContent, '^Daisy\\-boeken.*', true)
WebUI.verifyMatch(pContent, '^Daisy\\-boeken \\(25\\)', true)   // ( needs to be escaped, ) needs to be escaped as well
WebUI.verifyMatch(pContent, '[^\\(]+\\([0-9]+\\)', true)
String patternStr = '[^\\(]+\\(([0-9]+)\\)'
WebUI.verifyMatch(pContent, patternStr, true)
import java.util.regex.Pattern
import java.util.regex.Matcher
Pattern pattern = Pattern.compile(patternStr)
Matcher matcher = pattern.matcher(pContent)
if (matcher.matches()) {
    WebUI.comment("pContent=\"${pContent}\" DOES match regex ${patternStr}")
    String numberStr = matcher.group(1)
    WebUI.comment("matcher.group(1)=${numberStr}")
    Number numberValue = Integer.valueOf(numberStr)
    //WebUI.verifyGreaterThanOrEqual(numberValue, 25)
    WebUI.verifyGreaterThanOrEqual(numberValue, 26)   // this fails intensionally
} else {
    WebUI.comment("pContent=\"${pContent}\" does NOT match regex ${patternStr}")
}
WebUI.closeBrowser()

Russ Thomas said:

This particular problem is not one I’d use regex for, but still…

Try

2[5-9]|[3-9]\d+|[1-9]\d{2,}

https://regex101.com/r/Z9B7bR/1

I’d much prefer to extract the text and use Groovy but you don’t make clear exactly what HTML elements you’re dealing with – what (where) is your “text property”?

Thanks for your anwser Russ,

I’ll attach a png the properties from the of the obecjt taken with object spy. I hope it will suffice.

text property.PNG

I see. You’re dealing with Test Objects, not HTML elements. Seriously, I wouldn’t start there – it’s a bit like a guy in London who wants to fly into Paris airport by sailing across the Atlantic Ocean to New York and boarding a flight to France at JFK. Why go that way?

And if you really want to take that route, is there something wrong with Kazurayum’s code? It looks very comprehensive…

kazurayam said:

Provided with a HTML which contains:

Daisy-boeken (25)

make a Test Case with following lines, and run it.

WebUI.openBrowser('')

WebUI.navigateToUrl( the URL of your AUT )
String pContent = WebUI.getText(findTestObject(‘Page_Discussion 6366/p_Daisy-boeken (25)’))
// pContent==‘Daisy-boeken (25)’
WebUI.verifyMatch(pContent, ‘^Daisy.’, true)
WebUI.verifyMatch(pContent, '^Daisy\-.
’, true) // - needs to be escaped
WebUI.verifyMatch(pContent, ‘^Daisy\-boeken.*’, true)
WebUI.verifyMatch(pContent, ‘^Daisy\-boeken \(25\)’, true) // ( needs to be escaped, ) needs to be escaped as well
WebUI.verifyMatch(pContent, ‘[^\(]+\([0-9]+\)’, true)
String patternStr = ‘[^\(]+\(([0-9]+)\)’
WebUI.verifyMatch(pContent, patternStr, true)
import java.util.regex.Pattern
import java.util.regex.Matcher
Pattern pattern = Pattern.compile(patternStr)
Matcher matcher = pattern.matcher(pContent)
if (matcher.matches()) {
WebUI.comment(“pContent=”${pContent}" DOES match regex ${patternStr}")
String numberStr = matcher.group(1)
WebUI.comment(“matcher.group(1)=${numberStr}”)
Number numberValue = Integer.valueOf(numberStr)
//WebUI.verifyGreaterThanOrEqual(numberValue, 25)
WebUI.verifyGreaterThanOrEqual(numberValue, 26) // this fails intensionally
} else {
WebUI.comment(“pContent=”${pContent}" does NOT match regex ${patternStr}")
}
WebUI.closeBrowser()


Thank you for Kazurayam. It works fine. I'll still have to figure out what each line does. I'll have to face the learning curve...

Thank you for your comment Russ.

Kazurayum’s code runs perfectly. Could you point me out to some documentation how to automate “the right way”?

Kind regards,

How to Match/Verify One String which Contains another String(i.e, regex) ignoring Case of a string using VerifyMatch Function?

Surendra,

Could you please specify target HTML element which you want to match? Without a practical target, it is difficult to talk productively about it.

kazurayam
i want to search using “pro” in search bar, which gives me auto suggestion list irrespective of case (say “Idea Product”, “Products”),how do i verify if my search text is present in Suggestion list??

Right now i’m converting suggestion list results into smaller case & matchnig those stuff using selenium code…

Is there any better way of doing it in Katalon???

To ignore Case prefix (?i) to your regexp in the expectedText parameter.
Just an example:

orgStr = 'Hello World'
findStr = 'HELLO'
ignoreCase = '(?i)'
regexp = ignoreCase + '.*?'+ findStr + '.*?'
WebUI.verifyMatch(orgStr, regexp, true, FailureHandling.STOP_ON_FAILURE)

I too am very new to everything. I have a test that calls a method to verify a file has been downloaded. Here is the method.

@Keyword
	public boolean isFileDownloaded(String downloadPath, String fileName) {
		//When using this, ensure your test case includes the following two lines
		//String home = System.getProperty('user.home')
		//String userDownloads = new File(home + '/Downloads/')
		//use the variable userDownloads above as the downloadPath in this function call
		File dir = new File(downloadPath);
		File[] dirContents = dir.listFiles();
		if (dirContents.length > 1) {
			for (int i = 0; i < dirContents.length; i++) {
				if (dirContents[i].getName().equals(fileName)) {
					// File has been found, it can now be deleted
					dirContents[i].delete();
					KeywordUtil.markPassed("File has been successfully verified and deleted");
					return true;
				}
			}
			KeywordUtil.markFailedAndStop("File not found");
			return false;
		}
		KeywordUtil.markFailedAndStop("No files to verify");
		return false;

In my test, when I call the method I state the value of the variable fileName. However, that file name has a number in it that increases every time the file is downloaded ie: File_12345_Type.csv. I dont need to check the exact number to verify the file was downloaded, so I was hoping to use a regex to state the value. However, I would assume that you need to tell Katalon somehow that you are using a regex like in some commands. Any help with this would be appreciated =)

If anyone needs the answer, I edited the method this way
@Keyword
public boolean isFileDownloadedRegEx(String downloadPath, String RegEx) {
//When using this, ensure your test case includes the following two lines
//String home = System.getProperty(‘user.home’)
//String userDownloads = new File(home + ‘/Downloads/’)
//use the variable userDownloads above as the downloadPath in this function call
Pattern regexPat = Pattern.compile(RegEx)
File dir = new File(downloadPath);
File[] dirContents = dir.listFiles();
if (dirContents.length > 1) {
for (int i = 0; i < dirContents.length; i++) {
Matcher mat = regexPat.matcher(dirContents[i].getName())
if (mat.find()) {
// File has been found, it can now be deleted
dirContents[i].delete();
KeywordUtil.markPassed(“File has been successfully verified and deleted”);
return true;
}
}
KeywordUtil.markFailedAndStop(“File not found”);
return false;
}
KeywordUtil.markFailedAndStop(“No files to verify”);
return false;