Search code examples
robotframework

Getting SyntaxError: invalid syntax (<string>, line 1) on Set Global Variable statement


I use a Citrix VM, we have Python 3.7 and Robot 3.1.2. I have a .robot file for one of my custom keywords to fetch values of elements on a page to individual variables and then set the variable as a global variable so that I can access this variable in other custom keywords. This .robot file contains variables for many elements on a page. When I use this keyword in a test case and execute it, in some runs, the set global variable statements fail with the error - "SyntaxError: invalid syntax (<string, line 1"), however, there are many such statements for other similar variables (with the same type of values), works without any error. If I run again, it would show the same error on some different variable. Hence this error is not on the same variables always. Here is my sample code.

***Keywords***

Get Field values:

    ${count}=     get element count    xpath://<locator1>
    ${variable1}=   run keyword if    ${count}>0    get text    xpath://<locator1> 
    convert to string    ${variable1}
    set global variable    ${variable1}

    ${count}=     get element count    xpath://<locator2>
    ${variable2}=   run keyword if    ${count}>0    get text    xpath://<locator2> 
    convert to string    ${variable2}
    set global variable    ${variable2}

    ${count}=     get element count    xpath://<locator3>
    ${variable3}=   run keyword if    ${count}>0    get text    xpath://<locator3> 
    convert to string    ${variable3}
    set global variable    ${variable3}

Now if use this keyword in one test case and run it, at times it passes without any error and I can see that variable1, 2, and 3 have values as expected and I can access these variables in other keywords also as those are set as global variables. Next time if I run, the keyword - set global variable ${variable3} fails with the above error and the same statement works for variable 1 and variable 2. In the next run, the same statement might fail for variable 1 and/or variable 2. Please note that all these variable values get values as $amounts only. e.g. $33,600 Now sure why it shows a syntax error for "set global variable $<>" at times and the error is appearing on the random field in each run.

Failure Screenshot Failure Screenshot

failed keyword when the variable value is a $ rangefailed keyword when the variable value is a $ range


Solution

  • I am running RF 6.0.2, so my errors may differ. However, I was able to reproduce your problem, and it is related to how RF handles variables: Using variables with keywords creating or accessing variables

    Here is how you escape variables with dollar signs:

    Test Global
        ${report_avg6Months}=            Set Variable    $200 - $99,800
        ${report_avg6Months}=            Convert To String    ${report_avg6Months}
        Run Keyword And Expect Error     Variable '\${200 - $99,800}' not found.
        ...        Set Global Variable   ${report_avg6Months}
        Set Global Variable              \${report_avg6Months}