|
| 1 | +.. _ReplaceWithVAR: |
| 2 | + |
| 3 | +ReplaceWithVAR |
| 4 | +================================ |
| 5 | + |
| 6 | +Replace ``Set Variable``, ``Create Dictionary``, ``Create List`` and ``Catenate`` keywords with ``VAR``. |
| 7 | + |
| 8 | +.. |TRANSFORMERNAME| replace:: ReplaceWithVAR |
| 9 | +.. include:: disabled_hint.txt |
| 10 | + |
| 11 | +.. tab-set:: |
| 12 | + |
| 13 | + .. tab-item:: Before |
| 14 | + |
| 15 | + .. code:: robotframework |
| 16 | +
|
| 17 | + *** Keywords *** |
| 18 | + Custom Keyword |
| 19 | + ${var} Set Variable value |
| 20 | + Set Suite Variable ${SUITE_VAR} ${var} |
| 21 | + ${list} Create List ${var} second value |
| 22 | + ${string} Catenate join with spaces |
| 23 | +
|
| 24 | + .. tab-item:: After |
| 25 | + |
| 26 | + .. code:: robotframework |
| 27 | +
|
| 28 | + *** Keywords *** |
| 29 | + Custom Keyword |
| 30 | + VAR ${var} value |
| 31 | + VAR ${SUITE_VAR} ${var} scope=SUITE |
| 32 | + VAR @{list} ${var} second value |
| 33 | + VAR ${string} join with spaces separator=${SPACE} |
| 34 | +
|
| 35 | +Variable scope |
| 36 | +--------------- |
| 37 | + |
| 38 | +``VAR`` syntax supports setting the scope of the variable with ``scope=`` parameter. It's equivalent of the following |
| 39 | +keywords: |
| 40 | + |
| 41 | + - Set Local Variable |
| 42 | + - Set Task Variable |
| 43 | + - Set Test Variable |
| 44 | + - Set Suite Variable |
| 45 | + - Set Global Variable |
| 46 | + |
| 47 | +Other keywords such as ``Set Variable``, ``Catenate``, ``Create Dictionary`` and ``Create List`` create variable in the |
| 48 | +local scope. Local scope is omitted by default but can be explicitly set by using ``explicit_local`` parameter. |
| 49 | + |
| 50 | +``Set Suite Variable`` with ``children=True`` is ignored as it is not possible to have the same behaviour with ``VAR``. |
| 51 | + |
| 52 | +.. tab-set:: |
| 53 | + |
| 54 | + .. tab-item:: Before |
| 55 | + |
| 56 | + .. code:: robotframework |
| 57 | +
|
| 58 | + *** Keywords *** |
| 59 | + Custom Keyword |
| 60 | + ${var} Set Variable value |
| 61 | + Set Local Variable ${local_variable} value |
| 62 | + Set Task Variable ${task_variable} value |
| 63 | + Set Test Variable ${test_variable} |
| 64 | + Set Suite Variable ${suite_variable} value with ${value} |
| 65 | + Set Suite Variable ${suite_variable} value with ${value} children=${True} # ignored |
| 66 | + Set Global Variable ${global_variable} value |
| 67 | +
|
| 68 | + .. tab-item:: After |
| 69 | + |
| 70 | + .. code:: robotframework |
| 71 | +
|
| 72 | + *** Keywords *** |
| 73 | + Custom Keyword |
| 74 | + VAR ${var} value |
| 75 | + VAR ${local_variable} value |
| 76 | + VAR ${task_variable} value scope=TASK |
| 77 | + VAR ${test_variable} ${test_variable} scope=TEST |
| 78 | + VAR ${suite_variable} value with ${value} scope=SUITE |
| 79 | + Set Suite Variable ${suite_variable} value with ${value} children=${True} # ignored |
| 80 | + VAR ${global_variable} value scope=GLOBAL |
| 81 | +
|
| 82 | + .. tab-item:: After (explicit_local = True) |
| 83 | + |
| 84 | + .. code:: robotframework |
| 85 | +
|
| 86 | + *** Keywords *** |
| 87 | + Custom Keyword |
| 88 | + VAR ${var} value scope=LOCAL |
| 89 | + VAR ${local_variable} value scope=LOCAL |
| 90 | + VAR ${task_variable} value scope=TASK |
| 91 | + VAR ${test_variable} ${test_variable} scope=TEST |
| 92 | + VAR ${suite_variable} value with ${value} scope=SUITE |
| 93 | + Set Suite Variable ${suite_variable} value with ${value} children=${True} # ignored |
| 94 | + VAR ${global_variable} value scope=GLOBAL |
| 95 | +
|
| 96 | +Catenate |
| 97 | +-------- |
| 98 | + |
| 99 | +``Catenate`` keyword will be replaced with VAR. Separator is set using ``separator=`` parameter. ``Catenate`` keywords |
| 100 | +can be ignored by setting ``replace_catenate`` parameter to ``False``. |
| 101 | + |
| 102 | +Create Dictionary |
| 103 | +------------------ |
| 104 | + |
| 105 | +``Create Dictionary`` keyword will be replaced with VAR. Variable name will change from ``${name}`` to ``&{name}`` |
| 106 | +to reflect the type of the variable. ``Create Dictionary`` keywords can be ignored by setting |
| 107 | +``replace_create_dictionary`` parameter to ``False``. |
| 108 | + |
| 109 | +Create List |
| 110 | +------------ |
| 111 | + |
| 112 | +``Create List`` keyword will be replaced with VAR. Variable name will change from ``${name}`` to ``@{name}`` |
| 113 | +to reflect the type of the variable. ``Create List`` keywords can be ignored by setting |
| 114 | +``replace_create_list`` parameter to ``False``. |
| 115 | + |
| 116 | +Set Variable If |
| 117 | +---------------- |
| 118 | + |
| 119 | +``Set Variable If`` keyword will be replaced with IF and VAR. If value in ELSE branch is not set, implicit |
| 120 | +``${None}`` value will be used. Keywords with dynamic values (``@{values}``) are ignored since they may contain |
| 121 | +``ELSE IF`` and ``ELSE`` branches that should be also converted. ``Set Variable If`` keywords can be ignored by setting |
| 122 | +``replace_set_variable_if`` parameter to ``False``. |
| 123 | + |
| 124 | + |
| 125 | +.. tab-set:: |
| 126 | + |
| 127 | + .. tab-item:: Before |
| 128 | + |
| 129 | + .. code:: robotframework |
| 130 | +
|
| 131 | + *** Keywords *** |
| 132 | + Custom Keyword |
| 133 | + ${var} = Set Variable If ${rc} > 0 whatever |
| 134 | + ${var}= Set Variable If ${rc} == 0 zero |
| 135 | + ... ${rc} > 0 greater than zero less then zero |
| 136 | + ${var} Set Variable If ${condition} @{items} # ignored |
| 137 | +
|
| 138 | + .. tab-item:: After |
| 139 | + |
| 140 | + .. code:: robotframework |
| 141 | +
|
| 142 | + *** Keywords *** |
| 143 | + Custom Keyword |
| 144 | + IF ${rc} > 0 |
| 145 | + VAR ${var} whatever |
| 146 | + ELSE |
| 147 | + VAR ${var} ${None} |
| 148 | + END |
| 149 | + IF ${rc} == 0 |
| 150 | + VAR ${var} zero |
| 151 | + ELSE IF ${rc} > 0 |
| 152 | + VAR ${var} greater than zero |
| 153 | + ELSE |
| 154 | + VAR ${var} less then zero |
| 155 | + END |
| 156 | + ${var} Set Variable If ${condition} @{items} # ignored |
0 commit comments