@@ -163,15 +163,16 @@ the arguments as explained below:
163
163
164
164
.. _list variable :
165
165
.. _list variables :
166
+ .. _list expansion :
166
167
167
168
List variable syntax
168
169
~~~~~~~~~~~~~~~~~~~~
169
170
170
171
When a variable is used as a scalar like `${EXAMPLE} `, its value is be
171
172
used as-is. If a variable value is a list or list-like, it is also possible
172
- to use it as a list variable like `@{EXAMPLE} `. In this case individual list
173
- items are passed in as arguments separately . This is easiest to explain with
174
- an example. Assuming that a variable `@{USER} ` has value `['robot', 'secret'] `,
173
+ to use it as a list variable like `@{EXAMPLE} `. In this case the list is expanded
174
+ and individual items are passed in as separate arguments . This is easiest to explain
175
+ with an example. Assuming that a variable `@{USER} ` has value `['robot', 'secret'] `,
175
176
the following two test cases are equivalent:
176
177
177
178
.. sourcecode :: robotframework
@@ -189,6 +190,23 @@ requires its value to be a Python list or list-like object. Robot Framework
189
190
does not allow strings to be used as lists, but other iterable objects such
190
191
as tuples or dictionaries are accepted.
191
192
193
+ Starting from Robot Framework 4.0, list expansion can be used in combination with
194
+ `list item access `__ making these usages possible:
195
+
196
+ .. sourcecode :: robotframework
197
+
198
+ *** Test Cases * **
199
+ Nested container
200
+ ${nested} = Evaluate [['a', 'b', 'c'], {'key': ['x', 'y']}]
201
+ Log Many @{nested}[0] # Logs 'a', 'b' and 'c'.
202
+ Log Many @{nested}[1][key] # Logs 'x' and 'y'.
203
+
204
+ Slice
205
+ ${items} = Create List first second third
206
+ Log Many @{items}[1:] # Logs 'second' and 'third'.
207
+
208
+ __ `Accessing sequence items `_
209
+
192
210
Using list variables with other data
193
211
''''''''''''''''''''''''''''''''''''
194
212
@@ -229,6 +247,7 @@ __ `All available settings in test data`_
229
247
230
248
.. _dictionary variable :
231
249
.. _dictionary variables :
250
+ .. _dictionary expansion :
232
251
233
252
Dictionary variable syntax
234
253
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -237,7 +256,7 @@ As discussed above, a variable containing a list can be used as a `list
237
256
variable `_ to pass list items to a keyword as individual arguments.
238
257
Similarly a variable containing a Python dictionary or a dictionary-like
239
258
object can be used as a dictionary variable like `&{EXAMPLE} `. In practice
240
- this means that individual items of the dictionary are passed as
259
+ this means that the dictionary is expanded and individual items are passed as
241
260
`named arguments `_ to the keyword. Assuming that a variable `&{USER} ` has
242
261
value `{'name': 'robot', 'password': 'secret'} `, the following two test cases
243
262
are equivalent.
@@ -251,6 +270,11 @@ are equivalent.
251
270
Dict Variable
252
271
Login &{USER}
253
272
273
+ Starting from Robot Framework 4.0, dictionary expansion can be used in combination with
274
+ `dictionary item access `__ making usages like `&{nested}[key] ` possible.
275
+
276
+ __ `Accessing individual dictionary items `_
277
+
254
278
Using dictionary variables with other data
255
279
''''''''''''''''''''''''''''''''''''''''''
256
280
@@ -284,11 +308,16 @@ are imports, setups and teardowns where dictionaries can be used as arguments.
284
308
Accessing list and dictionary items
285
309
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286
310
287
- It is possible to access items of subscriptable variables, e.g. lists and
288
- dictionaries, using special syntax `${var}[item] `. Accessing items is an old
289
- feature, but prior to Robot Framework 3.1 the syntax was `@{var}[item] ` with
290
- lists and `&{var}[item] ` with dictionaries. The old syntax was deprecated in
291
- Robot Framework 3.2 and will not be supported in the future.
311
+ It is possible to access items of subscriptable variables, e.g. lists and dictionaries,
312
+ using special syntax like `${var}[item] ` or `${var}[nested][item] `.
313
+ Starting from Robot Framework 4.0, it is also possible to use item access together with
314
+ `list expansion `_ and `dictionary expansion `_ by using syntax `@{var}[item] ` and
315
+ `&{var}[item] `, respectively.
316
+
317
+ .. note :: Prior to Robot Framework 3.1 the normal item access syntax was `@{var}[item]`
318
+ with lists and `&{var}[item] ` with dictionaries. Robot Framework 3.1 introduced
319
+ the generic `${var}[item] ` syntax along with some other nice enhancements and
320
+ the old item access syntax was deprecated in Robot Framework 3.2.
292
321
293
322
.. _sequence items :
294
323
@@ -300,21 +329,20 @@ It is possible to access a certain item of a variable containing a `sequence`__
300
329
is the index of the selected value. Indices start from zero, negative indices
301
330
can be used to access items from the end, and trying to access an item with
302
331
too large an index causes an error. Indices are automatically converted to
303
- integers, and it is also possible to use variables as indices. Sequence items
304
- accessed in this manner can be used similarly as scalar variables.
332
+ integers, and it is also possible to use variables as indices.
305
333
306
334
.. sourcecode :: robotframework
307
335
308
336
*** Test Cases * **
309
- Sequence variable item
337
+ Positive index
310
338
Login ${USER}[0] ${USER}[1]
311
339
Title Should Be Welcome ${USER}[0]!
312
340
313
341
Negative index
314
- Log ${SEQUENCE}[-1]
342
+ Keyword ${SEQUENCE}[-1]
315
343
316
344
Index defined as variable
317
- Log ${SEQUENCE}[${INDEX}]
345
+ Keyword ${SEQUENCE}[${INDEX}]
318
346
319
347
Sequence item access supports also the `same "slice" functionality as Python `__
320
348
with syntax like `${var}[1:] `. With this syntax you do not get a single
@@ -337,14 +365,13 @@ specify the start index, the end index, and the step:
337
365
Keyword ${SEQUENCE}[::2]
338
366
Keyword ${SEQUENCE}[1:-1:10]
339
367
340
- .. note :: The slice syntax is new in Robot Framework 3.1 and does not work
341
- with the old `@{var}[index ] ` syntax .
368
+ .. note :: The slice syntax is new in Robot Framework 3.1. It was extended to work
369
+ with ` list expansion `_ like `@{var}[1: ] ` in Robot Framework 4.0 .
342
370
343
- .. note :: With earlier Robot Framework versions accessing items with
344
- an index or a slice was only supported with variables containing
345
- lists, tuples, or other objects considered list-like. Starting
346
- from Robot Framework 3.2, all sequences, including strings and
347
- bytes, are supported.
371
+ .. note :: Prior to Robot Framework 3.2, item and slice access was only supported
372
+ with variables containing lists, tuples, or other objects considered
373
+ list-like. Nowadays all sequences, including strings and bytes, are
374
+ supported.
348
375
349
376
__ https://docs.python.org/3/glossary.html#term-sequence
350
377
__ https://docs.python.org/glossary.html#term-slice
0 commit comments