Strings are equal, but different? Weird string-to-string comparison behaviour

Hi all,

This is a very simple macro that performs a string-to-string comparison, please check it out and let me know if you have any idea why it returns false:

{
    "Name": "PriceTest",
    "CreationDate": "2021-3-2",
    "Commands": [{
            "Command": "open",
            "Target": "https://ftygu.free.beeceptor.com",
            "Value": "priceRaw"
        },
        {
            "Command": "store",
            "Target": "1 399,90 €",
            "Value": "priceRaw"
        },
        {
            "Command": "storeText",
            "Target": "xpath=/html/body/span",
            "Value": "priceRaw2"
        },
        {
            "Command": "executeScript_Sandbox",
            "Target": "return ${priceRaw} === ${priceRaw2}",
            "Value": "isEqual"
        },
        {
            "Command": "echo",
            "Target": "${isEqual}",
            "Value": ""
        }
    ]
}

Doing === or == results false in both cases.Strings look equal to the human eye but hex codes are different:
function toHex(str) {
var result = ‘’;
for (var i = 0; i < str.length; i++) {
result += str.charCodeAt(i).toString(16);
}
return result;
}
toHex(“1 399,90 €”)
“31203339392c39302020ac” // String manually typed
“31a03339392c3930a020ac” // String obtained through storeText

Also, sorry for the messed format of this post, I’m not sure how to put the code inside code blocks here.

Thanks for checking!

1 Like

Hi. It looks like you want to compare numbers, not text strings. => You need to convert the string to a number. For this Javascript has parseFloat. But first you need to clean up your strings and remove (trim) the " " (space) and replace the comma “,” with a decimal point “.”.

{
  "Name": "number compare",
  "CreationDate": "2021-3-2",
  "Commands": [
    {
      "Command": "comment",
      "Target": "open // https://ftygu.free.beeceptor.com",
      "Value": "priceRaw"
    },
    {
      "Command": "store",
      "Target": "1399,90 €",
      "Value": "priceRaw"
    },
    {
      "Command": "store",
      "Target": "1 399,90 €",
      "Value": "priceRaw2"
    },
    {
      "Command": "comment",
      "Target": "storeText // xpath=/html/body/span",
      "Value": "priceRaw2"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var p = ${priceRaw}; p2= p.replace(\",\",\".\"); return p2;",
      "Value": "N_priceRaw"
    },
    {
      "Command": "echo",
      "Target": "Comma replaced: ${N_priceRaw}",
      "Value": "pink"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return parseFloat (${N_priceRaw})",
      "Value": "N_priceRaw"
    },
    {
      "Command": "comment",
      "Target": "Now clean up price2 => Remove . AND \" \"",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var p = ${priceRaw2}; p2= p.replace(\",\",\".\"); return p2;",
      "Value": "N_priceRaw2"
    },
    {
      "Command": "echo",
      "Target": "Comma replaced:  ${N_priceRaw2}",
      "Value": "orange"
    },
    {
      "Command": "comment",
      "Target": "Remove ALL spaces",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var p = ${N_priceRaw2}; p2= p.replace(/\\s/g, ''); return p2;",
      "Value": "N_priceRaw2"
    },
    {
      "Command": "echo",
      "Target": "Space removed: ${N_priceRaw2}",
      "Value": "orange"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return parseFloat (${N_priceRaw2})",
      "Value": "N_priceRaw2"
    },
    {
      "Command": "echo",
      "Target": "Now we have numbers: ${N_priceRaw} vs ${N_priceRaw2}",
      "Value": "blue"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${N_priceRaw} === ${N_priceRaw2}",
      "Value": "isEqual"
    },
    {
      "Command": "echo",
      "Target": "${isEqual}",
      "Value": "green"
    }
  ]
}

See also:

1 Like

Hi @ulrich,

Thanks for your detailed response!

Indeed, your assumptions are correct. The problem I was facing was the following:
var p = ${N_priceRaw2}; p2= p.replace(" ", “”); return p2;

The string p2 was coming out with no spaces removed, causing the parseInt/parseFloat to only parse the first digit. Your p.replace(/\s/g, ‘’) actually solved the problem, though I’m unsure why your version worked while mine didn’t. Why are the spaces not equal?

Edit: It looks like the test webpage reached its limit, if anyone wants the original HTML here it is:
https://pastebin.com/8mATNgLK

1 Like

I suggest you to use splt command and recreate value in prefered format is more easy and fast to do.

Splt part of price, with store you create the new price format and with a simple if compare it.

No need complex command to do this