Forum Discussion

abrar22's avatar
abrar22
Frequent Contributor
9 years ago

Compare 2 values

Hi All,

 

function CompareValues(Value1,Value2)

{
if (Value1==Value2)
{
Reporter.vCheckpoint("Value 1:" + Value1 + " is equal to Value 2:"+ Value2,"Value 1:" + Value1 + " is equal to Value 2:"+ Value2, 100);
return true;
}
else
Reporter.vError("Value 1:" + Value1 + " is not equal to Value 2:"+ Value2,"Value 1:" + Value1 + " is not equal to Value 2:"+ Value2, 100);
return false;
}

 

I am calling this method: CompareValues(Expiry1,Expiry2);

 

But the comparison is failing even the Log is showing  : Value 1:28-Apr-2016 (3M) is not equal to Value 2:28-Apr-2016 (3M)

 

Not sure what is the difference between them. ALso is there any inbuild Test complete function to compare 2 values?

 

Thanks

 

 

3 Replies

  • Mpho_Mogapi's avatar
    Mpho_Mogapi
    Occasional Contributor

    Hi Abrar,

     

    I have felt your pain at some point in my life. The point was that I did not know that string compares differently from ints.The quotations in a string mean a lot and simple tab space does not equal to space. My apologies for posting a reply in vbscript but this is what I used and helped me get my sleep back.

     

    Function CleanString(strSource)
        'On Error GoTo CleanStringErr
        ' convert tabs to spaces first
        strSource = Replace(strSource, vbTab, " ")
    
        ' convert all CRLFs to spaces
        strSource = Replace(strSource, vbCrLf, " ")
    
        ' Find and replace any occurences of multiple spaces
        Do While (InStr(strSource, "  "))
            ' if true, the string still contains double spaces,
            ' replace with single space
            strSource = Replace(strSource, "  ", " ")
        Loop
    
        ' Remove any leading or training spaces and return
        ' result
        CleanString = Trim(strSource)
        Exit Function
    
    'CleanStringErr:
        ' Insert error-handling code here
    End Function

    Cheers,

     

    Mpho

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    Since your two values are strings, I would trim them for the comparison to see if that gives the answer you expect

     

     

    if (aqString.Trim(Value1)==aqString.Trim(Value2))

     

     

  • djadhav's avatar
    djadhav
    Regular Contributor

    Try doing one of these:

     

    Change function declaration to:

     

    function CompareValues(Value1 : String,Value2 : String)

     

    OR

     

    change comparison expression to

     

    Value1.valueOf() == Value2