Jin Su Yu
Jin Su Yu

Reputation: 43

Robotframework, How to define a dynamic variable name

I want to Define variable having dynamic name.

For example

${${FILE}{VAR}}    Create List      // ${FILE} = TEST, ${VAR} = VAR

Then I want to get variable named '${TESTVAR}'.

Here is my Summarized code...

*** Settings ***

Library    SeleniumLibrary

Library    ExcelLibrary

Library    Collections

*** Variables ***

${VAR}=     VAR

*** Keywords ***

Open Document And Assign Variable

    [Arguments]     ${FILE}

    Open Excel Document filename=${FILE}    doc_id=doc_var


    ${${FILE}${VAR}}    Create List    # It doesn't work ..

Upvotes: 4

Views: 9686

Answers (2)

CrAzyD0g
CrAzyD0g

Reputation: 51

The previous answer is not really accurate. This can be achieved exactly with robot's "Variables inside variables" feature like so:

FOR  ${idx}  IN RANGE  3
   ${var_name} =  Catenate  SEPARATOR=_  var  ${idx}
   Set Suite Variable  ${${var_name}}  ${idx}
END

you get:
   var_1=1
   var_2=2
   var_3=3

Please note that variables resolution is happening only for keywords arguments (at least for robot version 3.1.2 that I am using), therefore either one of 'Set test variable', 'Set Suite Variable' or 'Set Global Variable' keywords must be used. The following won't work:

FOR  ${idx}  IN RANGE  3
   ${var_name} =  Catenate  SEPARATOR=_  var  ${idx}
   ${${var_name}} =  ${idx}
END

results in:
    No keyword with name '${${var_name}} =' found.

For your list case you just need to create a list variable and reassign it to dynamically named variable using the above mentioned keywords

Upvotes: 5

Laurent Bristiel
Laurent Bristiel

Reputation: 6935

This is currently not possible with Robot Framework. You can use "variables inside variables" to resolve the values of variables (see the documentation on this topic) but not to resolve/set the name of the variable itself.

Though I am afraid that would be confusing anyway. Maybe you can explain your motivations and people can come up with another solution for your problem.

Upvotes: 0

Related Questions