A keyword is an action or a group of actions in Robot Framework. It may be built in, supplied by a library or defined by us.
Built-in
A built-in keyword comes from the Standard Library or an external library. Robot Framework itself includes a group of ready-to-use keywords in the BuiltIn Keywords library. The basics include keywords for controlling test flow:
Run Keyword And Expect Error
Run Keyword And Ignore Error
Run Keyword And Return
Run Keyword And Return If
Run Keyword And Return Status
Run Keyword And Warn On Failure
Run Keyword If
Run Keyword If All Tests Passed
Run Keyword If Any Tests Failed
Run Keyword If Test Failed
Run Keyword If Test Passed
Run Keyword If Timeout Occurred
Run Keyword Unless
Run Keywords
Skip
Skip If
There are conversion keywords:
Convert To Boolean
Convert To Bytes
Convert To Hex
Convert To Integer
Convert To Number
Convert To Octal
Convert To String
And assertion keywords:
Should Be Empty
Should Be Equal
Should Be True
Should Contain
Should Contain Any
Should Contain X Times
Should End With
Should Match
Should Match Regexp
Should Not Be Empty
Should Not Be Equal
Should Not Be True
Should Not Contain
Should Not Contain Any
Should Not End With
Should Not Match
Should Not Match Regexp
Should Not Start With
Should Start With
The Robot Framework documentation explains how to use them. As described in the Library article, Standard Libraries such as Collections, DateTime and OperatingSystem also provide ready-made keywords. The same applies to external libraries such as SeleniumLibrary and AppiumLibrary.
User-defined
User-written keywords go under the Keywords section. Here is a SeleniumLibrary example:
*** Keywords ***
Open Browser To Google
Open Browser http://google.com chrome
Usually we would not create a keyword for one keyword, but it can hide implementation details and make the test easier to read. A non-technical reader looking at Open Browser http://google.com chrome may wonder what chrome is. Looking at Open Browser To Google, they immediately know what is happening.
Open Browser http://google.com chrome
Open Browser To Google
We can also add helper actions:
*** Keywords ***
Open Browser To Google
Open Browser http://google.com chrome
Maximize Browser Window
Arguments
A keyword can receive a fixed value or a variable. As with a Python function, define parameters using [Arguments]:
*** Keywords ***
Open Browser To Page
[Arguments] ${url} ${browser}
Open Browser ${url} ${browser}
Call it with literal values:
Open Browser To Page http://google.com chrome
Or with declared variables:
*** Variables ***
${URL} http://google.com
${BROWSER} chrome
*** Test Cases ***
Test
Open Browser To Page ${URL} ${BROWSER}
Arguments can have defaults. Unless a different value is supplied, the default is used, just as in Python:
*** Keywords ***
Open Browser To Page
[Arguments] ${url} ${browser}=chrome
Open Browser ${url} ${browser}
Return
A keyword can return a value for another keyword to use:
*** Keywords ***
Calculate Sum
[Arguments] ${a} ${b}
${sum}= Evaluate ${a}+${b}
RETURN ${sum}
*** Keywords ***
Test
${sum}= Calculate Sum 1 2
Log ${sum}
Using a Robot keyword for calculations and returning values is something we should generally avoid. Robot Framework is essentially a wrapper around Python, so calculations, loops and similar tasks run more slowly in Robot Framework. In this situation, write a Python Library for the task instead. Here is how.
Keywords from a self-defined library
A user-defined Library is a Python file that we import into Robot Framework. For example, mylib.py:
# mylib.py
def calculate_sum(a, b):
return a+b
Import and use it in Robot Framework:
*** Settings ***
Library mylib.py
*** Keywords ***
Test
${sum}= Calculate Sum 1 2
Log ${sum}
The result is the same, but this is much faster than implementing the calculation in Robot Framework.
Pay attention to keyword naming. Python's calculate_sum becomes Robot Framework's Calculate Sum: snake_case words are separated by spaces and capitalised.
get_user_info->Get User Infoget_user_info_by_id->Get User Info By Id
Robot Framework dictionaries work much like Python dictionaries, just as Robot lists resemble Python lists. The declaration syntax differs: dictionaries use & instead of the list marker @.
Conclusion
After the articles on Setting Up, First Test Case, Library, Resource and Variables, Setup and Teardown, Variables, and this Keyword article, we have enough knowledge to write a simple test suite.
Task: Write a test case to verify the sum of two numbers.
Try the task above. After this article I will go deeper into browser automation with SeleniumLibrary.