Robot Framework is a cost-effective, time-saving way for companies to implement automated testing. This open-source, low-code framework makes it easier for testers and developers to write test scripts. This article will show you the basics of Robot Framework.
Computer with macOS, Windows, Linux, or Unix.
Python 3.5+
IDE such as RIDE, VS Code, or PyCharm
Robot Framework
Selenium WebDriver for the browser (Chrome, Edge, Firefox, IE, Opera, or Safari), you are testing
A typical Robot Framework project has the following file structure:
Figure 1 – Example Robot Framework project file structure.
Library Folder - contains custom keyword libraries.
Resources Folder – contains the reusable Robot code files.
Results Folder – contains the executed test results.
Tests Folder – contains the Robot tests.
Folder and file names are case-insensitive. They should be descriptive and not too long, and use hyphens to separate words in the file name. I prefer to capitalize each word in the file and folder name to improve the readability of the test execution result file.
Robot Framework test scripts are text files with the *.robot or *.txt extensions. The four sections of basic Robot test scripts are setting, variables, keywords, and test cases.
The settings section contains the import statements for the external libraries, resources, and the setup and teardown commands.
The variables section contains keyword arguments, global, and local values. Variables make it easy to change the test data in one location.
The keywords section contains operations used to execute the tests. The different types of keywords are built-in keywords, library keywords, and user-defined keywords. Built-in and library keywords are lower-level keywords defined by the built-in Robot Framework library or an external library such as Selenium. User-defined keywords are keywords created by combining library keywords.
The test cases section contains the test cases. Two of the more common approaches to writing test cases are keyword-driven and Gherkin.
Keyword-driven Approach
The keyword-driven approach uses the Robot Framework built-in keywords and keywords from an external library like Selenium.
Figure 2 – Test case using Keyword-driven approach
Gherkin Approach
The Gherkin approach uses Gherkin syntax. Gherkin is a business readable language used in behavior-driven development (BDD).
Robot Framework is case-insensitive. The keywords Close Browser and close browser are the same command. Test scripts must contain the settings section. In simple test scripts, the keyword section is omitted and combined with the test cases section.
Tags are a way of classifying test cases. Tags in the test case section are in brackets. Commonly used tags are:
[Arguments] - Specify numeric or string arguments {my_argument}
Documentation - A description of the test
[Tags] - Type of test – Smoke
Examples are an excellent way to demonstrate how to write test scripts. There are several websites available to practice automated testing. In this example, I will use the live T-Mobile website. Why am I using a live website? The current automated testing practice websites do not have a cookie notification. New privacy laws require users to accept cookies. Most of the how-to articles on cookie notifications are complicated and often do not work. I want to show a simple way to accept or decline cookies without creating a custom library.
Create a test script to test the search feature on the T-Mobile website. The test will:
Open a Chrome browser and go to https://www.t-mobile.com,
Maximize the browser window,
Accept the cookie notification,
Scroll down to a specific section of the website (step added to show that the cookie notification was accepted),
Verify the cookie notification is no longer visible,
Enter the search term,
Verify the search results are visible,
Close the Chrome browser.
The test script uses the keyword-driven approach.
The test script contains the settings, variables, and test cases sections. The section names format is *** Section name ***. There must be one space between the section name and the asterisks.
Settings section
You must specify a keyword library to use for the project. This project uses the Selenium library. The syntax for specifying a library is:
Library Library Name
Add a short description of the test script using the documentation tag.
Documentation Short Description
Variables section
There are three variables. Variable names start with a ‘$’ and are in brackets.
Test cases section
Give the test case a name. The test case name in this example is Display iPhone Search Results.
Keywords
Open Browser – open the browser using the URL variable.
Maximize Browser Windows.
Click Element- use the element CSS selector to accept the cookie. Using an XPath or id selector generates an ‘element not found’ error.
Scroll Element to View - use the element XPath, id, or CSS selector.
Element Should Not Be Visible - use the element XPath, id, or CSS selector.
Click Element - use the element XPath, id, or CSS selector.
Input Text – using the search term variable.
Press Keys- use the word None and Enter.
Wait Until Page Contains – a string that appears when the page is fully loaded.
Page Should Contain – a string that appears on the search results page.
Close Browser – close Chrome browser.
The finished test script:
Figure 4 – Example test script
The test results log:
Figure 5 – Example results log
This article explored the basic concepts of Robot Framework. It is an efficient tool for creating automated tests for websites using low or no-code. You can view the example source code here: – https://github.com/VanitaCW/Robot-Framework-concepts-examples/tree/main/ExampleRobotTests.
Click here to view a video demonstration of the example test script.
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html
https://robotframework.org/robotframework/latest/libraries/BuiltIn.html
https://pixabay.com/photos/success-stairs-board-drawing-4168389/