2

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.

2
  • If that is your actual code, you'll get errors because the indentation is wrong. Aug 23, 2017 at 11:53
  • Sorry for confusing the question. The identation was wrong in my post but not in the code. I corrected in in the post.
    – Mytzenka
    Aug 23, 2017 at 12:10

1 Answer 1

4

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)
    }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.