Question

I'm building a script to test gift card functionality on a series of ecom sites and am hitting a frustrating issue where UFT will say the smaller number is greater than the larger number. The script opens and closes the browser between iterations and pulls the site URL from a new line in the datatable. It works up to the point of evaluating if the card balance is greater than the total. Here's the code I'm having trouble with:

vTotal=Browser("GLOBAL").Page("CHECKOUT_BILLING").WebElement("order-total").WebElement("Total").getroproperty("value")
for vt=1 to len(vTotal)
d=mid(vTotal,vt,1)
if isnumeric(d) then 
b=b&d
end if 
next

vGCBalance=Browser("GLOBAL").Page("CHECKOUT_BILLING").WebElement("GC_BALANCE").getroproperty("value")
for vg=1 to len(vGCBalance)
c=mid(vGCBalance,vg,1)
if isnumeric(c) then 
a=a&c
end if 
next
wait 3
If ( a) > ( b) = True Then
    msgbox ( a)&" Is Greater Than "&( b)
else
    msgbox ( b)&" Is Greater Than "&( a)
End If

I have tried several ways to set values for "a" and "b". This includes identifying the cart total and GC balance using descriptive programming. I've also used regEXP lines to separate numbers from the string produced by the GetROProperty. I've even tried writing the values to the datatable and had the comparison read those values.

In all cases if I do a msgbox for a or b it shows the correct values. I went with the above method because it returns the numbers without the decimal point.

It seems like UFT is using previous answers the IF THEN statement for each site (maybe the answer from the first time). Eventually it will say that a > b when a=16 and b=100 Is UFT caching the answer to the if then statement? Is my code wrong (I'm learning programming as I build my test scripts so it's possible)?

Any suggestions or advice would be great! Thanks, Robert

Was it helpful?

Solution

My first guess is that you're comparing two strings, not two numbers. The "&" operator performs string concatenation, so when you use statements like "b=b&d" or "a=a&c" you are building a string of text. If a = "16" and b = "100" then a is greater than be from a alphabetic comparison of two strings since the second character in a ("6") is greather than the second character in b ("0").

Try forcing a numeric comparison instead.

If CInt(a) > CInt(b) Then
    MsgBox a & " is greater than " & b
Else
    MsgBox b & " is greater than " & a
End If

Alternatively, you could force your variables to be re-evaluated as numbers after you are done building your strings.

a = CInt(a)
b = CInt(b)

Then all future statements would treat those as numbers, not text strings.

On an unrelated note, I would encourage you to use better variable names that mean something. Values like "a", "b", "c", and "d" don't help anyone read your code and understand why one variable is different than another. Disregard if you were just doing that for the purpose of this code snippet.

It's also possible your code could be greatly simplified. If the values you are reading from 'GetROProperty' are just decimal values (without currency characters), you could convert them directly to Double value types and use those for comparison. Example

strTotal = Browser("GLOBAL").Page("CHECKOUT_BILLING").WebElement("order-total").WebElement("Total").getroproperty("value")
dblTotal = CDbl(strTotal)

strGCBalance = Browser("GLOBAL").Page("CHECKOUT_BILLING").WebElement("GC_BALANCE").getroproperty("value")
dblGCBalance = CDbl(strGCBalance)

If dblTotal > dblGCBalance Then
    MsgBox "Total '" & strTotal & "' is greater than GC Balance '" & strGCBalance & "'"
Else
    MsgBox "GC Balance '" & strGCBalance & "' is greater than or equal to Total '" & strTotal & "'"
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top