Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

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

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

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

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

Send Email in Robot Framework Python Using Gmail

When using Robot Framework Python, some customized test result log files or report files need to be emailed to QA engineers. You can create custom Gmail Email Library to send email with attachment or no attachment.  

 

Step 1:  create a folder named "GmailEmailLibrary" under C:\Python27\Lib\site-packages (assuming that you have installed python at the root of C: drive)

C:\Python27\Lib\site-packages\GmailEmailLibrary

Step 2: write following codes in the file "gmailsendemail.py" and "__init__.py"

Print

Pdf Comparison In Robot Framework Python

Pdf comparison is a challenging work in test automation. In this example below, you will learn how to compare pdf files in Robot Framework Python. To compare pdf files (1) you need to install PDFMiner on your PC. (2) you need to use the code provided by Selenium Master and install it under the folder C:\Python27\Lib\site-packages\Pdf2TextLibrary.

 

PDFMiner is a tool for extracting information from PDF documents. It focuses on getting and analyzing text data.  Selenium Master wrote a python code to get page counts of a pdf file and extract its text. In this example, we have three pdf files as listed in the table below. 

Pdf File Name Page Count Text Content
 smpdf1.pdf  1  Selenium Master Pdf Comparison
smpdf2.pdf   1  Selenium Master Pdf Comparison
smpdf3.pdf   2  Selenium Master Pdf Comparison

Page 1
Sunday, August 17, 2014

 

Selenium Master Pdf Comparison
Page 2
Sunday, August 17, 2014

 When we compare the above 3 files, page count and text content should be equal for smpdf1.pdf and smpdf2.pdf.  However, page count and text content should not be equal for smpdf1.pdf and smpdf3.pdf. Click the link to see the result file. See the python library code and robot framework code. 

Print

Get Environment Variables In Robot Framework Python

In some cases, test report should include computer name, user name and operating system to identify on which computer tests were run, who run the test, and on what operating system tests were run.  All of these information are environment variables. In Robot Framework we can obtain environment variables by using Operating System provided keywords.  See the example below. 

 

Print

Run Certain Tests With Tags in Robot Framework Python

An application may have a lot of modules to test. When your robot framework has many tests/suites, you may need to run certain tests as smoke tests or integration tests to save time or to test specific modules. In robot framework, tagging tests will enable you to pick certain tests to run. See the example below.

 

Print

Run Tests in Certain Order in Robot Framework Python

In robot framework, we may need to run tests in a certain order when tests are dependent on other tests in previous orders. Generally speaking, in a test suite, tests will run in the order by which they are created. However, for easily seeing the order of tests, users can prefix tests with numbers. See the example below.

 

Print

Read Xml File in Robot Framework Python

Robot Framework has builtIn libraries to manipulate xml file in testing. In this example, you will learn how to read xml file, get element by xpath and element attribute.