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. 
Print

Copy Dictionary Robot Framework

Copy Dictionary keyword returns a copy of the given dictionary. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Copy Dictionary Test
    Comment    Returns a copy of the given dictionary
    ${MyDictionary}=    Create Dictionary
    Set To Dictionary    ${MyDictionary}    Foo1    Value1
    Set To Dictionary    ${MyDictionary}    Foo2    Value2
    ${CopiedDictionary}=    Copy Dictionary    ${MyDictionary}
    Log Dictionary    ${CopiedDictionary}
    Dictionary Should Contain Key    ${CopiedDictionary}    Foo1
 

Run the script and see the result below

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Copy Dictionary Test
20150424 00:01:50.622 :  INFO : ${MyDictionary} = {}
20150424 00:01:50.622 :  INFO : ${CopiedDictionary} = {u'Foo1': u'Value1', u'Foo2': u'Value2'}
20150424 00:01:50.622 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Copy Dictionary Test
Print

Copy List Robot Framework

Copy List keyword returns a copy of the given list. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Copy List Test
    comment    Returns a copy of the given list
    @{MyList}=    Create List    Foo1    Foo2    Foo3
    Log List    ${MyList}
    @{CopiedList}=    Copy List    ${MyList}
    Log List    ${CopiedList}
    List Should Contain Value    ${CopiedList}    Foo3

Run the script and see the result below.

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Copy List Test
20150424 00:07:37.103 :  INFO : @{MyList} = [ Foo1 | Foo2 | Foo3 ]
20150424 00:07:37.103 :  INFO : 
List length is 3 and it contains following items:
0: Foo1
1: Foo2
2: Foo3
20150424 00:07:37.103 :  INFO : @{CopiedList} = [ Foo1 | Foo2 | Foo3 ]
20150424 00:07:37.103 :  INFO : 
List length is 3 and it contains following items:
0: Foo1
1: Foo2
2: Foo3
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Copy List Test
Print

Count Values In List Robot Framework

Count Values In List Returns the number of occurrences of the given value in list. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Count Values In List Test
    Comment    Returns the number of occurrences of the given value in list.
    @{MyList}=    Create List    1
    Append To List    ${MyList}    2
    Append To List    ${MyList}    2
    Append To List    ${MyList}    3
    ${count}=    Count Values In List    ${MyList}    2
    Log    ${count}
    Should Be Equal    '${count}'    '2'

Copy List Test
    comment    Returns a copy of the given list
    @{MyList}=    Create List    Foo1    Foo2    Foo3
    Log List    ${MyList}
    @{CopiedList}=    Copy List    ${MyList}
    Log List    ${CopiedList}
    List Should Contain Value    ${CopiedList}    Foo3

Run the script and see the result below. The value "2" appeared twice in the list, so the assertion passed in the test

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Count Values In List Test
20150424 00:09:56.644 :  INFO : @{MyList} = [ 1 ]
20150424 00:09:56.654 :  INFO : ${count} = 2
20150424 00:09:56.654 :  INFO : 2
20150424 00:09:56.654 :  INFO : 
Argument types are:
<type 'unicode'>
<type 'unicode'>
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Count Values In List Test
Print

Get Dictionary Items Robot Framework

Get Dictionary Items keyword returns the items of a given dictionary. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Get Dictionary Items Test
    Comment    Returns items of the given dictionary
    ${dict}=    Create Dictionary
    Set To Dictionary    ${dict}    Foo1    Value1
    Set To Dictionary    ${dict}    Foo2    Value2
    ${items}=    Get Dictionary Items    ${dict}
    Log List    ${items}
    List Should Contain Value    ${items}    Value1

Run the script and see the result

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Get Dictionary Items Test
20150424 00:16:16.962 :  INFO : ${dict} = {}
20150424 00:16:16.962 :  INFO : ${items} = [u'Foo1', u'Value1', u'Foo2', u'Value2']
20150424 00:16:16.962 :  INFO : 
List length is 4 and it contains following items:
0: Foo1
1: Value1
2: Foo2
3: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Get Dictionary Items Test
Print

Dictionaries Should Be Equal Robot Framework

Dictionaries Should Be Equal keyword is used to compare dictionaries. If they are not equal, the keyword will Fail. See the implementation below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Dictionaries Should Be Equal Test
    Comment    Fails if the given dictionaries are not equal.
    ${Dict1}=    Create Dictionary
    Set To Dictionary    ${Dict1}    Foo1    Value1
    Set To Dictionary    ${Dict1}    Foo2    Value2
    Log Dictionary    ${Dict1}
    ${Dict2}=    Create Dictionary
    Set To Dictionary    ${Dict2}    Foo1    Value1
    Set To Dictionary    ${Dict2}    Foo2    Value2
    Log Dictionary    ${Dict2}
    Dictionaries Should Be Equal    ${Dict1}    ${Dict2}

Run the test and observe the result. The test passed.

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Dictionaries Should Be Equal Test
20150426 08:15:02.351 :  INFO : ${Dict1} = {}
20150426 08:15:02.351 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
20150426 08:15:02.351 :  INFO : ${Dict2} = {}
20150426 08:15:02.351 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Dictionaries Should Be Equal Test
Print

Dictionary Should Contain Item Robot Framework

Dictionary Should Contain Item keyword is used to check if the dictionary contain an item of key/value pair. See the implementation below.

 

*** Settings ***
Library           Collections

*** Test Cases ***
Dictionary Should Contain Item Test
    Comment    An item of key/value must be found in a dictionary
    ${MyDict}=    Create Dictionary
    Set To Dictionary    ${MyDict}    Foo1    Value1
    Set To Dictionary    ${MyDict}    Foo2    Value2
    Log Dictionary    ${MyDict}
    Dictionary Should Contain Item    ${MyDict}    Foo1    Value1

Run the script above. The test passed because ${MyDict} contains the item Foo1/Value1

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Item Test
20150426 08:17:22.786 :  INFO : ${MyDict} = {}
20150426 08:17:22.786 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Item Test
Print

Dictionary Should Contain Key Robot Framework

Dictionary Should Contain Key keyword is used to verify if a given key is found in the dictionary. If not, the keyword will Fail. See the implementation below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Dictionary Should Contain Key Test
    Comment    Fails if key is not found from dictionary
    ${MyDict}=    Create Dictionary
    Set To Dictionary    ${MyDict}    A1    Value1
    Set To Dictionary    ${MyDict}    A2    Value2
    Log Dictionary    ${MyDict}
    Dictionary Should Contain Key    ${MyDict}    A1
    Comment    You can use Dictionary Should Not Contain Key in similar ways

Run the script above. The test passed. 

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Key Test
20150426 08:23:18.669 :  INFO : ${MyDict} = {}
20150426 08:23:18.669 :  INFO : 
Dictionary size is 2 and it contains following items:
A1: Value1
A2: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Key Test
Print

Dictionary Should Contain Value Robot Framework

Dictionary Should Contain Value keyword is used to check if a dictionary contains a value. If value is not found, the keyword Fail. See the implementation below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Dictionary Should Contain Value Test
    Comment    Fails if value is not found from dictionary
    ${MyDict}=    Create Dictionary    Foo1    Value1
    Set To Dictionary    ${MyDict}    Foo2    Value2
    Log Dictionary    ${MyDict}
    Dictionary Should Contain Value    ${MyDict}    Value2
    Comment    You can use keyword Dictionary Should Not Contain Value in similar ways

Run the script above. The test passed. 

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Value Test
20150426 08:26:50.078 :  INFO : ${MyDict} = {u'Foo1': u'Value1'}
20150426 08:26:50.078 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Value Test
Print

Dictionary Should Contain Sub Dictionary Robot Framework

Dictionary Should Contain Sub Dictionary keyword is used to check if all items in dict1 are from dict2. The keyword will fail if there are differences between the keys and values. See the implementation below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Dictionary Should Contain Sub Dictionary Test
    Comment    Fails unless all items in dict2 are found from dict1
    ${dict2}=    Create Dictionary
    Set To Dictionary    ${dict2}    Foo1    Value1
    Set To Dictionary    ${dict2}    Foo2    Value2
    Log Dictionary    ${dict2}
    ${dict1}=    Create Dictionary
    Set To Dictionary    ${dict1}    Foo1    Value1
    Set To Dictionary    ${dict1}    Foo2    Value2
    Set To Dictionary    ${dict1}    Foo3    Value3
    Set To Dictionary    ${dict1}    Foo4    Value4
    Log Dictionary    ${dict1}
    Dictionary Should Contain Sub Dictionary    ${dict1}    ${dict2}

Run the script below. The test passed. 

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Sub Dictionary Test
20150426 08:33:25.678 :  INFO : ${dict2} = {}
20150426 08:33:25.678 :  INFO : 
Dictionary size is 2 and it contains following items:
Foo1: Value1
Foo2: Value2
20150426 08:33:25.678 :  INFO : ${dict1} = {}
20150426 08:33:25.678 :  INFO : 
Dictionary size is 4 and it contains following items:
Foo1: Value1
Foo2: Value2
Foo3: Value3
Foo4: Value4
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Dictionary Should Contain Sub Dictionary Test