简体   繁体   中英

Pass dynamic variable value from robot framework to python file

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM