Examples

Robot Framework Examples: Practical Test Cases for Real Projects

Test case cards with verified results

Robot Framework examples are the quickest way to understand how the framework behaves in real projects. This collection shows complete, runnable patterns for basic checks, data-driven testing, behaviour-driven style, API testing, file handling and process automation, each with a short explanation of why it is written that way.

Basic Verification Example

The simplest suites use only BuiltIn keywords. They are useful for learning syntax and for small sanity checks in pipelines, because they need no extra installation beyond the core package.

*** Test Cases ***
String Contains Expected Text
    ${message}=    Set Variable    Order 1042 confirmed
    Should Contain    ${message}    confirmed
    Should Match Regexp    ${message}    Order \\d+

Notice the double backslash in the regular expression. Robot Framework uses the backslash as its own escape character, so a literal backslash must be escaped once before Python’s regular expression engine sees it.

Data-Driven Example With Test Templates

When the same steps run with many input combinations, a test template keeps the suite compact. Each test row passes its arguments to the template keyword, and column headers in the Test Cases line act as documentation.

*** Settings ***
Test Template    Login Should Fail

*** Test Cases ***    USER      PASSWORD
Empty Password        demo      ${EMPTY}
Wrong Password        demo      wrong
Unknown User          nobody    secret

*** Keywords ***
Login Should Fail
    [Arguments]    ${user}    ${password}
    Attempt Login    ${user}    ${password}
    Error Message Should Be Shown

Each row appears as a separate test in the report, so one failing combination does not hide the others. This pattern is ideal for boundary values and validation rules.

Behaviour-Driven Example

Given, When, Then, And and But prefixes are dropped when keywords are matched. Teams practising behaviour-driven development can write scenarios in a familiar style while implementing steps as normal user keywords.

*** Test Cases ***
Registered User Can Log In
    Given Login Page Is Open
    When User "demo" Logs In
    Then Welcome Page Should Be Open

The step User "demo" Logs In uses an embedded argument, explained further in the Robot Framework keywords overview.

Web User Interface Example

Browser tests typically import SeleniumLibrary or Browser library and use a suite teardown to close browsers even when a test fails. Keeping the URL in a variable allows the same suite to target staging and production-like environments.

*** Settings ***
Library           SeleniumLibrary
Suite Teardown    Close All Browsers

*** Test Cases ***
Search Returns Results
    Open Browser    ${URL}    headlesschrome
    Input Text      name:q    robot
    Press Keys      name:q    ENTER
    Wait Until Page Contains Element    css:.result

Locator strategies, waits and headless execution are covered in the Robot Framework Selenium article.

API Testing Example

RequestsLibrary, installed as robotframework-requests, adds HTTP keywords. The example checks a health endpoint and validates a JSON field, which is a common first step when adding service-level checks to a pipeline.

*** Settings ***
Library    RequestsLibrary

*** Test Cases ***
Health Endpoint Responds
    ${resp}=    GET    ${API}/health    expected_status=200
    Should Be Equal    ${resp.json()}[status]    ok

File and Operating System Example

The OperatingSystem standard library handles files and directories without extra installation. It is frequently used to prepare test data, check generated exports and clean up afterwards.

*** Settings ***
Library    OperatingSystem

*** Test Cases ***
Export File Is Created
    Run Export Job
    File Should Exist    ${OUTPUT_DIR}/export.csv
    ${content}=    Get File    ${OUTPUT_DIR}/export.csv
    Should Contain    ${content}    order_id

Process Automation Example

A Tasks section replaces Test Cases for robotic process automation. The syntax and reports are identical, but the wording signals that the goal is completing work rather than verifying behaviour.

*** Tasks ***
Archive Monthly Invoices
    ${files}=    List Files In Directory    ${INBOX}    *.pdf
    FOR    ${file}    IN    @{files}
        Move File    ${INBOX}/${file}    ${ARCHIVE}
    END

Choosing the Right Pattern

Each pattern solves a different problem. Picking the right one early keeps suites readable as they grow and reduces the amount of refactoring needed later.

NeedRecommended pattern
Same steps, many inputsTest template
Business-readable scenariosGiven / When / Then with user keywords
Browser workflowsSeleniumLibrary or Browser library with resource files
Service checksRequestsLibrary
Repetitive office workTasks section

Control Structures Example

Modern versions of the framework support native IF, FOR, WHILE and TRY blocks. They are useful for small decisions and loops inside user keywords, although heavy logic still belongs in Python libraries where it is easier to test and maintain.

*** Keywords ***
Retry Until Ready
    FOR    ${attempt}    IN RANGE    5
        ${ready}=    Run Keyword And Return Status    Service Should Be Ready
        IF    ${ready}    RETURN
        Sleep    2s
    END
    Fail    Service did not become ready

Read Optional Config
    TRY
        ${data}=    Get File    ${CONFIG}
    EXCEPT    *No such file*    type=GLOB
        Log    Using default configuration
    END

Each block ends with END, and indentation inside the block is a readability convention. The log shows every loop iteration separately, which makes debugging retries straightforward.

Tips for Writing Your Own Examples

  • Name tests after the behaviour they verify, not the steps they take
  • Keep each test focused on one outcome
  • Use variables for URLs, users and paths
  • Add tags such as smoke or regression for selective runs

Small, focused examples are easier to copy into new suites and easier for reviewers to understand during code review.

Frequently Asked Questions

Where can I find more official examples?

The Robot Framework User Guide contains many short examples, and the project’s website links to demo projects that show complete suites for web and API testing.

Can these examples run without changes?

The BuiltIn and OperatingSystem examples need only small adjustments such as defining keywords or paths. Web and API examples need the relevant library installed and a real target URL.

Should examples go into one large file?

No. Group related tests into suite files and shared keywords into resource files, so each file stays short and focused.

How do I run only one example test?

Use the --test option with the test name, for example robot --test "Wrong Password" login.robot. Wildcards are supported, and tags offer another way to select groups of tests.

Designing Repeatable Examples

A useful example begins with a predictable starting state. For a checkout test, prepare a fresh cart and choose a product whose stock and price are controlled by the test environment. Populate the cart before the steps under examination, then check the order total and confirmation against known values. When the case fails, the report should identify whether preparation, checkout or verification caused the problem. An order that a previous run happened to leave behind gives the next run an unreliable starting point.

Give successful and unsuccessful paths their own cases. A checkout suite might confirm that a valid order produces a reference number and that an empty cart displays a clear message. For the second case, verify the intended message and check that no new order appears. This catches the case where an error message is shown after an order has already been recorded. Keep the expected effect visible in the test name so a reviewer can understand the behaviour from the report.

Before adding more examples, run each case by itself and then run the whole suite. A case that passes only when the full suite runs may rely on setup performed by another test. Move that setup into a shared keyword and call it where needed. Review the log after each change, paying particular attention to whether a failed assertion includes the actual and expected values needed to diagnose it without rerunning the entire test suite.

Conclusion

Robot Framework examples like these cover the patterns most teams need: basic checks, templates, behaviour-driven steps, web and API tests, file handling and RPA tasks. Copy the pattern that matches your need, adapt names and data, and build on the fundamentals from the Robot Framework tutorial.