Add Screenshots to Failed Tests in Robot Framework

HOME

In this tutorial, we will discuss the steps to add the screenshots to the failed tests in the Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create 3 new directories in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests, Drivers, and Resources

Below is the image of the new directories.

Step 3 – Download ChromeBinaries and place in Drivers directory

This directory contains the browser binary in it. As we are using Chrome, will keep chromedriver.exe here.

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

I will rename chromedriver.exe to Chrome.

Step 4 – Create Test Files

This directory contains multiple test case files consisting of test steps. 

Right-click on the new directory and select New File and provide the name as LoginPageTests.robot as shown below:

Below is the code for LoginPageTests.robot

*** Settings ***
Documentation       Tests to login to Login Page
Library     SeleniumLibrary
Test Setup      Open the Browser with URL
Test Teardown   Capture Screenshot On Failure
Suite Teardown  Close Browser Session
Resource       ../Resources/GenericResources.robot
Resource       ../Resources/LoginResources.robot

*** Test Cases ***

Validate Unsuccessful Login using invalid credentials

    LoginResources.Fill the login form     ${valid_username}       ${invalid_password}
    LoginResources.Verify the error message is correct


Validate successful Login

    LoginResources.Fill the login form     ${valid_username}       ${valid_password}
    DashboardResources.Verify Dashboard page opens


Validate Unsuccessful Login for blank username

     LoginResources.Fill the login form     ${blank_username}       ${valid_password}
     LoginResources.Verify the error message is displayed for username

Step 5 – Create Resources file for each page

It maintains the files which contain page elements as well as corresponding keywords. 

Right-click on the new directory and select New File and provide the name as LoginResources.robot, DashboardResources.robot and GenericResources.robot as shown below:

GenericResources.robot contains the keywords that are common to all the tests, like the opening of the browser or closing of the browser.

*** Settings ***
Documentation    A resource file with reusable keywords and variables.
Library     SeleniumLibrary

*** Variables ***
${valid_username}     Admin
${valid_password}       admin123
${invalid_username}     1234
${invalid_password}     45678
${blank_username}
${url}        https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser_name}      Chrome
${output_dir}      ./screenshots


*** Keywords ***

Open the Browser with URL
    Open Browser   ${url}    ${browser_name}   executable_path=C:/Users/vibha/Documents/Automation/Python/CrossBrowser_RobotFramework/Drivers/${browser_name}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Capture screenshot On Failure
    Run Keyword If Test Failed    Capture Page Screenshot    ${output_dir}/${TEST NAME}.png
    Close Browser

Close Browser Session
    Close Browser

Below is the code for LoginResources.robot

*** Settings ***
Documentation        All the page objects and keywords of landing page
Library     SeleniumLibrary

*** Variables ***
${login_error_message}      css:.oxd-alert-content--error
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module
${missing_username_error_message}    xpath://*[@class='oxd-form']/div[1]/div/span

*** Keywords ***

Fill the login form
    [Arguments]    ${username}      ${password}
   Input Text    css:input[name=username]   ${username}
   Input Password    css:input[name=password]   ${password}
   Click Button    css:.orangehrm-login-button


Verify the error message is correct
    Element Text Should Be    ${login_error_message}    Invalid credentials


Verify the error message is displayed for username
     Element Text Should Be    ${missing_username_error_message}      Required

Below is the code for DashboardResources.robot

*** Settings ***
Documentation        All the page objects and keywords of Dashboard page
Library     SeleniumLibrary

*** Variables ***
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module

*** Keywords ***

Verify Dashboard page opens
    Element Text Should Be    ${dashboard_title}      Dashboard

All the below-mentioned keywords are derived from SeleniumLibrary. The functionality of keywords mentioned above:

1. Open Browser − The keyword opens a new browser instance to the optional url.

2. Maximize Browser Window – This keyword maximizes the current browser window.

3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

5. Close Browser – Close the current browser.

6. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.

7. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.

The difference compared to Input Text is that this keyword does not log the given password on the INFO level.

8. Click button – This keyword is used to click the button identified by the locator. In this case, it is “Login” button.

9. Element Text Should Be – This keyword is used to verify that the current page contains the exact text identified by the locator. Here, we are checking the exact text “Invalid Credentials”.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html.

Step 6 – Execute the tests

To run this script, go to the command line and go to directory tests. We need the below command to run the Robot Framework script.

robot .

The output of the above program is

Step 8 – View Report and Log

We have 2 test cases passed and 1 failed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

The screenshots will be included in the log.html file under the specific failed test case step. Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
How to implement tagging in Robot Framework
 How to set variable values from Runtime command in Robot Framework
How to load data from CSV files in the Robot Framework?

Leave a comment