JSON
Введение | |
Пример | |
Похожие статьи |
Введение
Пример
Нужно прочитать из робота файл users.json и разобрать его содержимое
Структура проекта
json/ ├── data │ └── users.json └── src └── json.robot 2 directories, 2 files
{ "users": [{ "username": "tester0", "password": "secret0", "firstname": "Dmitry", "lastname": "Mendeleev", "phone": 12345 }, { "username": "tester1", "password": "secret0", "firstname": "Nikolai", "lastname": "Basov", "phone": 12346 } ] }
Robot Framework
*** Settings *** Library OperatingSystem Library Collections *** Test Cases *** Loading JSON ${json}= Get File ../users.json ${object}= Evaluate json.loads('''${json}''') json Log To Console ${object["users"][0]["username"]} &{user0}= Convert To Dictionary ${object["users"][0]} Log To Console ${user0}
robot json_from_file.robot
============================================================================== Json ============================================================================== Loading JSON ..tester0 ..{'username': 'tester0', 'password': 'secret0', 'firstname': 'Dmitry', 'lastname': 'Mendeleev', 'phone': 12345} Loading JSON | PASS | ------------------------------------------------------------------------------ Json | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/sandbox/robot/json/src/output.xml Log: /home/andrei/sandbox/robot/json/src/log.html Report: /home/andrei/sandbox/robot/json/src/report.html
Указать путь от текущей директории можно с помощью Normalize Path и CURDIR
${path}= Normalize path ${CURDIR}/../../../../data/users.json ${json}= Get File ${path}