Last Updated On
In this tutorial, we will discuss the steps to add the screenshots to the failed tests in the Robot Framework.
Table Of Contents
Prerequisite:
- Install Python
- Install PIP
- Install Robot Framework
- Install Robot framework Selenium Library
- 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 – Create a new project
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
Settings
- Documentation: Allows to add the description about the Login Test page.
- Library: Import SeleniumLibrary for browser interactions.
- Test Setup: Open the Browser with URL keyword to setup the browser before each test
- Test Teardown: Capture Screenshot On Failure keyword to capture screenshots if the test fails
- Suite Teardown: Close Browser Session keyword close all the browsers at the end of the test suite.
- Resource: Provide the path of the resource file that contains the reusable keywords, variables and other settings.
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
Variables used in GenericResources.robot
- ${valid_username}, ${valid_password}, ${invalid_username}, ${invalid_password},
- ${blank_username}: Assign values
- ${url}: The URL to navigate to
- ${browser_name}: Browser will be used to run the tests
- ${output_dir}: Directory where the screenshots will be saved.
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.
4. Run Keyword If Test Failed – Runs the given keyword with the given argument (Capture Page Screenshot), if the test failed.
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 7 – View Screenshots of the failed tests
Screenshots of the failed tests will be saved in screenshots folder with the name of failed test as shown in the below image.

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

Points to Consider
- Path to Screenshots: By default, screenshots are saved in the same directory as the test files. We can customize the path in the Capture Screenshot On Failure keyword. Here, we have used screenshot folder to save the screenshots in it.
- Browser Compatibility: Make sure that we have the correct WebDriver for the browser as well as compatible version of the driver (e.g., chromedriver for Chrome, geckodriver for Firefox, msedgedriver for Edge).
- Failure Scenarios: This setup captures screenshots only when a test fails, making it easier to debug issues.
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Additional Tutorials