Python aqConvert.StrToInt not working: argument is not a number
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Python aqConvert.StrToInt not working: argument is not a number
Hi,
I´m very new to TestComplete and trying a simple Keyword-test with the Windows calculator clicking 5 * 5 = 25. I run the test and converted the test to a Python script. All I´m trying to do is to convert the result from the output (in this case 25) from a string to an integer using aqConvert.StrToInt. I already checked the variable type with aqObject.GetVarType. The variable is definetly a string. Trying aqConvert.VarToInt. also raises an error. Here´s the code snippet I use and i also added a screenshot of the error. Can anyone please help me, what´s going wrong here? I can´t see any mistakes in my script.
def Test1():
# calculate 5 * 5 with Windows-Calculator (result = 25)
#Clicks the 'F_nf' object. (the Five-Button.Click)
Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Zehnertastatur.F_nf.Click()
#Clicks the 'Multiplizieren_mit' object.(the Multiply.Click)
Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Standardoperatoren.Multiplizieren_mit.Click()
#Clicks the 'F_nf' object. (the Five-Button.Click)
Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Zehnertastatur.F_nf.Click()
#Clicks the 'Gleich' object. (the Equals-Button.Click)
Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Standardoperatoren.Gleich.Click() # (Equal-Button.Click)
#Clicks the 'normalOutput' object.
Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Die_Anzeige_lautet_0_.textContainer.normalOutput.Click(38, 37) # (the result output)
# check if 'normalOutput' exists
if Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Die_Anzeige_lautet_0_.textContainer.normalOutput.Exists:
Log.Message("everything is fine") # it works 🙂
else:
Log.Error("nothing is fine")
res = Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Die_Anzeige_lautet_0_.textContainer.normalOutput
value = res.Text
# check the type of variable 'value'
TypeID = aqObject.GetVarType(value)
if TypeID == varOleStr:
str = "String" # variable 'value' is a String
Log.Message("TypeID: " + aqConvert.VarToStr(TypeID) + ", Type: " + str + ", Value: " + aqConvert.VarToStr(value))
# Converting the String to an Integer using aqConvert.StrToInt()
test = aqConvert.StrToInt(value) ########### -> Here´s the Problem: Python runtime error: The argument is not a number.
# check the result
if test == 25:
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25")
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
What is the value of the 'value' variable?
The idea is that, as per documentation, "the StrToInt method converts the string S, which represents an integer number in decimal notation", so if 'value', for example, contains something like '25.0' whereas the decimal separator in your locale is comma, this may be considered as integer that is not in decimal notation and cause the error.
/Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Alex,
thanks for you reply. The value is a string of '25' the result is not '25,0' or something like that, the 'value' contains '25' as a string. In my understanding it can´t be the decimal seperator comma in my locale (see screenshots down below). Furthermore, I checked if there are any space characters in the string 'value', for example value = ' 25 ', but I couldn´t find any. Just to make sure there are no space characters in it, I replaced space characters using
value = aqString.Replace(value, ' ', '')
but the problem still remains unsolved:
res = Aliases.Microsoft_WindowsCalculator.Rechner.LandmarkTarget.Die_Anzeige_lautet_0_.textContainer.normalOutput
value = res.Text
# check the type of variable 'value'
TypeID = aqObject.GetVarType(value)
if TypeID == varOleStr:
str = "String" # -> this is the type of variable 'value' it return in the Log.Message()
Log.Message("TypeID: " + aqConvert.IntToStr(TypeID) + ", Type: " + str + ", Value: " + value)
if aqConvert.StrToInt(value) == 25: # raises an error!
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25")
_________________________________________________________________________________________
I also tried the following possibilities using the string in the if-clause. It doesn´t raise an error but the result is not as expected:
if value == "25":
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25") # not the expected result
_________________________________________________________________________________________
In the following case I understand that the result is not '25,0' the 'value' contains '25'. In my understanding it can´t be the decimal seperator comma in my locale.
if value == "25,0":
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25") # expected Result
_________________________________________________________________________________________
Just to make sure, I tried all possibilites...
if value == "25.0":
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25") # expected Result
_________________________________________________________________________________________
if aqConvert.StrToFloat(value) == 25.0: # raises an error, expected Result
Log.Message("Result is 25")
else:
Log.Message("Result is NOT 25")
I´m really stuck in the mud. I hope you can help me. Thanks in advance.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
The only other idea that I have at the moment is to check html markup on the page for the '25' value.
Again, it might be prefixed or suffixed with tab or some other whitespace character that causes StrToInt() to fail.
If the markup contains pure '25' value then I have no better idea than create Support ticket via the https://support.smartbear.com/testcomplete/message/ form.
/Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Have you tried +value == +25 ? (unary operator)
Un sourire et ça repart
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I tried the same code which you shared it works fine in my end
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi anumpamchanti,
thanks for your reply. That´s pretty strange the script is working on your machine. Meanwhile I found the reason for this problem, but didn´t find a solution, yet. After I counted the number of characters of the string 'value' with len(value) I received a number of 4 characters. So I read out every single character with a for-loop:
for i in range(len(value)):
Log.Message("value[i]" + aqConvert.VarToStr(value[i]))
This is the result I get:
So the first and the last character seem to be empty, but they are not. I found out, they are unicode characters, not showing up in the log-message. So the string 'value' is of type unicode containig the "25" between those characters:
value = "\u202d25\u202c"
These characters are special unicode formatting characters, for example '\u202d' is a left-to-right-override character. In the if-clause I changed the "25" to "\u202d25\u202c" and it worked, but that´s not really a proper solution:
if value == "\u202d25\u202c":
Log.Message("Result is 25")
Have you got any idea how to get rid of these characters. Is there a way to convert the unicode string into something manageable?
Regards,
Christian
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
> they are unicode characters, not showing up in the log-message.
This is what I meant when thought "to check html markup on the page for the '25' value".
The quick idea is to use regular expression. For example (untested regexp):
if (aqObject.CompareProperty(value, BuiltIn.cmpMatches, '^[^0-9]*25[^0-9]*$', false, BuiltIn.lmNone))
Log.Checkpoint('Value equals to 25');
...
https://support.smartbear.com/testcomplete/docs/reference/misc/regular-expressions.html
/Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks everyone for a great discussion!
@Christian1 did you manage to solve the issue? Please share your solution!
Sonya Mihaljova
Community and Education Specialist
