Search code examples
robotframework

Robotframework : file_1.robot needs to update a variable in file_2.robot


I tried using the Set Global Variable keyword but I am not sure if I am doing this correct. I was hoping to make 2 files have access to this variable. Here is my variables section. I am using VS code with Robot plugin for syntax highlighting. The error show when I run this is:

Invalid variable name 'Set Global Variable'.robotcode.diagnostics(ModelError)

*** Variables ***

Set Global Variable    ${VERBOSE}    0

${SERIAL_PORT}    None

Is there a special library I need to import to use Set Global Variable?

My use case is that I have 2 robot files both of them need to know if Verbose mode is enabled. I pass verbose to file_1.robot file via command line, I was hoping I could also pass the verbose variable to a Resource file_2.robot, but I am unable to do this because in my second file there is no "command line argument to pass in to it"

Is there a way from file_1.robot I can set/update a variable in file_2.robot ?

For file one i can do this via command line, but for file 2 I was hoping something like this would exist:

Resource     ../resources/Serial.robot -v Verbose:0

(in this case Serial.robot is the infamous file 2 )

To make things even simpler I dont need Verbose in File 1 , i Just need it to pass it on to the resource file somehow


Solution

  • Set Global Variable is a keyword that could be used inside test cases or custom keywords. What you need is to define variable (link to documentation).

    I pass verbose to file_1.robot file via command line, I was hoping I could also pass the verbose variable to a Resource file_2.robot, but I am unable to do this because in my second file there is no "command line argument to pass in to it"

    Nope, you are passing global variable visible from everywhere. Take a look at documentation about scopes and priorities for variables.

    If you want to define once and use in multiple places you could create file with common variables and import in both files. Example:

    Here you define:

    # BaseVariables.robot
    *** Variables ***
    ${VERBOSE}    0
    

    And use:

    # file_1.robot
    *** Settings ***
    Resource    BaseVariables.robot
    

    and in second file

    # file_2.robot
    *** Settings ***
    Resource    BaseVariables.robot