Using Robot Framework: Beginner’s Tutorial
By Pawan Kumar, Community Contributor - November 8, 2024
Robot Framework is an open-source 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. It uses a keyword-driven testing approach where testers can easily create test cases in tabular syntax. This guide will provide a comprehensive understanding of the Robot Framework, its basic concepts, syntax, and how it can be used for automated testing. You can follow this guide as a robot framework tutorial.
- What is Robot Framework
- Different Types of testing supported by Robot Framework
- Basic Features of Robot Framework
- How to Use Robot Framework [with Example]
- Quick Robot Framework Cheatsheet
- Robot Framework vs Selenium: Which one is better?
- Benefits and Challenges of Robot Framework
- Best Practices for Robot Framework
- Frequently Asked Questions
What is Robot Framework
Robot Framework is a generic open-source test automation framework that provides an easy-to-use, keyword-driven approach to automation. It is written in Python and allows users to create high-level test cases that can be easily translated into machine-executable automation scripts. The framework has a rich set of libraries and tools that allow it to be used for various types of testing, including acceptance testing, functional testing, and unit testing.
One of the key features of the Robot Automation Framework is its keyword-driven approach, which allows testers to write tests using natural language syntax and easily reusable keywords. This makes it easy for both technical and non-technical users to write and maintain tests, as well as to collaborate on testing efforts. Robot Framework also has a modular architecture, which allows it to be extended with additional libraries and tools. These libraries can be used to automate various tasks, such as interacting with databases, testing web applications, or automating mobile applications.
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.
Overall, Robot Test Framework is a powerful and flexible tool for test automation, providing users with an easy-to-use, keyword-driven approach that can be extended with additional libraries and tools. Its modular architecture and built-in libraries make it a popular choice for testing a wide variety of applications and systems.
Read More: Robot Framework and Selenium Test Automation
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.
Read More: Top Testing Libraries for React
*** 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.
Robot Framework is a powerful and flexible Python robot framework that uses a keyword-driven approach to create test cases. By using keywords, variables, test data, assertions, and libraries, testers can create detailed and complex test cases that can be easily executed and analyzed. Robot Framework provides a range of built-in libraries and tools that can be used to automate testing in a variety of scenarios.
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.
*** Settings *** Library SeleniumLibrary Suite Setup Open Browser https://www.browserstack.com/automate chrome Suite Teardown Close Browser *** Test Cases *** Positive Login Test [Documentation] Test the positive login functionality [Tags] login Maximize Browser Window click link xpath=//nav/ul/li[5]/a[@title = 'Sign In'] Input Text id=user_email_login ****@gmail.com Input Password id=user_password ******** click element id=user_submit Wait Until Page Contains class=text-uppercase Negative Login Test - Incorrect Password [Documentation] Test the negative login functionality with incorrect password [Tags] login Maximize Browser Window click link xpath=//nav/ul/li[5]/a[@title = 'Sign In'] Input Text id=user_email_login *****@gmail.com Input Password id=user_password **** click element id=user_submit Wait Until Page Contains Invalid password Negative Login Test - Incorrect Email [Documentation] Test the negative login functionality with incorrect email [Tags] login Maximize Browser Window click link xpath=//nav/ul/li[5]/a[@title = 'Sign In'] Input Text id=user_email_login *******.com Input Password id=user_password ******* click element id=user_submit Wait Until Page Contains Invalide email format.
In this example, we have taken the BrowserStack website login page. We first import the SeleniumLibrary library and define a Suite Setup and Suite Teardown to open and close the browser respectively.
Next, we define three test cases: “Positive Login Test“, “Negative Login Test – Incorrect Password“, and “Negative Login Test – Incorrect Email“.
In each test case, we first define some documentation for the test case using the [Documentation] tag. We then define some tags for the test case using the [Tags] tag.
Next, we use the Input Text keyword to input an email and password into the respective fields and use the Click Button keyword to click the ”Sign in me” button.
Finally, we use the Wait Until Page Contains keyword to wait until the expected text appears on the page. In the positive login case, we wait until the user profile link appears. In the negative login cases, we wait until the error message appears.
To run this test case, you can save the code in a file called “test.robot” and execute it using the command robot test.robot. This will execute all of the test cases in the file and display the results in the terminal.
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.
Also Read: How to use for loops 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.
Here are the main differences between Robot Framework and Selenium.
Robot Framework | Selenium |
---|---|
Robot Framework is a generic open-source test automation framework that provides an easy-to-use, keyword-driven approach to automation | Selenium is a widely used open-source test automation tool that provides a set of APIs for browser automation |
Does not require programming knowledge | Requires programming knowledge to create and execute test cases |
Robot Framework supports programming languages such as Python, Java, and .NET | Selenium supports programming languages such as Java, Python, and C# |
Robot Framework Selenium offers a wider range of features such as loops, exception handling, and variable assignments. Robot Framework also offers a wide range of built-in libraries for various types of testing such as SeleniumLibrary for web testing, DatabaseLibrary for database testing, and XML Library for XML testing. | Selenium provides a rich set of APIs for automating web browsers |
Robot Framework is more user-friendly than Selenium due to its keyword-driven approach and tabular syntax. | Selenium provides more fine-grained control over web browsers and is more suitable for advanced users. |
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.