Master Robot Framework Variables and Keywords for Scalable Automation

Table of Contents

Introduction

Knowing variables and keywords is crucial, whether you're automating web apps or APIs. When you have a thorough understanding of how to use variables and keywords, automation in Robot Framework becomes effective. In addition to being modular and reusable, these two building pieces also make maintaining your test scripts simpler. As the Robot Framework is an open-source automation framework in software automation testing services, it is renowned for its ease of use and expandability.

What Are Variables in Robot Framework?

Variables store data and act as placeholders. Instead of hardcoding values (e.g., URLs, credentials, locators), variables help centralize the data and improve readability.

Key Benefits:

  • Avoid redundancy
  • Support easy updates
  • Improve flexibility in test data

Why Are Variables & Keywords So Important?

Variables and keywords form the backbone of Robot Framework scripts:

  • Variables help reduce redundancy and centralize data like URLs, credentials, and element locators.
  • Keywords promote reusability, allowing you to encapsulate frequently used logic into modular blocks.
  • With these two, build automation that's scalable, maintainable, and elegant.

Types of Variables in Robot Framework

1. Scalar Variables

Holds a single value like string, number, or boolean.
Syntax: ${variable_name}

${URL}          https://example.com
${USERNAME}     admin
${PASSWORD}     Pass@123
    

2. List Variables

Used to store multiple values.
Syntax: @{list_name}

@{BROWSERS}    Chrome    Firefox    Edge
    

3. Dictionary Variables

Store key-value pairs, like Python dictionaries.
Syntax: &{dict_name}

&{LOGIN_DATA}    username=admin    password=admin123
    

4. Environment Variables

Imported using Operating System or Built-in keywords.

${HOME}    Get Environment Variable    HOME
    

Variable Scopes in Robot Framework

ScopeDefinition PlaceAccess
LocalInside a test case or keywordThat block only
Test CaseInside the testIn that test only
Suite LevelIn Variables sectionAvailable in whole suite
GlobalPassed via command line or Set Global VariableAvailable everywhere

Use Variables section in your .robot file or import from a separate .py or .robot variable file for better organization.

What Are Keywords in Robot Framework?

Keywords define the actions or steps of your test case. Think of them as functions in other programming languages.

Benefits of Keywords:

  • Improve reusability
  • Simplify complex steps
  • Promote modularity

Types of Keywords:

1. Built-in Keywords

These come with Robot Framework out-of-the-box.

Log                 Hello World!
Should Be Equal     ${value1}    ${value2}
Sleep               2s
    

2. Library Keywords

Provided by libraries like:

  • SeleniumLibrary (for web UI)
  • RequestsLibrary (for APIs)
  • Collections (for list/dictionary ops)
Open Browser    https://example.com    Chrome
Click Element   id=login-btn
    

3. User-Defined Keywords

Created by you to group common steps.

*** Keywords ***
Login To Application
    [Arguments] ${username} ${password}
    Input Text id=username ${username}
    Input Text id=password ${password}
    Click Button id=login-btn
    

Use in test cases like:

Login To Application    admin    admin123
    

Robot Framework for Beginners Simplify Test Automation with Ease.png

How to Use Keywords Effectively

  • Use meaningful names like Verify Login instead of Test1.
  • Add [Arguments] to make them dynamic.
  • Use them across multiple test cases by importing resource files.

Keyword Structure & Arguments

You can create custom keywords with:

  • Positional arguments
  • Default values
  • Conditional steps
*** Keywords ***
Verify Status Code
    [Arguments]    ${response}    ${expected}=200
    Should Be Equal As Numbers    ${response.status_code}    ${expected}
    

Resource File Structure

Use resource files to keep your keywords and variables separate for better reusability.

/tests/
    login_tests.robot
/resources/
    keywords.robot
    variables.robot
    

Import Example:

*** Settings ***
Resource ../resources/keywords.robot
Resource ../resources/variables.robot
    

Example Test Case:

*** Settings ***
Library  SeleniumLibrary
Resource ../resources/keywords.robot
Resource ../resources/variables.robot

*** Test Cases ***
Valid Login Test
    Open Browser    ${URL}    ${BROWSER}
    Login To Application    ${USERNAME}    ${PASSWORD}
    Page Should Contain    Welcome
    Close Browser
    

Real-World Use Case:

*** Variables ***
${URL}         https://app.example.com
${BROWSER}     Chrome
${USERNAME}    testuser
${PASSWORD}    pass123

*** Keywords ***
Login To Dashboard
    Input Text id=username ${USERNAME}
    Input Text id=password ${PASSWORD}
    Click Button id=submit

*** Test Cases ***
Login Test
    Open Browser ${URL} ${BROWSER}
    Login To Dashboard
    Page Should Contain    Dashboard
    Close Browser
    

Conclusion

Mastering variables and keywords in Robot Framework are a crucial step toward building efficient and readable test automation. By separating logic and data, you ensure that your tests remain clean, easy to debug, and adaptable to changes.

About Author

Divya Panchal

Divya Panchal is an ISTQB-certified QA Tester at PixelQA a Software Testing Company with expertise in both manual and automation testing. She has evolved from a trainee to a key contributor across multiple projects, consistently ensuring software quality and reliability. With a strong focus on continuous professional development, Divya is dedicated to expanding her knowledge in automation and API testing, underscoring her commitment to delivering high-quality software solutions..