Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains an other string (substring) in robot framework?

How to check if a string contains an other string in robot framework?

Something like

${bool} | String Contains | Hello World | World

Get Substring doesn't help, because it needs a start index.

like image 551
Juri Avatar asked Jun 12 '17 09:06

Juri


People also ask

How do you check if a substring is in another string?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you split text in robot framework?

Splits the string using separator as a delimiter string. If a separator is not given, any whitespace string is a separator. In that case also possible consecutive whitespace as well as leading and trailing whitespace is ignored. Split words are returned as a list.

How do you write if condition in robot framework?

END Use Run Keyword If in Robot Framework Run Keyword If ${True} Log This line IS executed. Run Keyword If ${False} Log This line is NOT executed. Use Run Keyword Unless in Robot Framework Run Keyword Unless ${True} Log This line is NOT executed. Run Keyword Unless ${False} Log This line IS executed.

How do you escape special characters in Robot Framework?

The backslash character can be used to escape special characters so that their literal values are used. For example, if you have a variable that contains the value ${my_variable} , you will need to escape it with the backslash character: \${my_variable} before using it as an argument for a keyword.


2 Answers

${source}=    Set Variable    this is a string

# ${contains} will be True if "is a" is a part of the ${source} value
${contains}=  Evaluate   "is a" in """${source}"""

# will fail if "is a" is not a part of the ${source} value
Should Be True      "is a" in """${source}"""

# using a robotframework keyword from the String library
# it is actually a wrapper of python's "var_a in var_b" - the previous approaches
Should Contain    ${source}    is a

# as last alternative - an approach that will store 
# the result in a boolean, with RF standard keywords
# ${contains} will be True if "is a" is a part of the ${source} value
${contains}=    Run Keyword And Return Status    Should Contain    ${source}    is a

Hope the example is self-explanatory

like image 128
Todor Minakov Avatar answered Oct 11 '22 22:10

Todor Minakov


i have found an another solution

${match} | ${value} | Run Keyword And Ignore Error | Should Contain | full string    | substring
${RETURNVALUE} | Set Variable If | '${match}' == 'PASS' | ${True} | ${False}
like image 32
Juri Avatar answered Oct 11 '22 23:10

Juri