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 ..

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.