Keywords in Robot Framework

Keywords are the core building blocks of Robot Framework tests.

They represent actions or steps performed during a test case, such as opening a browser, entering text, or verifying results.

Robot Framework follows a keyword-driven testing approach, meaning every test action is expressed using keywords, making test cases readable, modular, and reusable.

1. Built-in Keywords

Robot Framework and its standard libraries provide many ready-to-use keywords, so you don’t need to write common functions from scratch.

Commonly Used Built-in Keywords

Keyword

Description

Example

Log

Logs a message

Log Hello Robot Framework

Sleep

Pause execution for given time

Sleep 2s

Should be Equal

Verifies equality

Should be Equal 5 5

Set Variable

Stores a value in a variable

${value}= Set Variable 10

Run Keyword If

Executes a keyword conditionally.

Run Keyword If ${status} == PASS Log Success

Evaluate

Executes a Python expression.

${sum}= Evaluate 5 + 3


Example Using Built-in Keywords

*** Test Cases ***
Check Built-In Keywords
    ${num}=    Set Variable    10
    Should Be Equal    ${num}    10
    Log    Test Passed
    Sleep    1s


Explanation:

Built-in keywords simplify test creation by handling common tasks like logging, waiting, and assertions without any additional coding.

2. SeleniumLibrary Keywords

When working with web automation, SeleniumLibrary provides powerful keywords to interact with browsers, elements, and pages.

You must import it using:

*** Settings ***
Library    SeleniumLibrary


Commonly Used SeleniumLibrary Keywords

Keyword

Description

Example

Open Browser

Opens a specified URL in a given browser.

Open Browser https://example.com chrome

Close Browser

Closes the active browser window.

Close Browser

Maximize Browser Window

Maximizes the current browser window.

Maximize Browser Window

Go To

Navigates to a new URL within the same session.

Go To https://google.com

Input Text

Enters text into an input field.

Input Text id=username testUser

Click Button

Clicks a button element.

Click Button id=loginBtn

Click Element

Clicks any element (button, link, etc.) by locator.

Click Element css:button.submit

Click Link

Clicks a hyperlink element.

Click Link Contact Us

Wait Until Element Is Visible

Waits until the given element is visible on the page.

Wait Until Element Is Visible id:username 10s

Page Should Contain

Verifies that the given text appears on the page.

Page Should Contain Welcome

Page Should Contain Element

Verifies that a given web element exists.

Page Should Contain Element xpath://button[text()='Login']

Get Title

Returns the current page title.

${title}= Get Title

Capture Page Screenshot

Takes a screenshot of the current page.

Capture Page Screenshot


Example Using SeleniumLibrary Keywords

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${URL}    https://example.com/login
${BROWSER}    chrome
*** Test Cases ***
Login Test Using Selenium Keywords
    Open Browser    ${URL}    ${BROWSER}
    Maximize Browser Window
    Input Text    id=username    demoUser
    Input Text    id=password    password123
    Click Button    id=login
    Page Should Contain    Welcome
    Capture Page Screenshot
    Close Browser


Explanation:

This test opens a browser, logs into a sample site, verifies a welcome message, captures a screenshot, and then closes the browser.

3. User-Defined Keywords

You can create your own keywords to group multiple actions together.
This makes your tests cleaner, reusable, and easier to maintain.

Example – Creating and Using Custom Keywords

*** Keywords ***
Open Google Page
    Open Browser    https://www.google.com    chrome
    Wait Until Page Contains    Google
    Title Should Be    Google
Verify Google Text
    Page Should Contain    Google
    Page Should Contain Element    //textarea[@name='q']
Close Browser Safely
    Close Browser
*** Test Cases ***
Test Google Page
    Open Google Page
    Verify Google Text
    Close Browser Safely


Test Explanation:

Open Google Page: Opens Google and verifies the page title.
Verify Google Text: Checks that “Google” appears and the search box exists.
Close Browser Safely: Closes the browser session.
 

Related Tutorials