A Step-by-Step Robot Framework Tutorial
Himanshu Sheth
Posted On: January 6, 2025
169973 Views
10 Min Read
The Robot Framework is a popular Python-based open-source tool to automate websites and web applications. The combination of the Robot Framework and Selenium can be harnessed for web automation testing.
The SeleniumLibrary in the Robot Framework provides a set of keywords that help in writing high-level readable test cases for interacting with WebElements in the Document Object Model (DOM).
In this Robot Framework tutorial, we deep dive into the nuances of web automation testing with keyword-driven testing with Robot Framework in Python.
TABLE OF CONTENTS
What Is Robot Framework?
For starters, the Robot Framework is an open-source, keyword-driven test automation framework majorly used for Robotic Process Automation (RPA) and acceptance testing.
Akin to the Gherkin syntax, the Robot Framework uses a simple, human-readable syntax, thereby enabling both technical and non-technical users to write effective test cases. Third-party Robot Framework libraries like SeleniumLibrary and AppiumLibrary can be leveraged for web browser testing and mobile app testing, respectively.
Robot Framework Architecture
Robot Framework is technology-independent and has a highly modular architecture. It allows you to create high-level keywords from the existing built-in keywords and test libraries in popular programming languages.
Here’s a high-level overview of its architecture:
- Test Data: The first layer of the Robot Framework is the test data, which is in tabular format. Since it is in this format, maintaining it is very easy. The Robot Framework receives this test data and starts processing it. Upon execution, it generates reports and logs.
- Robot Framework: The primary advantage of the Robot Framework is that it is agnostic of the target under test. The interaction with the layers below the framework can be done using the libraries [built-in or user-created] that make use of application interfaces.
- Test Libraries: The Robot Framework interacts only with the other layers in the system using libraries, which makes the implemented code more portable and maintainable.
- System Under Test: This is the actual target (i.e., website or web app) on which the testing activity is performed. Libraries act as an interface between the Robot Framework Core and the system under test.
The report is in HTML and XML format. The generated reports offer detailed information about every line that is executed as a part of the test case.
Now, let’s look at the prerequisites that we need to follow to run Robot Framework tests.
Prerequisites for Robot Framework Testing
In this Robot Framework tutorial, we will look at some real-life examples to run tests using Robot Framework and Selenium.
1. Install the VS Code IDE (Preferred) or PyCharm Community Edition. However, you need to install the Intellibot Plugin in case you opt for PyCharm Community Edition
To install the IntelliBot Plugin in PyCharm, go to File -> Settings -> Plugins -> Search for Intellibot in the plugins marketplace -> Install the plugin -> Restart the IDE
In our case, we will create the project in the VS Code IDE. For better management of dependencies and environments, it is recommended that we use a virtual environment (venv).
Note: Please replace pip3 with pip depending on the pip version installed on your machine.
2. Run the commands virtualenv venv and source venv/bin/activate on the terminal to create the virtual environment.
3. Now that the virtual environment is ready, let’s install the Robot Framework library by triggering pip3 install robotframework on the terminal.
For our example project, we will add all the required libraries in the requirements.txt file. Run the command robot –version to check the version of the installed Robot Framework. At the time of writing, the latest version of Robot is 7.1.1. However, in this Robot Framework tutorial, we will use the version 5.0.1.
4. Run the command pip3 install robotframework-seleniumlibrary on the terminal for installing the SeleniumLibrary. The latest version of the SeleniumLibrary is 6.6.1. In this Robot Framework tutorial, we will use the version 6.5.0.
To verify the installation of the SeleniumLibrary, run the command pip3 show robotframework-seleniumlibrary.
The .robot script/file contains all the necessary libraries, both internal and external, that the test cases might use. You can also use action chains in Selenium with the keywords of the SeleniumLibrary.
5. Apart from Robot Framework and SeleniumLibrary, we will also install webdriver-manager and robotframework-pabot libraries
It is a good practice to add all the project dependencies and external libraries in the requirements.txt file.
With this, we are all set to run our first Robot test with Selenium and Python!
Getting Started With Robot Framework Tutorial
In this Robot Framework tutorial, we will run four web automation tests using Robot Framework and SeleniumLibrary. Before doing so, we will set up the project and execution environment.
Understanding Project Structure
Test execution with the Robot Framework and SeleniumLibrary will be performed with frameworks installed on local machines and a cloud grid-like LambdaTest. In later sections of this Robot Framework tutorial, we will discuss the integral nuances of cloud execution.
LambdaTest is an AI-powered test execution platform that lets you perform Python automated tests using Robot Framework on an online Selenium Grid of various real browsers and OS environments.
The tests demonstrating the usage of the SeleniumLibrary with Robot Framework are located in the Tests/Common folder.
Let’s do a deep dive into the project structure:
- Resources/PageObject: Consists of three sub-directories mentioned below:
- Common: Contains the file LambdaTestStatus.py that helps in updating test execution status on the LambdaTest Web Automation dashboard.
- KeyDefs/Common.robot: Common.robot houses custom keywords for realizing actions like opening/closing browsers, clicking menu items, and more.
- KeyDefs/SeleniumDrivers.robot: This solution (inspired by this Robot Community Thread) is required only if you are using Selenium (v 4.9 and above).
- Locators/Locators.py: As the name indicates, this file contains all the locators used for locating elements on the document under test. We will touch base on a few of these locators in the further sections of this Robot Framework tutorial.
- Tests/Common: Consists of four .robot files where we have implemented the actual tests
- pyproject.toml: Configuration file for build management purposes
- requirements.txt: File that contains dependencies (or external libraries) to be installed for the project
SeleniumDrivers.robot houses custom keywords for managing WebDrivers for Chrome, Firefox, and Edge browsers. The management is done using the webdriver-manager library.
After creating a virtual environment (venv), run the commands poetry install –no-root and pip3 install -r requirements.txt to install the dependencies required for the project.
Lastly, all the tests that are a part of this Robot Framework project can be executed on the local machine as well as the LambdaTest cloud grid.
- Execution on the local machine – export EXEC_PLATFORM=local
- Execution on the LambdaTest cloud grid – export EXEC_PLATFORM=cloud
When executing tests on the LambdaTest cloud grid, you also need to export environment variables LT_USERNAME and LT_ACCESS_KEY, which can be obtained from the LambdaTest Security page. The combination of LT_USERNAME and LT_ACCESS_KEY are used for authentication when tests are run on LambdaTest.
Defining Variables and Resources
Just like include in the C language, the Robot Framework also provides built-in keywords for defining variables that can be used throughout the test execution. Think of it like defining global variables with extern in the C programming language!
The Resource file provides modularization to the tests using the Robot Framework. Variables, custom (or user-defined) keywords, and paths to external libraries are all a part of the Resource file. The content of the file can be used across tests (or test suites). In our case, we have created custom keywords for opening/closing the web browser (on a local machine and LambdaTest cloud grid).
The Open test browser keyword takes three arguments: TEST_URL, BROWSER, and ltoptions. Since this keyword is used when tests are executed on the LambdaTest cloud grid, the input browser options (or capabilities) are generated with the LambdaTest Automation Capabilities Generator.
As mentioned in the official documentation of SeleniumLibrary, the remote_url argument of the Open browser keyword helps set the remote URL of the grid. Similarly, the options argument helps set the desired options used for test execution.
The Move to Element keyword contains logic for realizing the hover (or move to) operation on the located element/item. The element (i.e., ${menu_item}) is loathed with the partial link explicit locator strategy combined with the Get WebElement method of the SeleniumLibrary.
Once the element is located, the Mouse Over method lets you simulate the hover operation on the element. The Click Menu Item is a user-defined keyword that helps users click on a link located with the partial link locator. We could have used any other locator, but this was the ideal one for the element we were planning to locate the test in!
Once the element is located, the Click Link method of SeleniumLibrary simulates the click action on the link.
Similarly, the SeleniumDrivers.robot file provides user-defined keywords that help in instantiating Chrome/Firefox/Edge browser(s) on the local machine. This solution is a must-have in case you are running Robot tests on Selenium 4.9 (and above), the solution inspiration is from a Robot Framework Forum thread.
The code includes both Common.robot and SeleniumDrivers.robot files using the Resource keyword (under the Settings section).
The Variables keyword in the Robot Framework helps define global variables that can be accessed across test suites. In our case, variables are created (in the Locators.py file) that point to the appropriate locators used for locating elements on the document (or web page).
As seen below, the Variables keyword lets you import/handle the variables defined in the Locators.py file. The Robot Framework has a rich ecosystem of built-in and external libraries. These libraries can be imported with the Library setting in the Settings section.
You can also use the Import Library keyword from the BuiltIn library for importing libraries in the code. Now that variables are defined and libraries are imported, let’s look at some of the scenarios that can be automated with the Robot Framework in the next section of this Robot Framework tutorial.
Locating WebElements
Before handling WebElements in Selenium Python, it is essential to locate them using the appropriate web locator. Similar to web locators in Selenium, the SeleniumLibrary in Robot Framework also supports all popular locators such as ID, XPath, Name, Link Text, Partial Link Text, etc
The locator is mostly provided as a string using the locator syntax. As stated in the official documentation of SeleniumLibrary, the locator can either be specified with a prefix or it can just be implicit. The explicit locator is specified using either locator-strategy:value or strategy=value.
For example, the XPath of an element can be specified as xpath:
Test Scenario:
|
Implementation:
Code Walkthrough:
The test scenario being demonstrated is replicated thrice so that we can use the Robot Framework for parallel test execution. These tests can be executed on three different browser combinations: Chrome, Firefox, and Edge.
To get started, we first use the Resource keyword to include the external resources (e.g., libraries, user-defined keywords, etc.) in the test suite. The Common.robot file contains all the user-defined keywords. Along similar lines, the Variables keyword is used for defining the variables that would be used throughout the tests and test suites.
The value of the environment variable EXEC_PLATFORM is assigned to a local variable that is accessible across the test.
To execute on the LambdaTest cloud grid, we first create a local dictionary variable named lt_options_cloud_1 that consists of browser options (or capabilities) generated with the LambdaTest Automation Capabilities Generator.
Next up, we assign the value of &{lt_options_cloud_1} to the LT:Options key within the &{CAPABILITIES_CLOUD_1} dictionary. Hence, &{lt_options_cloud_1} is nested inside &{CAPABILITIES_CLOUD_1} under the LT:Options key.
The user-defined keyword Test Teardown calls the other user-defined keyword – Close local test browser or Close test browser depending on whether the test is executed on a local machine or LambdaTest cloud grid.
The Test Cases section contains all the test cases (or test scenarios). If the tests are executed on the local machine, the first step is to set the path of the respective browser driver (e.g., ChromeDriver, EdgeDriver, or GeckoDriver). As stated earlier, this step is only necessary if you are using Selenium 4.9 (and above).
Depending on the point of execution (i.e., Robot Framework on the local machine or LambdaTest cloud), the respective browser is instantiated so that automated tests can be performed on the URL under test.
Once the page is loaded, the Page Should Contain Element keyword is used to verify whether the element (located with the Name locator) is present on the page. Then, select the necessary checkboxes and invoke the Click button keyword (of the SeleniumLibrary) on them.
Like SendKeys in Selenium, the Input text keyword of SeleniumLibrary helps in sending text to the text field identified by the locator. Now that the required text (or a new entry in the ToDo list) is entered in the textbox, we now click the add button with the Click Button keyword of the SeleniumLibrary.
Finally, the Should Be Equal As Strings keyword is used to verify whether the new ToDo item (or text) has been added successfully. Now that execution is completed, the Test Teardown user-defined keyword is invoked to clean up resources used during the test.
Test Execution:
To execute tests on LambdaTest, set the environment variable EXEC_PLATFORM to cloud. Then, run the command robot Tests/Common/test_locate_elements.robot on the terminal to execute the ToDo tests.
Shown below is the execution snapshot from the LambdaTest Web Automation dashboard. All three tests are executed successfully.
Handling Keyboard Actions
When handling keyboard actions in Selenium WebDriver, entering text in text boxes, simulating key presses, and performing other frequently used actions are common actions. Using appropriate keywords from the SeleniumLibrary, you can also navigate through different WebElements on the page.
The Press Keys keyword simulates key presses (or a combination of presses) on the WebElement or on the active browser. It can also be used to press special keys like RETURN, ALT, ARROW_DOWN, ARROW_UP, etc. or a combination of these normal and special keys.
Like SendKeys in Selenium, the Input Text keyword of SeleniumLibrary lets you enter text in the text field located with the appropriate locator. Similarly, the Input Text Into Alert keyword helps in entering text in the alert box once the context is switched to the alert box (or window).
Test Scenario:
|
Implementation:
Code Walkthrough:
Navigate to the LambdaTest Selenium Playground website and locate the element Input Form Submit with the XPath locator. Since maximizing the browser window is one of the best practices of Selenium, we first maximize the browser window with the Maximize Browser Window keyword.
Once the element is located, simulate the click operation by invoking the Click Link keyword on the located element. Since we are entering text in the text box, there is an option to use the Input text keyword with the element locator and the text to be entered in it.
Similarly, the Press Keys keyword with the element locator and text/key combination can be used to enter text. Here, we are entering the text Himanshu and Sheth with a space between the words. The Select From List By Value keyword is used to select the appropriate option (e.g., US) from the drop-down list located with the Name locator.
Click on the Submit button (located with the XPath locator) by invoking the Click Button keyword of the SeleniumLibrary. The JavaScript method window.scroll(0, 0) scrolls to the top of the page. The Execute JavaScript keyword helps in executing the JS code supplied to it as an argument.
Finally, the test is considered as passed if the web page contains the text Thanks for contacting us, we will get back to you shortly.
Test Execution:
To execute tests on LambdaTest, set the environment variable EXEC_PLATFORM to cloud. Then, run the command robot Tests/Common/test_keyboard_actions.robot on the terminal to execute the keyboard interaction tests.
Shown below is the execution snapshot from the LambdaTest Web Automation dashboard. Both the tests are executed successfully.
The same set of tests can be run using the Robot Framework installed on the local machine. All you need to do is set the environment variable EXEC_PLATFORM to local for executing tests on your machine.
Handling Mouse Actions
For handling mouse actions in Selenium WebDriver, the Robot Framework offers flexibility to simulate mouse interactions in conjunction with SeleniumLibrary. It provides keywords that let you perform mouse actions like mouse click, mouse down, mouse over, drag and drop, etc. on the respective elements on the page.
A simple example of mouse action is moving over a WebElement and clicking on the respective element. In this section of the Robot Framework tutorial, we look at how to perform mouse actions like drag and drop and click on a sub-menu item using a combination of keywords provided by SeleniumLibrary.
Test Scenario 1 (Drag and Drop):
|
Test Scenario 2 (Mouse Over Operations):
|
Implementation:
Code Walkthrough (Drag and Drop):
Once the URL under test is loaded, the Wait Until Element Is Visible keyword is invoked to check if the two draggable and one droppable item are visible in the DOM.
Once we have checked that the elements are visible, the Get WebElement keyword is used for locating those elements with the XPath locator.
Now that the elements are located, we can drag them to the target location with the Drag And Drop keyword of SeleniumLibrary.
Code Walkthrough (Mouse Over Operations):
Once the LambdaTest eCommerce Playground is loaded, we first move to the element (or perform a mouse over) to the target menu. As seen in the implementation of the user-defined Move to Element keyword, the Menu element is located with the Get WebElement keyword and Partial Link Text locator.
Once the element is located, the Mouse Over keyword is invoked to simulate the hover operation on the located element.
Once the menu opens up, we first locate the sub-menu item in the located menu using the Partial Link locator. The Click Link keyword helps in clicking on the link/sub-menu item.
Finally, get the title of the opened page with the Get Title keyword. The Should Be Equal As Strings keyword of the BuiltIn library marks the result as Failed if the two strings are not equal.
Test Execution:
Set the environment variable EXEC_PLATFORM to cloud for executing tests on LambdaTest.
Run the command robot Tests/Common/test_mouse_actions.robot on the terminal to execute the mouse action tests.
Shown below is the execution snapshot from the LambdaTest Web Automation dashboard. Both the tests are executed successfully.
Scrolling Web Page
When interacting with elements on dynamic web pages, it is important to check if the element is present in the DOM and visible in the viewport. To do this, we first scroll the page (a certain number of times) and then use the Scroll Element Into View keyword, which further scrolls until the specified element is visible within the browser’s viewport.
Interacting with elements on infinite-scrolling websites (e.g., eCommerce) can become challenging because the viewport does not provide much information about the element’s availability. Here, we need to scroll to the bottom of the page and then interact with the element once it is in the viewport.
Test Scenario:
|
Implementation:
Code Walkthrough:
Navigate to the Scraping Infinite Scrolling page and maximize the browser window. Since the content is loaded dynamically (i.e., on page scroll), scroll till the end of the page is performed using the combination of the window.scroll() method with document.documentElement.scrollHeight as the input argument.
We create two user-defined keywords – Get Window Height and Scroll Down.
Get Window Height
The JavaScript method document.body.scrollHeight returns the height of the entire document. Once the height is calculated, its value is returned with the RETURN keyword.
Scroll Down
As the name suggests, the window.scroll() method in JavaScript performs a vertical scroll until it reaches the end of the page (i.e., document height calculated with document.body.scrollHeight).
For scrolling till the end of the page, a while loop is executed where the exit happens when there is no more room to scroll the page. First, the current window height (after maximizing the window) is calculated with Get Window Height and then assigned to a variable named ${start_height}.
After a single scroll down (via Scroll Down keyword) operation, the window height is again calculated (i.e., ${scroll_height}). The same cycle is repeated till the current window height is the same as ${scroll_height}.
Now that we have reached the end of the page, we check to see if the desired element locator (e.g., XPath) is present.
As discussed earlier, the Scroll Element Into View keyword of SeleniumLibrary is invoked for scrolling to the element identified by the locator (i.e. XPath) into the view. Now, the located element is clicked with the Click Element keyword.
The JavaScript expression return document.readyState == “complete” checks if the web page (after the click operation) has loaded completely or not. The Wait For Condition keyword is invoked to check if the page load is complete. Post this, we mark the test as Passed.
Test Execution:
Set the environment variable EXEC_PLATFORM to cloud for executing tests on LambdaTest.
Run the command robot Tests/Common/test_infinite_scroll.robot on the terminal to execute the test that does vertical scrolling with the Robot Framework.
Shown below is the execution snapshot from the LambdaTest Web Automation dashboard. Both the tests are executed successfully.
Best Practices for Using Robot Framework
When writing tests with Robot Framework and SeleniumLibrary, it is important to follow the best implementation practices to reduce test maintenance and accelerate test execution.
In this Robot Framework tutorial, let’s look at some of the best practices for building scalable, reliable, and maintainable Robot Framework tests:
Suite Setup and Suite TearDown
As stated in Robot’s official documentation, test cases and test suites can have setup and teardown. In simple terms, Suite Setup defines the keyword(s) that are executed before tests in a suite are executed. On the other hand, Suite Teardown defines the keyword(s) that are executed after test cases in a suite have completed execution.
At an abstract level, Suite Setup and Suite Teardown keywords do the same job that is normally performed by fixtures in pytest. The usage of these keywords improves the maintainability of the code. They are particularly useful if the same browser instance has to be shared across multiple test cases within the same suite.
Self-Explanatory Naming Convention
Whether it is about naming variables/test suites/test cases/resource files, etc, it is very important to maintain consistency in the naming convention. The names that you select should be self-explanatory, and they should focus on what the file is all about. The name must be descriptive and self-explanatory so that it automatically provides more information about the nitty-gritty of the respective test.
Avoid Dependency of Tests
Like one of the Selenium best practices, it is recommended to avoid test case dependencies when writing Selenium tests with Robot Framework. In case Test Case A is dependent on Test Case B, any logic change in Test Case B might have some unintentional impact on Test Case A. Hence, it is best to treat each test case as an independent entity.
Avoid Hard-Coded Variables
We all know that hard-coded variables are a complete no-no in the case when implementing tests. You can either create a separate Python file that only contains the variables as we did in the demonstration section of this Robot Framework tutorial.
Alternatively, you can also pass the variable values from the command line using the –variable option.
Leverage Data-Driven Testing
Workflow tests and data-driven tests are two broad categories of test cases that you would ideally realize with the Robot Framework. A workflow test generally has a predefined structure – precondition, action, verification, and cleanup. Mostly, there is less usage of complex test logic in such tests.
Also, keywords describe the intent of the tests. The Valid Login test case that we discussed earlier is a Workflow test where test cases were designed using a keyword-driven style.
On the other hand, data-driven testing with the Robot Framework lets you run a test case against different input arguments. As stated in the official documentation of the Robot Framework, Test Templates in Robot Framework help in converting keyword-driven tests to impactful data-driven tests.
Template keywords have the flexibility to accept both normal positional and named arguments, as well as arguments embedded in the keyword name.
Leverage Libraries and User-Defined Keywords
As mentioned in the earlier sections of this Robot Framework tutorial, it is possible to create user keywords using lower-level keywords. These keywords offer a much-needed layer of abstraction, making them more modular and reusable. User-defined keywords can also contain some programming logic.
It is recommended to create a user library that can perform those custom tasks if the logic involves too many computations.
Skip Tests Under Conditions
Skipping tests is handy when you want to prioritize the execution of certain tests (e.g., only flaky tests). You can also use the skip functionality when the tests are still in the development phase.
Skipping resource-intensive tests or tests dependent on external environments/configurations. In summary, you can skip tests when you want them not to be executed under specific conditions or during a particular run.
Use Wait Until Keyword for Better Synchronization
Like waits in Selenium, the SeleniumLibrary provides keywords like Sleep that mimic the functionality. Since blocking sleep is not a good practice, it is recommended to use waits in a certain condition.
SeleniumLibrary offers keywords preceding Wait Until Element, Wait Until Page and Wait Until Location for realizing conditional waits in the Robot Framework. For example, the Wait Until Element Is Not Visible keyword waits until the element locator is not visible. On similar lines, Wait Until Page Contains waits until a certain text appears on the document/page.
It is recommended that you leverage the Wait Until keyword to automate Robot Framework tests on pages with dynamic content.
Lastly, suite initialization files can also be used to set up test suites before any test cases within those suites are executed.
Wrapping Up!
Thanks for making this far! Robot Framework is a powerful keyword-driven testing framework. Using keywords from the SeleniumLibrary, developers and testers can come up with test suites and test cases that can be easily maintained.
Though you can make use of the Robot Framework for testing on different browsers, using an online Selenium Grid approach can be more economical and scalable.
In this detailed Robot Framework tutorial, we covered the essentials of Robot Framework and provided hands-on examples. If you have any questions (or suggestions), please comment below.
Happy Testing!
Frequently Asked Questions (FAQs)
Is Robot Framework easy to learn?
Yes, Robot Framework is easy to learn due to its keyword-driven approach and simple syntax. It is suitable for beginners and supports multiple libraries for various tasks.
What is Robot Framework used for?
Robot Framework is used for automated testing of websites and mobile applications. It supports web, API, and mobile testing across various platforms.
How to build a Robot Framework?
To build a Robot Framework project, install it using Python’s pip, create test cases in .robot files, and execute them using the robot command. You can also integrate libraries for extended functionality.
Which tool is used for Robot Framework?
Which tool is used for Robot Framework?
Robot Framework is run using its CLI tool, often combined with editors like RIDE, PyCharm, or VS Code. Libraries like SeleniumLibrary are used for web testing.
Citations
- Robot Framework: https://robotframework.org/
Got Questions? Drop them on LambdaTest Community. Visit now