Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ..
like image 617
Jin Su Yu Avatar asked Oct 15 '25 20:10

Jin Su Yu


1 Answers

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

like image 93
CrAzyD0g Avatar answered Oct 19 '25 10:10

CrAzyD0g