How to Add Variables and Keywords in Robot Framework Script?

Are you struggling to make your test scripts more efficient and reusable in Robot Framework? This guide will walk you through the essential steps to configure and optimize variables and keywords in Robot Framework. Whether you’re a beginner or an experienced tester, this tutorial provides practical insights for building efficient test automation workflows.


Introduction

Robot Framework is a widely-used test automation tool known for its simplicity and flexibility. It allows testers to create reusable test scripts with a focus on variables and keywords—two core components that make testing efficient. In this guide, we’ll discuss:

  • How to add variables
  • Creating custom keywords
  • Running optimized scripts
    And, don’t forget to watch our detailed YouTube tutorial for step-by-step instructions: Watch Now.

What are Variables in Robot Framework?

Variables are placeholders that store data, making test scripts more readable and maintainable. Robot Framework supports:

  • Scalar Variables: Store single values (e.g., ${USERNAME})
  • List Variables: Store a list of items (e.g., @{ITEMS})
  • Dictionary Variables: Store key-value pairs (e.g., &{CREDENTIALS})

Benefits of Using Variables

  1. Reusability: Update values in one place, and they reflect across test cases.
  2. Readability: Test scripts become easier to understand and maintain.
  3. Efficiency: Reduces redundancy in test scripts.

Step-by-Step Guide: Adding Variables in Robot Framework

  1. Declare Variables: Add a [Variables] section in your .robot file to define scalar, list, or dictionary variables.
*** Variables ***
${USERNAME}    admin
${PASSWORD}    pass123
@{ITEMS}       item1    item2    item3
&{CREDENTIALS}    username=admin    password=pass123

Use Variables in Test Cases: Variables can be used in any test step.

*** Test Cases ***
Login Test
    Input Text    username_field    ${USERNAME}
    Input Text    password_field    ${PASSWORD}
    Click Button  login_button

Override Variables:
Use command-line arguments to override variable values dynamically during runtime.

What are Keywords in Robot Framework?

Keywords are reusable test steps that make scripts modular. Robot Framework provides:

  • Built-in Keywords (e.g., Open Browser, Input Text)
  • User-Defined Keywords for custom logic.

Creating and Using Custom Keywords

  1. Define Keywords: Use the [Keywords] section to group test steps into a reusable keyword.
*** Keywords ***
Login
    Input Text    username_field    ${USERNAME}
    Input Text    password_field    ${PASSWORD}
    Click Button  login_button

Use Keywords in Test Cases:

*** Test Cases ***
Login Test
    Login
    Verify Element    dashboard

Debugging Keywords: Add logs to troubleshoot keyword execution:

Log    Login Successful

Running Your Robot Framework Scripts

Once your variables and keywords are configured, run your script using the command line:

robot test_suite.robot

Watch the Full Video Tutorial

For a detailed walkthrough, watch our YouTube video where we demonstrate:

  • Declaring variables
  • Writing reusable keywords
  • Best practices for script optimization

Gain knowledge in software testing and elevate your skills to outperform competitors.

Training ProgramDemo TimingTraining FeesAction
Software Testing Online Certification TrainingDemo at 09:00 AM ETStarts at $1049Book your demo
Software Testing Classroom Training in VirginiaDemo at 01:00 PM ET every SundayStarts at $1699Book your demo
Selenium Certification TrainingDemo at 10:00 AM ETStarts at $550Book your demo
Manual Testing CourseDemo at 09:00 AM ETStarts at $400Book your demo
SDET Course – Software Automation Testing TrainingDemo at 11:00 AM ETStarts at $550Book your demo
Automation Testing Real-Time Project TrainingDemo at 10:00 AM ETStarts at $250Book your demo
Business Analyst CertificationDemo at 12:00 PM ETStarts at $550Book your demo

Search for QA Testing Jobs, Automation Roles, and more…