Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Create Dictionary Robot Framework

Create Dictionary keyword is used to create and return a dictionary based on a given item. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Create Dictionary Test
    [Documentation]    Creates and returns a dictionary based on given items
    Comment    Create a Dictionary Object
    ${MyDictionary}=    Create Dictionary
    Comment    Adds the given key_value_pairs and items to the dictionary
    Set To Dictionary    ${MyDictionary}    Foo1    Value1
    Set To Dictionary    ${MyDictionary}    Foo2    Value2
    Set To Dictionary    ${MyDictionary}    Foo3    Value3
    Set To Dictionary    ${MyDictionary}    Foo4    Value4
    Comment    Logs the size and contents of the dictionary using given level.
    Log Dictionary    ${MyDictionary}
    Comment    Copy Existing Dictionary to a new Dictionary
    ${MyDictionary2}=    Copy Dictionary    ${MyDictionary}
    Log Dictionary    ${MyDictionary2}
    Comment    Returns keys of the given dictionary
    ${Keys}=    Get Dictionary Keys    ${MyDictionary}
    Log    ${Keys}
    Comment    Returns a value from the given dictionary based on the given key
    ${keyValue}=    Get From Dictionary    ${MyDictionary}    Foo2
    Log    ${keyValue}
    Comment    Removes the given keys from the dictionary.
    Remove From Dictionary    ${MyDictionary}    Foo1
    Log Dictionary    ${MyDictionary}
    Comment    Keeps the given keys in the dictionary and removes all other.
    Keep In Dictionary    ${MyDictionary}    Foo4
    Log Dictionary    ${MyDictionary}
    Dictionary Should Contain Key    ${MyDictionary}    Foo4

Run the script above and check the result.

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Create Dictionary Test
20150423 23:52:27.737 :  INFO : ${MyDictionary} = {}
20150423 23:52:27.747 :  INFO : 
Dictionary size is 4 and it contains following items:
Foo1: Value1
Foo2: Value2
Foo3: Value3
Foo4: Value4
20150423 23:52:27.747 :  INFO : ${MyDictionary2} = {u'Foo4': u'Value4', u'Foo1': u'Value1', u'Foo2': u'Value2', u'Foo3': u'Value3'}
20150423 23:52:27.747 :  INFO : 
Dictionary size is 4 and it contains following items:
Foo1: Value1
Foo2: Value2
Foo3: Value3
Foo4: Value4
20150423 23:52:27.747 :  INFO : ${Keys} = [u'Foo1', u'Foo2', u'Foo3', u'Foo4']
20150423 23:52:27.747 :  INFO : [u'Foo1', u'Foo2', u'Foo3', u'Foo4']
20150423 23:52:27.747 :  INFO : ${keyValue} = Value2
20150423 23:52:27.757 :  INFO : Value2
20150423 23:52:27.757 :  INFO : Removed item with key 'Foo1' and value 'Value1'.
20150423 23:52:27.757 :  INFO : 
Dictionary size is 3 and it contains following items:
Foo2: Value2
Foo3: Value3
Foo4: Value4
20150423 23:52:27.757 :  INFO : Removed item with key 'Foo2' and value 'Value2'.
20150423 23:52:27.757 :  INFO : Removed item with key 'Foo3' and value 'Value3'.
20150423 23:52:27.757 :  INFO : 
Dictionary has one item:
Foo4: Value4
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Create Dictionary Test

The test passed. In this example these keywords below are used as well.

Set To Dictionary        Adds the given key_value_pairs and items to the dictionary
Log Dictionary           Logs the size and contents of the dictionary using given level.
Remove From Dictionary   Removes the given keys from the dictionary
Keep In Dictionary       Keeps the given keys in the dictionary and removes all other
Dictionary Should Contain Key  Verifies that a dictionary should contain the key.