if-statementautomated-testsrobotframeworkatdd

It is not possible to have ' in a saved variable in Robot Framework?


I have these variables:

*** Variables ***
${current}  ""
${doc_type}  ""
${document_type}  ""
${HoiProo}  Hoist\'s Proof Loading Certificate
${HoiMacIns}  Hoisting Machinery Inspection Certificate
${inspection_certificate}  Certificate.InspectionCertificate
${test_certificate}  Certificate.TestCertificate 

and this keyword:

*** Keywords ***
Set Doc_type
${doc_type} =  Set Variable If
    ...  '${current}' == '${HoiProo}'  ${test_certificate}
    ...  '${current}' == '${HoiMacIns}'  ${inspection_certificate}
    Set Suite Variable  ${document_type}  ${doc_type}

Whole thing

Setup current
  ${current}  ${HoiMacIns}
Setup Doctype
  Set Doc_type

But I don't understand why Robot keeps giving me this error:

Evaluating expression ''Hoisting Machinery Inspection Certificate' == 'Hoist's Proof Loading Certificate'' failed: SyntaxError: invalid syntax (<string>, line 1)

*I have also tried to remove '-signs *

Set Doc_type
${doc_type} =  Set Variable If
    ...  ${current} == ${HoiProo}  ${test_certificate}
    ...  ${current}' == ${HoiMacIns}  ${inspection_certificate}
    Set Suite Variable  ${document_type}  ${doc_type}

and to type it out

Set Doc_type
${doc_type} =  Set Variable If
    ...  ${current} == Hoist\'s Proof Loading Certificate  ${test_certificate}
    ...  ${current}' == ${HoiMacIns}  ${inspection_certificate}
    Set Suite Variable  ${document_type}  ${doc_type}

If ${current} is ${HoiProo} then ${doc_type} should be ${test_certificate}. This flow works as I have tested to compare only ${HoiMacIns}. In the future I want to add more certificates and more doc_types for the if-else, that's why I need to have this thing run like this.


Solution

  • When using expressions, you have to remember that the expression is valid syntax after robot substitutes variables. If your variable contains Hoist's and you try to use an expression like '${HoiProo}', robot is going to create an expression like 'Hoist's', which is invalid syntax.

    The easiest way around this is to use a special syntax for variables in expressions. If you omit the curly braces, robot will directly use the variable in the expression, removing the need to do any extra quoting.

    For example:

    ${doc_type} =  Set Variable If
        ...  $current == $HoiProo  ${test_certificate}
        ...  $current == $HoiMacIns  ${inspection_certificate}
    

    This is all documented in the BuiltIn library documentation in the section named Evaluating expressions.