Using an If to check that a string contains a certain substring

I’m new to Kantu and one of the things I’m trying to do with my first macro is click a certain area of the screen based on some text that is already there. I’ve stored the string in a variable (Description) and I now want to check it for a substring using an if statement. I’ve been trying several different ways to use a contains function, but I’m apparently failing to hit the right syntax.

So, what should my condition (Target:) be for the if statement if I want it to evaluate true when the variable includes the number 110?

Here is a code snippet to check that a string contains a certain substring. I copied the code from the DemoPDFTest_with_OCR macro that ships with Kantu.

{
      "Command": "storeEval",
      "Target": "var string = \"${q}\", substring = \"110\";  b= string.includes(substring); b;",
      "Value": "textfound"
    },
2 Likes

Thank you! I went through several of the demos, but I don’t think I went through that one. I appreciate the help!

Update: Since UI.Vision Version 4 and newer, the easier solution is to use executeScript command and the Javascript .includes feature to check that a string contains a certain substring (“string comparison”):

{
  "Name": "includes",
  "CreationDate": "2019-6-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "test tim meyer",
      "Value": "e"
    },
    {
      "Command": "if_v2",
      "Target": "${e}.includes (\"tim\") == true",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "yes, it contains this string",
      "Value": "blue"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

this is not working for me. When I set the if_v2 target to:

${!SCRAPE}.includes (“test”) == true

to search the variable SCRAPE for the word “test” I get this:

error]

Line 31: Error in condition of if_v2: Invalid or unexpected token.

What am I doing wrong?

Your personal variables must not include ! - the ! symbol is reserved for the internal variables.

Hello, Happy New Year!
This command works like charm, but when we have a substring with some accent, like “Orange-PP”, the Macro can’t handle due “-” character and simply ignore. How can we fix this? Thanks!

See also Selenium IDE string operations with executeScript_Sandbox - it has examples for removing characters from string.

[error] Line 2: undefined is not a function when run your code

Since Ui.Vision Version 8+ you need to use .LastIndexOf of instead of .includes inside executescript_sandbox.

This modified macro works fine:

{
  "Name": "test3",
  "CreationDate": "2023-5-11",
  "Commands": [
    {
      "Command": "store",
      "Target": "test tim meyer",
      "Value": "e",
      "Description": ""
    },
    {
      "Command": "if",
      "Target": "${e}.lastIndexOf(\"tim\") > -1",
      "Value": "",
      "Description": "the return value of .lastIndexOf() is -1 if the substring is not found in the string at all."
    },
    {
      "Command": "echo",
      "Target": "yes, it contains this string",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}
1 Like