How to assert integer value extracted from string?

Im a beginner into RF and just starting to get hang of things

My RF code

AssertOrder # IN ORDER DOCUMENT PAGE
${orderdocamt} Get Text ${orderdoctotal}
${orderdocamtint} Convert Currency Text To Integer ${orderdocamt}
Should Be Equal ${orderdocamtint} ${amount}
Log To Console Checked Order total Equal to Sales Document amount

This is the custom keyword im using
Convert Currency Text To Integer
[Arguments] ${currency_text}
${currency_values} Get Regexp Matches ${currency_text} $\d+(?:,\d{3})*(?:.\d{2})?
${integer_values} Create List
FOR ${value} IN @{currency_values}
${integer_part} ${decimal_part} Split String ${value} .
${integer_part} Remove String ${integer_part} ,
${integer_value} Convert To Integer ${integer_part}${decimal_part}
Append To List ${integer_values} ${integer_value}
END
[Return] ${integer_values}

When i log to console,it is outputting for ${orderdocamtint} . I tried many things but couldnt understand why integer value is not returning … It is returning blank .Where do i go wrong

You are returning a list and then using Should Be Equal which does not work with lists.

I am with mobile, so cannot test your code, but you should edit and add a sample input for us to replicate.

AssertOrder
${orderdocamtint} Convert Currency Text To Integer $1500.50
Should Be Equal ${orderdocamtint} 1500.50
Log To Console Checked Order total Equal to Sales Document amount

This is the custom keyword im using
Convert Currency Text To Integer
[Arguments] ${currency_text}
${currency_values} Get Regexp Matches ${currency_text} $\d+(?:,\d{3})*(?:.\d{2})?
${integer_values} Create List
FOR ${value} IN @{currency_values}
${integer_part} ${decimal_part} Split String ${value} .
${integer_part} Remove String ${integer_part} ,
${integer_value} Convert To Integer ${integer_part}${decimal_part}
Append To List ${integer_values} ${integer_value}
END
[Return] ${integer_values}

How do i convert the list returned to scalar variable ?

Hi @savadibrahim,

  1. I had a go at it, first issue seems to be with you regular expression, it’s not returning any matches, but I’m sure what you wanted to get out of the regex?
    A $ has a special meaning in regex, it’s the end of string character, so if you want to include it you need to escape it (\$) but from your keyword it looks like you didn’t want it?
    I put your example string and your regex as is into regex101 for you to play around and figure out the regex you need → regex101: build, test, and debug regex

  2. When you get the regex returning what you want, the second issue is I expect your Convert To Integer line will convert the dollar amount to integer cents, and append that to the list that’s returned? so when you compare 150050 to 1500.50 Should Be Equal will fail, is that what you want?

  3. on your Should Be Equal line in your test, as @HelioGuilherme66 mentioned, you are comparing a list to a value, perhaps you want to compare the fist item in the list, something like this:

Should Be Equal 	${orderdocamtint}[0] 	1500.50

Finally FYI, use three back ticks (```) on the line before and after your code for a code block so your white space is retained. I guess you had something like this:

*** Settings ***
Library 	String


*** Test Cases ***
AssertOrder
	${orderdocamtint} 	Convert Currency Text To Integer 		$1500.50
	Should Be Equal 	${orderdocamtint} 	1500.50
	# Log To Console Checked Order total Equal to Sales Document amount

*** Keywords ***
# This is the custom keyword im using
Convert Currency Text To Integer
	[Arguments] 	${currency_text}
	${currency_values} 		Get Regexp Matches 	${currency_text} 	$\d+(?:,\d{3})*(?:.\d{2})?
	${integer_values} 	Create List
	FOR 	${value} 	IN 	@{currency_values}
		${integer_part} 	${decimal_part} 	Split String 	${value} 	.
		${integer_part} 	Remove String 	${integer_part} 	,
		${integer_value} 	Convert To Integer 	${integer_part}${decimal_part}
		Append To List 	${integer_values} 	${integer_value}
	END
	[Return] 	${integer_values}

Anyway with a bit of playing I’m guessing you wanted a regex something like \$([\d\.,]*), but you need to escape the escape character in robot framework, so that becomes \\$([\\d\\.,]*)

So this is my version:

*** Settings ***
Library 	String
Library 	Collections

*** Test Cases ***
AssertOrder
	${orderdocamtint} 	Convert Currency Text To Integer 		$1500.50
	# Should Be Equal 	${orderdocamtint} 	1500.50
	Should Be Equal 	${orderdocamtint}[0] 	1500.50
	# Log To Console Checked Order total Equal to Sales Document amount
	# ${orderdocamtint} 	Convert Currency Text To Integer 		$1500.50 $13.50 something else 1234568 $987654.32 and the non decimal $13

*** Keywords ***
# This is the custom keyword im using
Convert Currency Text To Integer
	[Arguments] 	${currency_text}
	# ${currency_values} 		Get Regexp Matches 	${currency_text} 	$\d+(?:,\d{3})*(?:.\d{2})?
	${currency_values} 		Get Regexp Matches 	${currency_text} 	\\$([\\d\\.,]*) 	1
	${integer_values} 	Create List
	FOR 	${value} 	IN 	@{currency_values}
		${integer_part} 	${decimal_part} 	Split String 	${value} 	\.
		${integer_part} 	Remove String 	${integer_part} 	,
		${integer_value} 	Convert To Integer 	${integer_part}${decimal_part}
		Append To List 	${integer_values} 	${integer_value}
	END
	[Return] 	${integer_values}

As I mentioned in point 2 it fails with 150050 != 1500.50

  1. Note this will still have an issue with whole dollar values i.e. $13 (with no cents)

Hope that helps,

Dave.