3

I have a project that has been implemented using a mix of python and Robotframework scripts. I have have a bunch of configuration items stored inside my projects Config.ini file which looks like this:

[Environment]
Username: username@mail.com
Password: testpassword

[WebUI]
login_url:        http://testsite.net/

Python is able to interpret the above variables alright using the ConfigManager object like this:

class MyConfigManager(ConfigManager):
    """
    Class to hold all config values in form of variables.
    """

def __init__(self):
    super().__init__("dispatch/config.ini")
    self._android = Android(self._config)

@property
def username(self):
    return self._config.get(_env_section, "Username")

@property
def password(self):
    return self._config.get(_env_section, "Password")

config = MyConfigManager()

Is it possible to import config.ini in Robotframework as a variables file and use these values? I am trying not to have another variables file for my Robot scripts.

EDIT:

I am attempting to do something like this with my robot file:

*** Settings ***
Documentation   WebUI Login Tests
Library         SeleniumLibrary
Resource        common_keywords.robot
Variables       config.ini
# ^ will this work?  
Default Tags    Smoke
Suite Setup     Set Selenium Timeout        15seconds
Suite Teardown  Close Browser

*** Variables ***

${login_button}     class:auth0-lock-submit



*** Test Cases ***
TC001_Login_Test
    [Documentation]     Open login page, login with credentials in arguments.
    Open Browser Confirm Login Page     chrome      ${login_url}
    Provide Input Text          name:email          ${username}
    Provide Input Text          name:password       ${password}
    Find Element And Click      ${login_button}
# the three vars ${login_url},  ${username}, ${password} would be from 
# config.ini but this isnt working. What am I doing wrong? or is not
# possible to do this?  
Souparno
  • 350
  • 1
  • 5
  • 18

2 Answers2

6

Robot framework variable files can be python code, and because they are python, you can create variables any way you want.

You just need to create a python function that returns a dictionary of key/value pairs. Each key will become a robot variable.

For example, for the data in your question, if you want to create variables like ${CONFIG.Environment.username}, you could do it like this:

import ConfigParser
def get_variables(varname, filename):
    config = ConfigParser.ConfigParser()
    config.read(filename)

    variables = {}
    for section in config.sections():
        for key, value in config.items(section):
            var = "%s.%s.%s" % (varname, section, key)
            variables[var] = value
    return variables

Save it to a file named "ConfigVariables.py", and put it where your test can find it. You can then use it like this:

*** Settings ***
Variables  ConfigVariables.py  CONFIG  /tmp/Config.ini

*** Test cases ***
Example
    should be equal  ${CONFIG.Environment.username}  username@mail.com
    should be equal  ${CONFIG.Environment.password}  testpassword
    should be equal  ${CONFIG.WebUI.login_url}       http://testsite.net/

Using functions to define variables is described in the robot framework user guide, in the section titled Variable Files

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

You can still use your config.ini file as below:

[Environment]
Username = username@mail.com
Password = testpassword

[WebUI]
login_url =        http://testsite.net/

is and simpley create a python file 'MyVariables.py fetch your variables as following:

import sys
import os
import ConfigParser


try:
    dir_path = os.path.dirname(os.path.realpath(__file__))
    config_file = os.path.join(dir_path,'config.ini')

    config = ConfigParser.ConfigParser()
    config.read(config_file)

    login_url = config.get('WebUI','login_url')    
    Username = config.get('Environment','Username')
    Password= config.get('Environment','Password')


except (ConfigParser.Error, ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
    sys.stderr.write("Error: {0}.\n".format(e))
    sys.exit()

Notice that you have defined your variables on the py file same as they are named on the config file pointing to the corresponding section, for example in python file we defined variable 'login_url' using the config file section 'WebUI' and the variable 'login_url'

And on your robot file you just need to set the variables file on the settings and you can user the defined variables on you test case:

*** Settings ***
Library                 Selenium2Library
Variables               MyVariables.py


*** Test Cases ***
Testing Variables
    log variables

Your report will be something like:

15:02:41.615 INFO ${login_url} = http://testsite.net/
15:02:41.616 INFO ${Password} = testpassword
15:02:41.616 INFO ${Username} = username@mail.com

Omar Mughrabi
  • 113
  • 1
  • 3
  • I have added similar code but not working..... failed: Importing variable file '/ConfigVariables.py' , Please any help, I can show on screen share @Omar Mughrabi – simond Mar 08 '22 at 15:26
  • Sorry I have missed your comment, do you still need support here? if you still need help just let me know, and Apologies again. – Omar Mughrabi Sep 26 '22 at 14:56