Automation using Robot Framework

Abhijit Paul
11 min readDec 30, 2024

--

Robot Framework is generic open-source free-to-use test automation framework that uses keyword-driven testing and allows easy-to-use tabular syntax to create test cases. It supports different testing approaches such as acceptance, integration, and unit testing.

In addition to its modular architecture, Robot Framework also has a rich set of built-in libraries, including libraries for testing HTTP, FTP, SSH, and XML, as well as libraries for testing user interfaces and databases.

Different Types of testing supported by Robot Framework

Robot Framework is used when there is a need for test automation in a software development process. It is particularly useful in projects that require continuous integration and delivery, as it supports different types of testing and can be easily integrated with other tools such as Jenkins and Git.

Robot Test Framework is a versatile test automation framework that can be used in a variety of scenarios. Some common use cases for the Robot Framework include:

  • Acceptance Testing: Robot Automation Framework is often used for acceptance testing, which involves testing whether a software system meets the specified requirements. This type of testing can be done at various stages of the software development lifecycle, from early prototypes to final releases.
  • Regression Testing: Regression testing is used to ensure that software changes or updates do not introduce new bugs or issues. Robot Framework is a popular choice for regression testing because of its modular architecture and ability to easily reuse test cases.
  • Functional Testing: Python Robot Framework can be used for functional testing, which involves testing the functionality of a software system. This can include testing individual functions or components, as well as testing the system as a whole.
  • Integration Testing: Integration testing involves testing how different components of a software system work together. The Robot Framework can be used to automate this type of testing, allowing testers to easily test the interactions between different components.
  • Continuous Integration/Continuous Delivery (CI/CD): It can be integrated with CI/CD pipelines to automate testing as part of the software development process. This allows teams to catch issues early in the development cycle and ensure that new changes do not introduce new bugs or issues.
  • API Testing: Robot Framework has built-in libraries for testing APIs, making it a popular choice for testing RESTful and SOAP web services.

Overall, Robot Framework is a flexible and versatile tool that can be used in a variety of testing scenarios. Its modular architecture and rich set of libraries make it a popular choice for automating various types of testing, from acceptance testing to API testing.

Basic Features of Robot Framework

Robot Framework is a popular test automation framework that uses a keyword-driven approach to create test cases. The framework is built in Python and can be used for both web-based and desktop-based applications. Here are some basic concepts of Robot Framework:

  • Keywords: Keywords are the building blocks of Robot Framework test cases. They represent a single action or step that the test case will take. Keywords can be either user-defined or built-in. User-defined keywords are custom functions created by the tester, while built-in keywords are pre-defined functions that are included in the Robot Framework library.
*** Keywords ***
My Keyword
Log This is my first keyword
  • Test Cases: Test cases are a collection of keywords that represent a specific test scenario. Test cases can be organized into test suites, which are collections of related test cases.
*** Test Cases ***
My Test Case
  • Variables: Variables are used to store data that can be used throughout the test case. Variables can be assigned a value using the keyword “Set Variable“, and their value can be retrieved using the keyword “Get Variable Value“.
*** Variables ***
${MY_VARIABLE} Hello World
  • Test Data: Test data is the input that is used in a test case. Test data can be stored in a separate file, such as a CSV or Excel file, and then accessed using the “Data-Driven Testing” approach in Robot Framework. In the below example, the “Get File” keyword is used to read the test data from the “login_data.csv” file and store it in a variable called “${data}”. The “FOR” loop is then used to iterate over each row of the test data and set the “${username}” and “${password}” variables to the values from the CSV file. Finally, the “Login” keyword is used to perform the login functionality with the specified username and password.
*** Keywords ***
Login With Credentials From CSV
[Arguments] ${filename}
${data}= Get File ${filename}
: FOR ${row} IN @{data}
${username}= Set Variable ${row['username']}
${password}= Set Variable ${row['password']}
Login ${username} ${password}
  • Assertions: Assertions are used to validate the expected output of a test case. Robot Framework provides a wide range of built-in assertions, such as “Should Be True“, “Should Be False“, “Should Be Equal“, and “Should Not Be Equal“.
*** Keywords ***
Login With Credentials From CSV
[Arguments] ${filename}
${data}= Get File ${filename}
FOR ${row} IN @{data}
${username}= Set Variable ${row['username']}
${password}= Set Variable ${row['password']}
Login ${username} ${password}
Should Be Equal verify HomePage #assertion for Homepage
END
  • Libraries: Libraries are a collection of reusable code that can be used in Robot Framework. Libraries can be either built-in or user-defined. Built-in libraries are included in the Robot Framework library, while user-defined libraries can be created by the tester. Adding a library to Robot Framework is a straightforward process. To add a library, you need to download the library and save it to a directory on your computer. You can then add the directory path to the Robot Framework settings using the Library keyword.
*** Settings ***
Library SeleniumLibrary
  • Tags: Tags are used to categorize test cases and make it easier to run specific tests. Tags are defined using the keyword “Tags” followed by the tag name.
*** Test Cases ***
My Test Case
Tags Smoke Test
  • Test Execution: Test execution is the process of running the test cases. Robot Framework provides multiple ways to execute tests, such as running individual test cases, running test suites, or running all test cases in a directory.
  • Exception catching: Exception catching is used to handle errors and exceptions that may occur during the test execution. Robot Framework provides a built-in keyword named “Run Keyword And Expect Error” that can be used to catch exceptions. This keyword executes a given keyword and expects it to fail with a specific error message. If the keyword does not fail or fails with a different error message, the test case will fail.Suppose you have a login functionality that raises an exception if the username or password is incorrect. You can use the “Run Keyword And Expect Error” keyword to catch the exception and handle it in a custom way. Here’s an example of how to use this keyword to catch an exception in a login test case:
*** Test Cases ***
Login Test
[Documentation] Test the login functionality
: TRY
Login john.doe password
Log Login successful
: EXCEPT AssertionError
Log Login failed: username or password is incorrect
  • Reports and Logs: Robot Framework provides detailed reports and logs that can be used to analyze the test results. The reports include information such as the test case status, execution time, and error messages.

How to Use Robot Framework [with Example]

First, you’ll need to install Robot Framework on your machine. We can do this using the pip package manager by running the command pip install robotframework in your terminal.

Once we have installed Robot Framework, we can create a new test case by creating a new text file with a .robot extension. This file should contain the test cases, test suites, and keywords that you want to run.

# FileName: Resources.robot

*** Settings ***
Library SeleniumLibrary timeout=10 implicit_wait=2

*** Variables ***
${LOGIN_URL} https://practicetestautomation.com/practice-test-login
${BROWSER} headlesschrome

*** Keywords ***
Open My Browser
[Documentation] This keyword opens browser and maximizes it
Open Browser ${LOGIN_URL} ${BROWSER}
Maximize Browser Window

Close Browsers
[Documentation] This keyword closes all open browser
Close All Browsers

Open Login Page
[Documentation] This keyword navigates to login page
Go To ${LOGIN_URL}

Input Username
[Arguments] ${username}
Input Text id=username ${username}

Input Passkey
[Arguments] ${password}
Input Text id=password ${password}

Click Login Button
Click Button id=submit

Click Logout Link
Click Link xpath=//*[text()='Log out']

Error Message Is Visible
Wait Until Page Contains Your username is invalid!

Dashboard Page Should Be Visible
Wait Until Page Contains Logged In Successfully
# FileName: Testcases.robot

*** Settings ***
Library SeleniumLibrary timeout=1 implicit_wait=2
Resource resources.robot
Suite Setup Open My Browser
Suite Teardown Close Browsers
Test Template Invalid Login

*** Test Cases ***
Valid Username Empty Password student ${EMPTY}
[Documentation] Test the login functionality with combination of valid username and empty password

Valid Username Invalid Password student xyz
[Documentation] Test the login functionality with combination of valid username and invalid password

Invalid Username Valid Password student1 Password123
[Documentation] Test the login functionality with combination of invalid username and valid password

Invalid Username Empty Password student1 ${EMPTY}
[Documentation] Test the login functionality with combination of invalid username and empty password

Invalid Username Invalid Password student1 xyz
[Documentation] Test the login functionality with combination of invalid username and invalid password

*** Keywords ***
Invalid Login
[Documentation] This keyword attempts to verify login functionality with combination of valid and invalid credentials
[Arguments] ${username} ${password}
TRY
Input Username ${username}
Input Passkey ${password}
Click Login Button
Log To Console Login unsuccessful
Error Message Is Visible
EXCEPT AssertionError
Log To Console Login failed: username or password is incorrect
FINALLY
Log To Console Finally block is getting executed
END

In this example, we have taken the practicetestautomation.com website page. We first import the SeleniumLibrary library and define a Suite Setup and Suite Teardown to open and close the browser respectively and named the Test Template.

Quick Robot Framework Cheatsheet

Here’s a quick Robot Framework cheatsheet for you that will help you gain more understanding of the test automation framework.

  • Test Case Structure in Robot Framework
*** Test Cases ***
Test Case Name
[Documentation] Test case description
[Tags] tag1 tag2
[Setup] keyword1
[Teardown] keyword2
Step 1 keyword3 arg1 arg2
Step 2 keyword4 arg3
...
  • Keywords
*** Keywords ***
Keyword Name
[Documentation] Keyword description
[Arguments] ${arg1} ${arg2}
[Tags] tag1 tag2
${variable} = keyword1 ${arg1} ${arg2}
keyword2 ${variable}
keyword3
...
  • Variables
*** Variables ***
${variable1} value1
${variable2} value2
...
  • Imports
*** Settings ***
Library library_name
Resource resource_file.robot
Variables variable_file.py
...
  • Comments
# This is a comment
*** Test Cases ***
Test Case Name
# This is a comment in a test case
Step 1 keyword1 # This is a comment after a keyword
...
  • Assertions
# This is a comment
*** Test Cases ***
Test Case Name
# This is a comment in a test case
Step 1 keyword1 # This is a comment after a keyword
...
  • Control Flow
IF ${condition}
keyword1
ELSE IF ${condition2}
keyword2
ELSE
keyword3
END
  • Loops
FOR ${variable} IN @{list}
keyword1 ${variable}
END
FOR ${index} IN RANGE ${start} ${end} ${step}
keyword1 ${index}
END
  • Exceptions
Try
keyword1
Catch Exception ${error}
keyword2
Finally
keyword3
END

This is just a brief overview of some of the most commonly used elements in Robot Framework, which could be useful in creating test cases in Robot Framework.

Robot Framework vs Selenium: Which one is better?

The choice between Robot Framework and Selenium depends on the specific needs of the testing project. If the project requires a simple and user-friendly test automation framework, Robot Framework is a better choice. If the project requires more fine-grained control over web browsers and a richer set of APIs, Selenium is a better choice.

Benefits and Challenges of Robot Framework

Here are some of the core benefits of using Robot Framework for automation testing:

  • Easy to use: Robot Framework uses a keyword-driven approach that is easy to read and write. It does not require programming knowledge, making it accessible to non-programmers.
  • Extensible: Robot Framework can be extended using custom libraries for specific testing needs. It also supports various programming languages and integrates with various tools for continuous integration and delivery.
  • Rich set of features: Robot Framework offers a wide range of built-in libraries for various types of testing such as SeleniumLibrary for web testing, DatabaseLibrary for database testing, and XMLLibrary for XML testing. It also offers features such as loops, exception handling, and variable assignments for creating complex test cases.
  • Cross-platform: Robot Framework can be used on various operating systems such as Windows, macOS, and Linux.

However, Robot Framework also has some challenges, including

  • Limited support for mobile testing: Robot Framework does not provide built-in support for mobile testing. However, it can be extended using third-party tools such as AppiumLibrary for mobile testing.
  • The steep learning curve for advanced features: While Robot Framework is easy to use for basic testing, it may require programming knowledge for advanced features such as custom libraries and integration with third-party tools.

Best Practices for Robot Framework

To make the most out of Robot Framework, it is recommended to follow some best practices, including:

  • Follow a modular approach: Break down your test cases into smaller, reusable modules that can be easily maintained and updated.
  • Use meaningful names: Use meaningful names for test cases, keywords, and variables that accurately describe their purpose.
  • Keep test data separate: Store test data in separate files such as CSV or JSON files. This makes it easier to maintain and update test data.
  • Use version control: Use a version control system such as Git to track changes to your test cases and ensure that everyone is working on the latest version.
  • Use descriptive and clear documentation: Document your test cases and keywords with clear descriptions and examples to make it easier for others to understand and maintain them.
  • Keep your test cases simple: Write test cases that are simple, focused, and easy to understand. This makes it easier to troubleshoot issues and maintain the test suite.
  • Use assertions: Use assertions to verify expected results and ensure that the application is working as expected.
  • Use tags: Use tags to group test cases and run them selectively based on their tags. This makes it easier to run specific subsets of tests based on their requirements.
  • Regularly review and refactor your test suite: Regularly review and refactor your test suite to remove redundant or outdated test cases and ensure that it remains maintainable and up-to-date.
  • Run tests on Real Devices: To get more accurate test results, it is important to test under real user conditions. BrowserStack allows you to run tests on 3000+ real devices and browsers for a comprehensive and accurate testing. It provides features like geolocation testing, network simulation, etc.
  • Run tests in parallel: Run tests in parallel to save time and speed up the test execution process. This can be achieved using tools such as Selenium Grid or Robot Framework’s own Parallel Execution feature. And, also we can use BrowserStack for parallel execution of our test cases.

Conclusion

Robot Framework is a powerful and versatile test automation framework that offers a wide range of features and benefits. It is easy to use, extensible and supports various types of testing such as web testing, database testing, and XML testing. While it may have some challenges such as limited support for mobile testing and a steep learning curve for advanced features, these can be overcome with the use of third-party tools and best practices. By following best practices such as a modular approach, meaningful names, and documentation, you can make the most out of Robot Framework and create a maintainable and efficient test suite.

Frequently Asked Questions

1. Why isn’t Robot Framework used more often for test automation?

The robot framework is not often used for test automation due to two main reasons.

  • Limitations in handling complex programming logic
  • Dependence on keyword-driven testing

The above constraints make Robot framework less flexible for complex test cases.

2. Is the Robot Framework free of cost?

Yes. Robot framework is open-source and free to use.

--

--

Abhijit Paul
Abhijit Paul

Written by Abhijit Paul

Abhijit is a Lead Python Automation Developer and a ML and DL practitioner. Reach out to me at https://abhijit-ai.netlify.app & https://tr.ee/41j5_j2oXS

No responses yet