Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

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

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

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

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

Manipulate Dictionary In Robot Framework

In Robot Framework, users can use following keywords to manipulate Dictionaries.  

 

Create Dictionary (used for creating a new dictionary)
Set To Dictionary (used for adding key and value to a dictionary)
Copy Dictionary (used for copying existing dictionary to a new dictionary)
Log Dictionary (used for logging dictionary keys and values)
Get Dictionary Keys (used for retrieving all keys in a dictionary)
Get From Dictionary (used for returning a value of a key)
Remove From Dictionary (used for removing given keys from a dictionary)
Keep In Dictionary (used for keeping the given keys in the dictionary and removes all other) 
Dictionary Should Contain Key (used for verifying that the dictionary should contain the key) 

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 see the result below

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

In this example, we used all the keywords mentioned above. The test passed.

Print

Convert To List Robot Framework

In Robot Framework, the keyword "Convert To List" is used to to convert the given items to a list. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Convert To List Test
    ${tup1}    Set Variable    a    b    c    d    sm
    @{MyList}=    Convert To List    ${tup1}
    Log List    ${MyList}
    ${ListLength}=    Get Length    ${MyList}
    Log    'List Size is : ' ${ListLength}
    List Should Contain Value    ${MyList}    sm
 

Run the script above and see the result

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Convert To List Test
20150416 23:42:46.834 :  INFO : ${tup1} = [u'a', u'b', u'c', u'd', u'sm']
20150416 23:42:46.834 :  INFO : @{MyList} = [ a | b | c | d | sm ]
20150416 23:42:46.834 :  INFO : 
List length is 5 and it contains following items:
0: a
1: b
2: c
3: d
4: sm
20150416 23:42:46.844 :  INFO : Length is 5
20150416 23:42:46.844 :  INFO : ${ListLength} = 5
20150416 23:42:46.844 :  INFO : 'List Size is : ' 5
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Convert To List Test

In the example above, we define a variable ${tup1} that contains few values: 'a','b','c','d','sm'; and then we convert the variable to a list @{MyList}; we logged list length and verified that the list contained the value sm.

Print

Combine Lists Robot Framework

In Robot Framework, the keyword "Combine Lists" combine the given lists and return the result. See the example below. 

 

*** Settings ***
Library           Collections

*** Test Cases ***
Combine Lists Test
    [Documentation]    Combines the given lists together and returns the result. Given list does not get changed.
    @{a}=    Create List    a    b    c
    @{b}=    Create List    d    e    f
    @{ab}=    Combine Lists    ${a}    ${b}
    Log List    ${ab}
    List Should Contain Value    ${ab}    a

Run the script above the see the result below

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Combine Lists Test
20150416 23:36:51.695 :  INFO : @{a} = [ a | b | c ]
20150416 23:36:51.705 :  INFO : @{b} = [ d | e | f ]
20150416 23:36:51.705 :  INFO : @{ab} = [ a | b | c | d | e | f ]
20150416 23:36:51.705 :  INFO : 
List length is 6 and it contains following items:
0: a
1: b
2: c
3: d
4: e
5: f
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Combine Lists Test

The result showed that List a contains (a,b,c); List b contains (d,e,f); when we combine List a and b, List ab contained (a,b,c,d,e,f); we verified that List ab contained item value f;

Print

Append To List Robot Framework

In Robot Framework, the keyword "Append To List" is used to add values to the end of list.  See the example below. 

*** Settings ***
Library           Collections

*** Test Cases ***
Append To List Test
    [Documentation]    Adds values to the end of list
    @{MyList}=    Create List
    Append To List    ${MyList}    a
    Append To List    ${MyList}    b
    Append To List    ${MyList}    c
    Append To List    ${MyList}    d
    Append To List    ${MyList}    abcd
    Log List    ${MyList}
    List Should Contain Value    ${MyList}    abcd
 

Run the script and see the result below

Starting test: RobotCollectionsExample.Collections Keyword Test Suite.Append To List Test
20150416 23:25:28.476 :  INFO : @{MyList} = [ ]
20150416 23:25:28.476 :  INFO : 
List length is 5 and it contains following items:
0: a
1: b
2: c
3: d
4: abcd
Ending test:   RobotCollectionsExample.Collections Keyword Test Suite.Append To List Test

The result shows that a, b, c, d, and abcd are added to the list ${MyList}

Print

Create List Robot Framework

In Robot Framework, Create List keyword is used to create a list object. To use the keyword, we need to import the library Collections.  See the example below. 

We create a list object named ${MyList} and assign values "item1", "item2", and "item3" when creating the list. Using the keyword "Append To List" to add another value "item4". We do assertion on item4. 

 

Note: we tagged the test case as "CollectionsTest".

*** Settings ***
Library           Collections

*** Test Cases ***
Create List Test Case
    [Tags]    CollectionsTest
    @{MyList}=    Create List    item1    item2    item3
    Append To List    ${MyList}    item4
    Log List    ${MyList}
    List Should Contain Value    ${MyList}    item4
 

Execute the script and see the result below:

Starting test: RobotExample.Collections Test Suite.Create List Test Case
20150415 23:51:46.481 :  INFO : @{MyList} = [ item1 | item2 | item3 ]
20150415 23:51:46.484 :  INFO : 
List length is 4 and it contains following items:
0: item1
1: item2
2: item3
3: item4
Ending test:   RobotExample.Collections Test Suite.Create List Test Case

The test passed

Print

How To Read Values From Text File In Robot Framework

In Robot Framework, you can use the keyword "Get File" in OperatingSystem to read values from a text file. Let's say, you have a text file "TestFile.txt" in your robot project folder "D:\RobotExample". Here is the file content. 

 

Selenium Master 

Here is the robot framework code to read the text file and assign its content to a variable.

*** Settings ***
Library           OperatingSystem

*** Test Cases ***
Read Text File
    [Tags]    test text
    ${TextFileContent}=    Get File    TestFile.txt
    Log    ${TextFileContent}

Run the robot framework script and see the result log below; test result is written under TestResult folder. 

RobotExample                                                                                             
=========================================================================================================
RobotExample.Read File Suite                                                                             
=========================================================================================================
Read Text File                                                                                   | PASS |
---------------------------------------------------------------------------------------------------------
RobotExample.Read File Suite                                                                     | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
=========================================================================================================
RobotExample                                                                                     | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
=========================================================================================================
Output:  D:\RobotExample\TestResult\output-20150411-172035.xml
Log:     D:\RobotExample\TestResult\log-20150411-172036.html
Report:  D:\RobotExample\TestResult\report-20150411-172036.html

test finished 20150411 17:20:36
Starting test: RobotExample.Read File Suite.Read Text File
20150411 17:20:36.377 :  INFO : Getting file 'D:\RobotExample\TestFile.txt'
20150411 17:20:36.377 :  INFO : ${TextFileContent} = Selenium Master
20150411 17:20:36.377 :  INFO : Selenium Master
Ending test:   RobotExample.Read File Suite.Read Text File

Test passed and the text file content is assigned to the variable ${TextFileContent}.