Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Page Scroll Using Robot Framework Python

When using Robot Framework to test web application, sometimes, robot needs to scroll page to a location to display web elements or to do other operations. We can achieve scroll page by using keywords "Execute JavaScript window.scrollTo(x,y)".  See the example below.

 

Note: in this example, I created a custom keywords "Scroll Page To Location" with two arguments "${x_location} and ${y_location}".  I use the keyword in the ScrollPage test case under Robot Example Suite.

create a robot project as shown below.

ScrollPage Test Case Step:

Text View Image

Text View

*** Settings ***
Library           Selenium2Library

*** Test Cases ***
Scroll Page
    Open Browser    http://demo.mahara.org    firefox
    Maximize Browser Window
    Sleep    5s
    Scroll Page To Location    0    2000

*** Keywords ***
Scroll Page To Location
    [Arguments]    ${x_location}    ${y_location}
    Execute JavaScript    window.scrollTo(${x_location},${y_location})

Run the test and observer the result. The test passed. In this script, Sleep 5s is for you to observe the page scroll, so in actual test framework, you can remove that step.

Print

Get All Links in Robot Framework Python

In Robot Framework, you can use the keyword "Get All Links" to count and verify all the links. In this articles, you will learn how to use the keyword "Get All Links" to get all the links that contain text. Mahara ePortfolio demo site is used as an example.

 

Code Implementation

1. Open Browser and navigate to the site "http://demo.mahara.org";

2. Count all the links with the xpath "//a";

3. Create a list item to contain each link that has text length greater than 1

4. Loop through each link and store text value in the list

5. Loop through the list and print each link

6. Close the browser

See the robot framework code below

*** Settings ***
Library           Selenium2Library
Library           Collections

*** Test Cases ***
Get All Links
    [Tags]    Links
    Open Browser    http://demo.mahara.org    firefox
    Maximize Browser Window
    Comment    Count Number Of Linkds on the Page
    ${AllLinksCount}=    Get Matching Xpath Count    //a
    Comment    Log links count
    Log    ${AllLinksCount}
    Comment    Create a list to store link texts
    @{LinkItems}    Create List
    Comment    Loop through all links and store links value that has length more than 1 character
    : FOR    ${INDEX}    IN RANGE    1    ${AllLinksCount}
    \    Log    ${INDEX}
    \    ${lintext}=    Get Text    xpath=(//a)[${INDEX}]
    \    Log    ${lintext}
    \    ${linklength}    Get Length    ${lintext}
    \    Run Keyword If    ${linklength}>1    Append To List    ${LinkItems}    ${lintext}
    ${LinkSize}=    Get Length    ${LinkItems}
    Log    ${LinkSize}
    Comment    Print all links
    : FOR    ${ELEMENT}    IN    @{LinkItems}
    \    Log    ${ELEMENT}
    Close Browser

Run the robot code and see the result below

Starting test: Robotexample.Links Test.Get All Links
20150309 12:19:20.551 :  INFO : Opening browser 'firefox' to base url 'http://demo.mahara.org'
20150309 12:19:29.102 :  INFO : ${AllLinksCount} = 15
20150309 12:19:29.106 :  INFO : 15
20150309 12:19:29.108 :  INFO : @{LinkItems} = [ ]
20150309 12:19:29.110 :  INFO : 1
20150309 12:19:29.128 :  INFO : ${lintext} = 
20150309 12:19:29.130 :  INFO : 
20150309 12:19:29.131 :  INFO : Length is 0
20150309 12:19:29.132 :  INFO : ${linklength} = 0
20150309 12:19:29.134 :  INFO : 2
20150309 12:19:29.154 :  INFO : ${lintext} = 
20150309 12:19:29.155 :  INFO : 
20150309 12:19:29.157 :  INFO : Length is 0
20150309 12:19:29.157 :  INFO : ${linklength} = 0
20150309 12:19:29.159 :  INFO : 3
20150309 12:19:29.180 :  INFO : ${lintext} = Mahara user manual
20150309 12:19:29.181 :  INFO : Mahara user manual
20150309 12:19:29.182 :  INFO : Length is 18
20150309 12:19:29.183 :  INFO : ${linklength} = 18
20150309 12:19:29.188 :  INFO : 4
20150309 12:19:29.206 :  INFO : ${lintext} = 
20150309 12:19:29.207 :  INFO : 
20150309 12:19:29.208 :  INFO : Length is 0
20150309 12:19:29.209 :  INFO : ${linklength} = 0
20150309 12:19:29.211 :  INFO : 5
20150309 12:19:29.228 :  INFO : ${lintext} = Register
20150309 12:19:29.230 :  INFO : Register
20150309 12:19:29.232 :  INFO : Length is 8
20150309 12:19:29.232 :  INFO : ${linklength} = 8
20150309 12:19:29.236 :  INFO : 6
20150309 12:19:29.253 :  INFO : ${lintext} = Lost username / password
20150309 12:19:29.255 :  INFO : Lost username / password
20150309 12:19:29.256 :  INFO : Length is 24
20150309 12:19:29.256 :  INFO : ${linklength} = 24
20150309 12:19:29.260 :  INFO : 7
20150309 12:19:29.280 :  INFO : ${lintext} = Persona
20150309 12:19:29.281 :  INFO : Persona
20150309 12:19:29.282 :  INFO : Length is 7
20150309 12:19:29.283 :  INFO : ${linklength} = 7
20150309 12:19:29.286 :  INFO : 8
20150309 12:19:29.305 :  INFO : ${lintext} = Mahara user manual
20150309 12:19:29.306 :  INFO : Mahara user manual
20150309 12:19:29.307 :  INFO : Length is 18
20150309 12:19:29.308 :  INFO : ${linklength} = 18
20150309 12:19:29.311 :  INFO : 9
20150309 12:19:29.329 :  INFO : ${lintext} = Mahara wiki
20150309 12:19:29.330 :  INFO : Mahara wiki
20150309 12:19:29.331 :  INFO : Length is 11
20150309 12:19:29.332 :  INFO : ${linklength} = 11
20150309 12:19:29.335 :  INFO : 10
20150309 12:19:29.352 :  INFO : ${lintext} = Mahara homepage
20150309 12:19:29.353 :  INFO : Mahara homepage
20150309 12:19:29.354 :  INFO : Length is 15
20150309 12:19:29.355 :  INFO : ${linklength} = 15
20150309 12:19:29.359 :  INFO : 11
20150309 12:19:29.378 :  INFO : ${lintext} = 
20150309 12:19:29.379 :  INFO : 
20150309 12:19:29.380 :  INFO : Length is 0
20150309 12:19:29.380 :  INFO : ${linklength} = 0
20150309 12:19:29.383 :  INFO : 12
20150309 12:19:29.399 :  INFO : ${lintext} = Terms and conditions
20150309 12:19:29.400 :  INFO : Terms and conditions
20150309 12:19:29.401 :  INFO : Length is 20
20150309 12:19:29.402 :  INFO : ${linklength} = 20
20150309 12:19:29.405 :  INFO : 13
20150309 12:19:29.426 :  INFO : ${lintext} = Privacy statement
20150309 12:19:29.427 :  INFO : Privacy statement
20150309 12:19:29.428 :  INFO : Length is 17
20150309 12:19:29.429 :  INFO : ${linklength} = 17
20150309 12:19:29.432 :  INFO : 14
20150309 12:19:29.447 :  INFO : ${lintext} = About
20150309 12:19:29.449 :  INFO : About
20150309 12:19:29.450 :  INFO : Length is 5
20150309 12:19:29.450 :  INFO : ${linklength} = 5
20150309 12:19:29.453 :  INFO : Length is 10
20150309 12:19:29.454 :  INFO : ${LinkSize} = 10
20150309 12:19:29.454 :  INFO : 10
20150309 12:19:29.457 :  INFO : Mahara user manual
20150309 12:19:29.458 :  INFO : Register
20150309 12:19:29.460 :  INFO : Lost username / password
20150309 12:19:29.461 :  INFO : Persona
20150309 12:19:29.463 :  INFO : Mahara user manual
20150309 12:19:29.465 :  INFO : Mahara wiki
20150309 12:19:29.467 :  INFO : Mahara homepage
20150309 12:19:29.469 :  INFO : Terms and conditions
20150309 12:19:29.471 :  INFO : Privacy statement
20150309 12:19:29.472 :  INFO : About
Ending test:   Robotexample.Links Test.Get All Links

Test result shows that the test passed

Print

Writing To Excel In Robot Framework

In some cases, users may need to write some test data or test result data into Excel Spreadsheet. Although there is robotframework-excellibrary available for use, it is not easy to manipulate Excel file with provided keywords. In this article, I explain how to use xlsxwriter python package to write to Excel File in Robot Framework. XlsxWriter is a Python module that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file.

Here is a comprehensive list of tools for working with Python and Excel. It covers writing Excel Add-Ins in Python, reading and writing Excel files, and interacting with Excel. It's a great resource for understanding the differences between all the different Python/Excel tools out there.
 

 

Here is the implementation idea.

1. Install XlsxWriter Python  package in your computer that runs robot framework;

2. Create a custom python class to write to excel file;

3. Add the custom python file as a resource in the robot framework test suite;

4. Create a list with row, column, and content in the robot framework;

5. Using the custom python file to write the content to an Excel File (see the picture below for the excel file test3.xlsx);

Picture 1: robot framework resource and library

Picture 2: robot framework test case steps

Picture 3: robot framework test result

Code Implementation

Note: after writing code for "CreateExcelFile.py", you need to save it in the robot framework test suite folder.

Robot Framework Ride Code

*** Settings ***
Resource          CreateExcelFile.py
Library           Collections
Library           CreateExcelFile.ExcelUtility

*** Variables ***

*** Test Cases ***
Write Excel Test
    [Tags]
    @{content}    Create List
    Append To List    ${content}    1    1    Test Case 1
    Append To List    ${content}    2    1    Test Case 2
    Append To List    ${content}    3    1    Test Case 3
    Append To List    ${content}    4    1    Test Case 4
    Write To Excel File    test3.xlsx    ${content}

Run the test and see the result. The test passed. "test3.xlsx" file is created. 

Robotexample.Excel Test.Excel Suite                                                                                           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
======================================================================================================================================
Robotexample.Excel Test                                                                                                       | 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
======================================================================================================================================

CreateExcelFile.py

Print

Select Window Robot Framework Python

In some cases, we need to to switch between windows on test automation. In the example below, we need to select different windows to do verification. 

In Robot Framework, Select Window keyword can switch between windows. 

 Windows Title Note
 Robot Framework  First Window
 After clicking on the Get Libraries link, the page displays as below    
 Robot Framework  First Window
 After clicking on the Builtit link, a new window displays as below    

 Robot Framework 

Documentation

Second Window

 This is a new 

windows 

 

 Clicking on the View button near Builtin,  a window displays below    
 Builtin  Second Window

 Robot Framework Code Implementation

Click here to see the final test result

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

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

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

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

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

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.