Change the value of a variable in Robot Framework
Introduction
In Robot Framework you cannot simply declare a variable with = directly in the test code, as is done in Python
Within a single keyword
A variable declared earlier in *** Variables *** can be changed with
Set Variable
Scope - within the keyword (only in the same Keyword)
*** Settings *** Documentation Working With Variables *** Variables *** ${x}= '7' *** Test Cases *** Verify That Variables Can Be Changed [Documentation] This test case verifies variable reassignment [Tags] Functional Start TestCase Verify Search Results Finish TestCase *** Keywords *** Start TestCase Log To Console Start Verify Search Results Log To Console Verify Log To Console ${x} ${x}= Set Variable '8' Log To Console ${x} Should Be Equal ${x} '8' Finish TestCase Log To Console Done
robot -d results var.robot
============================================================================== Var :: Working With Variables ============================================================================== Verify That Variables Can Be Changed :: This test case verifies va... Start .Verify '7' '8' .Done Verify That Variables Can Be Changed :: This test case verifies va... | PASS | ------------------------------------------------------------------------------ Var :: Working With Variables | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/tests/var/results/output.xml Log: /home/andrei/robot/tests/var/results/log.html Report: /home/andrei/robot/tests/var/results/report.html
Set Global Variable
To make the changed variable visible in other keywords, you can use Set Global Variable
*** Settings *** Documentation Working With Variables *** Variables *** ${x}= '7' *** Test Cases *** Verify That Variables Can Be Changed [Documentation] This test case verifies variable reassignment [Tags] Functional Start TestCase Change RPM Version Verify RPM Version Finish TestCase *** Keywords *** Start TestCase Log To Console Start Change RPM Version Log To Console ${x} Set Global Variable ${x} '8' Log To Console ${x} Verify RPM Version Log To Console Verify Log To Console ${x} Should Be Equal ${x} '8' Finish TestCase Log To Console Done
robot variables.robot
============================================================================== Variables :: Working With Variables ============================================================================== Verify That Variables Can Be Changed :: This test case verifies va... Start .'7' '8' .Verify '8' .Done Verify That Variables Can Be Changed :: This test case verifies va... | PASS | ------------------------------------------------------------------------------ Variables :: Working With Variables | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/tests/variables/output.xml Log: /home/andrei/robot/tests/variables/log.html Report: /home/andrei/robot/tests/variables/report.html
Article author: Andrei Olegovich