Escape character issue in "run keyword if" in Robot Framework



  • This script is not working as the string contains single quotes (') and double quotes (") and comparing the string is not working in this case.

    ${Key1}=      set variable     It\'s in \"Detailing\": The Perfect Gift
    ${KEY_FLAG}   run keyword if   "${Key1}"=="It\'s in \"Detailing\": The Perfect Gift"        set variable        X1
    log     ${Key_FLAG}
    

    Any solution ?



  • The condition passed to Run keyword if needs to be a valid python expression after substituting.

    The easiest solution is to do one or both of the following:

    • use triple quotes to quote the string
    • use robot's ability to directly use the variable as $Key1 rather than ${Key1}

    Also, it's not necessary to escape the quotes when calling Set Variable. They are harmless but make the code harder to read.

    For example, the following example uses both techniques and should pass:

    *** Variables ***
    ${KEY_FLAG}  ' and "
    
    *** Test Cases ***
    Example
        ${Key1}=  set variable  It's in "Detailing": The Perfect Gift ${KEY_FLAG}
    
        run keyword if  $Key1=='''It's in "Detailing": The Perfect Gift ' and "'''
        ...  pass execution  the condition is true
        fail  the condition is false
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2