4

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 ..
Aksen P
  • 4,564
  • 3
  • 14
  • 27
Jin Su Yu
  • 43
  • 1
  • 3

2 Answers2

5

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

CrAzyD0g
  • 51
  • 1
  • 3
0

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.

Laurent Bristiel
  • 6,819
  • 34
  • 52