Mytzenka
Mytzenka

Reputation: 235

Variables from variables file

I am trying to use variables from a variables phyton file. I think I am doing everything as described in the user manual, yet, the variables remain unavailable to me.

This is the variables file

TEST_VAR = 'Michiel'

def get_variables(environment = 'UAT'):

    if environment.upper() == 'INT':
        ENV = {"NAME": "Bol.com INT",
               "BROWSER": "safari",
               "URL": "www.bol.com"}
    else:
        ENV = {"NAME": "Bol.com UAT",
               "BROWSER": "firefox",
               "URL": "www.bol.com"}

    return ENV

I import this like this:

*** Settings ***
Variables  Variables.py  ${ENVIRONMENT}
Library  Selenium2Library
Resource  ../PO/MainPage.robot
Resource  ../PO/LoginPage.robot

*** Variables *** 
${ENVIRONMENT}

*** Keywords ***
Initialize suite
    log  ${TEST_VAR}
    log  start testing on ${ENV.NAME}

Start application
    open browser  ${ENV.URL}  ${ENV.BROWSER}

Stop application
    close browser

Both files are at the same level in the folder structure. Yet the variables from the file are not available to me. Not even the normal variable.

Can someone tell me what I am doing wrong here? Must be something small that I'm forgetting. Many thanks.

Upvotes: 2

Views: 14305

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

When you use a variable file like this, you don't end up with a variable named ${ENV}. Instead, every key in the dictionary that you return becomes a variable.

From the robot framework user guide (emphasis added):

An alternative approach for getting variables is having a special get_variables function (also camelCase syntax getVariables is possible) in a variable file. If such a function exists, Robot Framework calls it and expects to receive variables as a Python dictionary or a Java Map with variable names as keys and variable values as values.

In this specific case that means that you will end up with the variables ${NAME}, ${BROWSER} and ${URL}.

If you want to set a variable named ${ENV}, you will have to make that part of the returned dictionary

# Variables.py
from robot.utils import DotDict

def get_variables(environment="UAT"):
    ...
    return {
        "ENV": DotDict(ENV)
    }

Upvotes: 4

Related Questions