Skip to content

Commit 80eaad3

Browse files
committed
UG: Small enhancements to dict/list variable docs
1 parent 233d9a4 commit 80eaad3

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

doc/userguide/src/CreatingTestData/Variables.rst

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ When a variable is used as a scalar like `${EXAMPLE}`, its value will be
171171
used as-is. If a variable value is a list or list-like, it is also possible
172172
to use as a list variable like `@{EXAMPLE}`. In this case individual list
173173
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']`,
174+
an example. Assuming that a variable `@{USER}` has value `['robot', 'secret']`,
175175
the following two test cases are equivalent:
176176

177177
.. sourcecode:: robotframework
@@ -228,7 +228,7 @@ similarly as scalar variables.
228228

229229
*** Test Cases ***
230230
List Variable Item
231-
Login @{USER}
231+
Login @{USER}[0] @{USER}[1]
232232
Title Should Be Welcome @{USER}[0]!
233233

234234
Negative Index
@@ -313,19 +313,27 @@ Accessing individual dictionary items
313313
It is possible to access a certain value of a dictionary variable
314314
with the syntax `&{NAME}[key]`, where `key` is the name of the
315315
selected value. Keys are considered to be strings, but non-strings
316-
keys can be used as variables. Dictionary items accessed in this
317-
manner can be used similarly as scalar variables:
316+
keys can be used as variables. Dictionary values accessed in this
317+
manner can be used similarly as scalar variables.
318+
319+
If a key is a string, it is possible to access its value also using
320+
attribute access syntax `${NAME.key}`. See `Creating dictionary variables`_
321+
for more details about this syntax.
318322

319323
.. sourcecode:: robotframework
320324

321325
*** Test Cases ***
322326
Dict Variable Item
323-
Login &{USER}
327+
Login &{USER}[name] &{USER}[password]
324328
Title Should Be Welcome &{USER}[name]!
325329

326-
Variable Key
330+
Key As Variable
327331
Log Many &{DICT}[${KEY}] &{DICT}[${42}]
328332

333+
Attribute Access
334+
Login ${USER.name} ${USER.password}
335+
Title Should Be Welcome ${USER.name}!
336+
329337
Using dictionary variables with settings
330338
''''''''''''''''''''''''''''''''''''''''
331339

@@ -464,7 +472,7 @@ Dictionary variables can be created in the variable table similarly as
464472
list variables. The difference is that items need to be created using
465473
`name=value` syntax or existing dictionary variables. If there are multiple
466474
items with same name, the last value has precedence. If a name contains
467-
an equal sign, it can be escaped__ with a backslash like `\=`.
475+
a literal equal sign, it can be escaped__ with a backslash like `\=`.
468476

469477
.. sourcecode:: robotframework
470478

@@ -475,16 +483,16 @@ an equal sign, it can be escaped__ with a backslash like `\=`.
475483
&{EVEN MORE} &{MANY} first=override empty=
476484
... =empty key\=here=value
477485

478-
Dictionary variables created in variable table have two extra properties
486+
Dictionary variables have two extra properties
479487
compared to normal Python dictionaries. First of all, values of these
480488
dictionaries can be accessed like attributes, which means that it is possible
481489
to use `extended variable syntax`_ like `${VAR.key}`. This only works if the
482490
key is a valid attribute name and does not match any normal attribute
483491
Python dictionaries have. For example, individual value `&{USER}[name]` can
484492
also be accessed like `${USER.name}` (notice that `$` is needed in this
485-
context), but `&{MANY}[${3}]` does not work as `${MANY.3}`.
493+
context), but using `${MANY.3}` is not possible.
486494

487-
Another special property of dictionaries created in the variable table is
495+
Another special property of dictionary variables is
488496
that they are ordered. This means that if these dictionaries are iterated,
489497
their items always come in the order they are defined. This can be useful
490498
if dictionaries are used as `list variables`_ with `for loops`_ or otherwise.
@@ -568,7 +576,7 @@ Assigning scalar variables
568576
''''''''''''''''''''''''''
569577

570578
Any value returned by a keyword can be assigned to a `scalar variable`_.
571-
As illustrated by the example below, the required syntax is very simple.
579+
As illustrated by the example below, the required syntax is very simple:
572580

573581
.. sourcecode:: robotframework
574582

@@ -599,7 +607,7 @@ Assigning list variables
599607
''''''''''''''''''''''''
600608

601609
If a keyword returns a list or any list-like object, it is possible to
602-
assign it to a `list variable`_.
610+
assign it to a `list variable`_:
603611

604612
.. sourcecode:: robotframework
605613

@@ -609,7 +617,7 @@ assign it to a `list variable`_.
609617
Length Should Be ${list} 3
610618
Log Many @{list}
611619

612-
Because all Robot Framework variables are stored in same namespace, there is
620+
Because all Robot Framework variables are stored in the same namespace, there is
613621
not much difference between assigning a value to a scalar variable or a list
614622
variable. This can be seen by comparing the last two examples above. The main
615623
differences are that when creating a list variable, Robot Framework
@@ -622,7 +630,7 @@ Assigning dictionary variables
622630
''''''''''''''''''''''''''''''
623631

624632
If a keyword returns a dictionary or any dictionary-like object, it is possible
625-
to assign it to a `dictionary variable`_.
633+
to assign it to a `dictionary variable`_:
626634

627635
.. sourcecode:: robotframework
628636

@@ -633,16 +641,18 @@ to assign it to a `dictionary variable`_.
633641
Do Something &{dict}
634642
Log ${dict.first}
635643

636-
Because all Robot Framework variables are stored in same namespace, it would
644+
Because all Robot Framework variables are stored in the same namespace, it would
637645
also be possible to assign a dictionary into a scalar variable and use it
638646
later as a dictionary when needed. There are, however, some actual benefits
639647
in creating a dictionary variable explicitly. First of all, Robot Framework
640648
verifies that the returned value is a dictionary or dictionary-like similarly
641649
as it verifies that list variables can only get a list-like value.
642-
Another benefit is that Robot Framework converts the value into a special
643-
dictionary it uses also when `creating dictionary variables`_ in the variable
644-
table. These dictionaries are sortable and their values can be accessed using
645-
attribute access like `${dict.first}` in the above example.
650+
651+
A bigger benefit is that the value is converted into a special dictionary
652+
that it uses also when `creating dictionary variables`_ in the variable table.
653+
Values in these dictionaries can be accessed using attribute access like
654+
`${dict.first}` in the above example. These dictionaries are also ordered, but
655+
if the original dictionary was not ordered, the resulting order is arbitrary.
646656

647657
Assigning multiple variables
648658
''''''''''''''''''''''''''''

0 commit comments

Comments
 (0)