Variables, resource files and user keywords
On this page
The first test works, but read it again as somebody who does not know the page: id:my-text-id, name:my-select, css:button[type=submit]. The test says how the page is built, not what the user does. This chapter moves the how into keywords and resource files so the tests read like the behaviour they check. It is the Robot Framework version of the page object pattern; the page object model course explains the idea in general.
Variables#
Robot Framework has four kinds of variable syntax:
| Syntax | Kind | Example |
|---|---|---|
${NAME} |
scalar: one value |
${BROWSER} holds chrome
|
@{NAME} |
list, expanded into separate arguments | @{USERS} |
&{NAME} |
dictionary, expanded into named arguments |
&{ENGINES} in chapter 5 |
%{NAME} |
environment variable |
%{TB_KEY}, or %{TB_BUILD=robot-local} with a default |
Define them in a *** Variables *** section. Names are case-insensitive and spaces and underscores are ignored, so ${FORM URL} and ${form_url} are the same variable. Uppercase for suite-level values is the common convention.
Any variable can be overridden from the command line, and the command line wins over the file:
robot --variable BROWSER:firefox tests/
That one switch is how the same suite runs on Chrome locally and on Safari in the cloud later, without touching a test. Robot Framework also sets a number of built-in variables you will use: ${TEST NAME}, ${TEST STATUS} (in a teardown), ${SUITE NAME}, ${EMPTY}, ${TRUE} and ${FALSE}.
User keywords#
A user keyword is defined in a *** Keywords *** section exactly like a test case, plus an optional [Arguments] line:
*** Keywords ***
Fill In The Form
[Arguments] ${text} ${option}=Two
Input Text id:my-text-id ${text}
Select From List By Label name:my-select ${option}
${option}=Two gives the argument a default. Callers can pass it by position, Fill In The Form Hello Three, or by name, Fill In The Form Hello option=Three; the named form is easier to read when a keyword has more than one optional argument. A keyword can hand a value back with RETURN, and the caller stores it with ${value}= My Keyword.
Resource files#
A resource file is a .resource file with Settings, Variables and Keywords, but no test cases. Tests import it with Resource and can then use everything it defines. Split them by responsibility.
resources/browser.resource knows how to start and stop a browser:
*** Settings ***
Documentation Opens and closes the browser.
Library SeleniumLibrary timeout=10s
*** Variables ***
${BROWSER} chrome
*** Keywords ***
Open Test Browser
[Arguments] ${url}
Open Browser ${url} headless${BROWSER}
Set Window Size 1920 1080
Close Test Browser
Close All Browsers
resources/web_form.resource knows the web form, and nothing else:
*** Settings ***
Documentation Keywords for the Selenium practice web form.
Resource browser.resource
*** Variables ***
${FORM URL} https://www.selenium.dev/selenium/web/web-form.html
*** Keywords ***
Open The Web Form
Open Test Browser ${FORM URL}
Title Should Be Web form
Fill In The Form
[Arguments] ${text} ${option}=Two
Input Text id:my-text-id ${text}
Select From List By Label name:my-select ${option}
Submit The Form
Click Button css:button[type=submit]
Confirmation Should Be Shown
Wait Until Page Contains Received!
Title Should Be Web form - target page
Resource paths are resolved relative to the file that imports them, so browser.resource works from web_form.resource in the same folder, and tests one level down import ../resources/web_form.resource.
The web form's submit button gets a new id and the locator has to change. With the structure above, how many files do you edit?
Tests only call Submit The Form; the locator lives in one keyword. That is the point of the resource layer. browser.resource knows nothing about any particular page.
Tests that read like behaviour#
tests/web_form.robot:
*** Settings ***
Documentation The web form, written with user keywords.
Resource ../resources/web_form.resource
Test Setup Open The Web Form
Test Teardown Close Test Browser
*** Test Cases ***
Submitting The Form Shows A Confirmation
Fill In The Form Robot Framework
Submit The Form
Confirmation Should Be Shown
The Dropdown Keeps The Chosen Option
Fill In The Form anything option=Three
List Selection Should Be name:my-select Three
Disabled Input Cannot Be Edited
Element Should Be Disabled name:my-disabled
Test Setup and Test Teardown in Settings apply to every test in the file, so no test opens or closes a browser itself. Each test gets a fresh browser, which keeps tests independent: none of them depends on what a previous one left behind. That independence is what makes parallel runs in chapter 7 possible.
The last two tests still call SeleniumLibrary keywords directly. That is fine for one-off checks; once the same locator shows up in a second test, move it into the resource file.
robot --outputdir results tests/web_form.robot
robot --outputdir results --variable BROWSER:firefox tests/web_form.robot
Submitting The Form Shows A Confirmation | PASS |
------------------------------------------------------------------------------
The Dropdown Keeps The Chosen Option | PASS |
------------------------------------------------------------------------------
Disabled Input Cannot Be Edited | PASS |
------------------------------------------------------------------------------
Web Form :: The web form, written with user keywords. | PASS |
3 tests, 3 passed, 0 failed
Both commands pass: headless Chrome and headless Firefox, selected with nothing but a variable.
browser.resource sets ${BROWSER} chrome. You run robot --variable BROWSER:firefox tests/. Which browser opens?
Variables given with --variable have higher priority than those in Variables sections, which is exactly what lets the file hold a sensible default while CI picks another browser.
Where Python keywords come in#
Some things are awkward in Robot Framework syntax: building nested data, calling an API, parsing a file. Any Python module can be imported as a library, and its functions become keywords: def testingbot_options(...) is called as TestingBot Options. The next chapter uses exactly that to build the capabilities for the TestingBot grid.