user3548662
user3548662

Reputation: 29

How to import variable file dynamically in robot framework

I am working in robot framework. I have two variables files in it. I want to pass these variables files dynamically. In certain condition I want to send a.py and in others b.py. But this information I want to pass dynamically. Can you please help me with this

Upvotes: 2

Views: 9030

Answers (3)

Hemjal
Hemjal

Reputation: 310

The most Simple and straightforward way is that you will declare all of your variables in a Python file. For example:

#usbconf.py
target_port="COM29"
target_baudrate=115200

Then import this usbconf.py file in your target robot file as follows:

#test_uart.robot
*** settings ***
Library    SerialLibrary
Library    SeleniumLibrary
Variables    usbconf.py

*** Variables ***

${response}    0A

*** test cases ***
Test UART Device
    [Documentation]    Test Serial Connection
    [Setup]    Add Port    ${target_port}    timeout=30  encoding=ascii  baudrate=${target_baudrate}  bytesize=8
    Open Port   ${target_port}
    Log To Console    ${target_port}
    Port Should Be Open

Additional note: You should have both files in the same folder.

Upvotes: 0

IRSHAD
IRSHAD

Reputation: 2943

You can import files based on the variable you are passing during execution

*** Settings ***
Variables    config_${TEST_ENVIRONMENT}.yaml

*** Test Cases ***

*** Keywords ***

Robot command to execute:

$ robot --variable TEST_ENVIRONMENT:local file_name.robot

Upvotes: 0

izikovic
izikovic

Reputation: 133

You can use the Import library keyword to manually import an external file.

Then use the Run Keyword If keyword to check which library to import. For example:

Run Keyword If   '${VAR}' == 'a'   Import Library   a.py
Run Keyword If   '${VAR}' == 'b'   Import Library   b.py

You can pass the VAR as a parameter to your test:

pybot --variable VAR:a TestSuite

Upvotes: 6

Related Questions