Search code examples
stringrobotframework

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.


Solution

  • ${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