1

I'm trying to pass a dynamic value from robot framework to a variable in python file (which have all my variables).

In .py file I have the following:

index = BuiltIn().get_variable_value("${iIndex}")
MyDynamicVar="xpath://div["+str(index)+"]"  #or without str

and in .robot file I have:

    FOR  ${i}  IN RANGE  ${c}
            ${iIndex} =    Set Variable    ${i+1}
            log to console   ${MyDynamicVar}
            log to console    ***************************************
    END

I tried to set an initial value in variables sections in robot file, and the value changed, but I need the value to be dynamic!

0

1 Answer 1

2

str.format will do the trick for you, which would give the following in the variable file:

MyDynamicVar="xpath://div[{}]"

Then you can use the Extended variable syntax to call the format function on the string like:

*** Settings ***
Variables    variables.py

*** Test Cases ***
Test
    FOR  ${i}  IN RANGE  5
            ${iIndex} =    Set Variable    ${i+1}
            log to console   ${MyDynamicVar.format(${iIndex})}
            log to console    ***************************************
    END

You can even simplify to ${MyDynamicVar.format(${i+1})}. Here are the results:

==============================================================================
Test
==============================================================================
Test                                                                  xpath://div[1]
***************************************
xpath://div[2]
***************************************
xpath://div[3]
***************************************
xpath://div[4]
***************************************
xpath://div[5]
***************************************
Test                                                                  | PASS |
------------------------------------------------------------------------------
Sign up to request clarification or add additional context in comments.

1 Comment

@A.Han I forgot to mention that a variable file is executed once when imported so any latter change will not take effect. That is why your approach did not update the variable. :)

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.