Dictionaries in Robot Framework
Introduction
You can create a list using &
Let's look at the test dict.robot
*** Variables *** &{url} hh=https://www.heihei.ru tb=https://www.topbicycle.ru uu=https://www.urn.su *** Test Cases *** Test1 Log To Console ${url.hh} Log To Console ${url.tb} Log To Console ${url.uu}
robot dict.robot
============================================================================== Dict ============================================================================== Test1 https://www.heihei.ru .https://www.topbicycle.ru .https://www.urn.su Test1 | PASS | ------------------------------------------------------------------------------ Dict | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html
A key can be stored in a variable
*** Variables *** ${site} tb &{url} hh=https://www.heihei.ru tb=https://www.topbicycle.ru uu=https://www.urn.su *** Test Cases *** Test1 Log To Console ${url.hh} Log To Console ${url.tb} Log To Console ${url.uu} Test2 Log To Console ${url.${site}}
robot dict.robot
============================================================================== Dict ============================================================================== Test1 https://www.heihei.ru .https://www.topbicycle.ru .https://www.urn.su Test1 | PASS | ------------------------------------------------------------------------------ Test2 https://www.topbicycle.ru Test2 | PASS | ------------------------------------------------------------------------------ Dict | PASS | 2 tests, 2 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html
With this way of setting the key, you can specify the desired site when calling the test from the terminal
robot -v site:uu dict.robot
============================================================================== Dict ============================================================================== Test1 https://www.heihei.ru .https://www.topbicycle.ru .https://www.urn.su Test1 | PASS | ------------------------------------------------------------------------------ Test2 https://www.urn.su Test2 | PASS | ------------------------------------------------------------------------------ Dict | PASS | 2 tests, 2 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html
DotDict
The object that the robot creates as a dictionary has the type
DotDict
It is important to remember this if you pass it on to
Python
Convert To Dictionary
To convert DotDict to a regular dictionary you need to use the Convert To Dictionary keyword
&{dict} topic=qa lang=python frame=robot ${python_dict}= Convert To Dictionary ${dict}
Article author: Andrei Olegovich