Forum Discussion

olosunsira12's avatar
olosunsira12
Occasional Contributor
4 years ago
Solved

Comparing 2 numbers contained in different web objects

Hello!!

 

I'm struggling trying to compare 2 numbers , each of them contained in different web objects:

 

first web object

 

second web object

 

I want to check if the first number (1270) is greather than the second one (514),after applying filters.

How can I do that by keyword test and also with scripting test?

 

For example if I use the property contentText to compare this 2 objects, TestComplete take the whole "Madrid: 514 encontrados" and I only want to compare the numbers inside the text (1270 v/s 514)

 

See pictures attached to get context, object spy, and Name Mapping

  • You will need to use aqString methods to pick out the numbers you need.  They are actually still strings at that point, so then you'll need to use aqConvert to make them numbers and then you can compare them. There are keyword test and scripting examples in both of these.

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/methods.html

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqconvert/index.html

  • Convert both values in numbers with regEx.

    See here (look for Getting numerical values from string)

     

     

    function ExtractNumber(Str, DefaultValue)
    {
      var MatchArr, re;
      re = /[-+]?\d*\.?\d+([eE][-+]?\d+)?/gm; //Specify the regular expression
      MatchArr=Str.match(re); //Search for occurrences
      //If no numbers were found then return default value
      if (MatchArr==null) return DefaultValue
        //Else, convert a string with first occurrence into a number
        else return aqConvert.StrToFloat(MatchArr[0]);
    }

     

     

     So you'll have something like that :

     

    let value1 = ExtractNumber(WebObject1.Property, -1);
    let value2 = ExtractNumber(WebObject2.Property, -1);
    let value1IsGreaterThanValue2 = value1 > value2;
    
    

     

     

2 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    You will need to use aqString methods to pick out the numbers you need.  They are actually still strings at that point, so then you'll need to use aqConvert to make them numbers and then you can compare them. There are keyword test and scripting examples in both of these.

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/methods.html

     

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqconvert/index.html

  • BenoitB's avatar
    BenoitB
    Community Hero

    Convert both values in numbers with regEx.

    See here (look for Getting numerical values from string)

     

     

    function ExtractNumber(Str, DefaultValue)
    {
      var MatchArr, re;
      re = /[-+]?\d*\.?\d+([eE][-+]?\d+)?/gm; //Specify the regular expression
      MatchArr=Str.match(re); //Search for occurrences
      //If no numbers were found then return default value
      if (MatchArr==null) return DefaultValue
        //Else, convert a string with first occurrence into a number
        else return aqConvert.StrToFloat(MatchArr[0]);
    }

     

     

     So you'll have something like that :

     

    let value1 = ExtractNumber(WebObject1.Property, -1);
    let value2 = ExtractNumber(WebObject2.Property, -1);
    let value1IsGreaterThanValue2 = value1 > value2;