The structure of a Robot file
A Robot file is made of sections. Each section owns one part of the test file. The basic sections are:
- Settings: configuration such as libraries, Setup and Teardown.
- Variables: variables shared by the test cases.
- Test Cases: the test cases themselves.
- Keywords: reusable actions or functions. Test cases can call these keywords, and keywords can call one another.
Settings
Settings contains configuration for the test case.
- Library declares the libraries to use. Here I use SeleniumLibrary. Without it you can still use Robot Framework's BuiltIn keywords, but not the keywords provided by that library.
*** Settings ***
Library SeleniumLibrary
For API testing, you could also import RequestsLibrary:
*** Settings ***
Library SeleniumLibrary
Library RequestsLibrary
For readable syntax, press Tab after typing Library; the editor inserts four spaces between Library and the library name. It is similar to Python's four-space indentation convention.
- Resource and Variables declare the resource and variable files used by the test. A Resource contains reusable keywords, while Variables contains values shared by the test cases.
*** Settings ***
Library SeleniumLibrary
Resource resource.robot
Variables variables.robot
Variables variables_2.py
Files imported as Resources must also be Robot files. Test cases can call keywords from them. I will cover the exact contents of a Resource in the next article; compared with a normal test file, some sections are not allowed.
For Variables, you can use a Robot file, a Python file or nothing at all. Values declared there are available throughout the test cases. The details, including the special rules for Python files, will come later.
- Test Setup and Test Teardown are keywords run before and after a test case. They can be Robot Framework BuiltIn keywords or library keywords.
*** Settings ***
Library SeleniumLibrary
Test Setup Open Browser https://google.com chrome
Test Teardown Close Browser
You can also use Suite Setup and Suite Teardown. They work similarly, but have a different scope; I will discuss their use and meaning in a later article.
To run multiple keywords in Setup or Teardown, define a separate keyword under the Keywords section and call it, or use Run Keywords:
*** Settings ***
Library SeleniumLibrary
Test Setup Run Keywords Open Browser 'https://google.com' chrome
... AND Maximize Browser Window
The easiest way to create the line break and AND is to type AND at the end of the previous line and press Enter; Robot Framework adds ... to the new line. You can also type ..., press Tab, type AND, then press Tab.
There are other settings such as Tags, Documentation and Metadata. I may write separate articles for them; for now, see the Robot Framework documentation's settings section.
Variables
The Variables section is where we declare values shared by the test cases.
*** Variables ***
${URL} https://google.com
${BROWSER} chrome
Use them in a test case:
*** Test Cases ***
Test Case 1
Open Browser ${URL} ${BROWSER}
I will cover declaring and using each variable type in another article. Usually this section holds values that are passed directly as keyword arguments and that we change often.
For a login test, for example, username and password vary between data sets. The Input Text keyword takes two arguments: a locator for the username or password field, and the value to enter. The changing value is a variable in the Variables section. The locator does not change, so it can live in a Resource file instead.
Test Cases
Test Cases is the main section. Each test starts with its name. Spaces separate words, and [] can be used to set a code name for a test case.
The syntax looks like this:
*** Test Cases ***
Test Case 1
Open Browser https://google.com chrome
Maximize Browser Window
Element Should Be Visible //input[@name='q']
Close Browser
Test Case 2
Open Browser https://google.com chrome
Maximize Browser Window
Input Text //input[@name='q'] Robot Framework
Click Button //input[@name='btnK']
Element Should Be Visible //div[@id='result-stats']
Close Browser
There must be at least two spaces between a keyword and its arguments. Test-case content must be indented at least two spaces from the test name; four spaces, or one Tab, is the usual convention.
Keywords
The Keywords section contains reusable actions or functions. Test cases can call them, and they can call one another.
*** Keywords ***
Open Google
Open Browser https://google.com chrome
Maximize Browser Window
Search For
[Arguments] ${keyword}
Input Text //input[@name='q'] ${keyword}
Click Button //input[@name='btnK']
Element Should Be Visible //div[@id='result-stats']
Keywords can accept arguments, and test cases call them with argument values:
*** Test Cases ***
Test Case 1
Open Google
Search For Robot Framework
Keywords can call one another too:
*** Keywords ***
Open Google
Open Browser https://google.com chrome
Maximize Browser Window
Search For
[Arguments] ${keyword}
Open Google
Input Text //input[@name='q'] ${keyword}
Click Button //input[@name='btnK']
Element Should Be Visible //div[@id='result-stats']
Here Search For calls Open Google before doing the other actions.
Avoid circular keyword calls completely, or your test case will enter a time loop for 7,749 lifetimes and never escape.
Putting it all together, a complete test file is:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://google.com
${BROWSER} chrome
*** Test Cases ***
Test Case 1
Open Google
Search For Robot Framework
*** Keywords ***
Open Google
Open Browser ${URL} ${BROWSER}
Maximize Browser Window
Search For
[Arguments] ${keyword}
Open Google
Input Text //input[@name='q'] ${keyword}
Click Button //input[@name='btnK']
Element Should Be Visible //div[@id='result-stats']
Try running this test file.
Typical question: What is the structure of a Robot file?
Typical question: What is the Settings section?
Typical question: How many ways are there to perform multiple actions in Test Setup and Test Teardown?