Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Patterns and Extensions

Page objects and layered architecture

Structure a growing Robot Framework suite in layers: tests, business keywords, page keywords and libraries; resource files, naming and a runnable page-object stand-in.

A suite of ten tests can be one flat file. A suite of five hundred cannot. When the login button's id changes and forty tests break, you learn why architecture matters. A layered suite separates what is tested from how the application is driven, so a change in the interface is fixed in one place.

This lesson explains the layers, the page object pattern, how to arrange resource files and folders, and common smells. The Robot code is read-only (no real browser here); the runnable stand-ins show the layers in Python.

The four layers

Four layers

Tests Scenarios in business words.
Business keywords Log In As, Buy Product.
Page keywords Locators and clicks per page.
Libraries Selenium, Requests, Python.

Each layer only calls the layer below it.

1 / 2
  1. Tests read like a story: Log In As demo s3cret, Add Product To Cart Clean Code.
  2. Business keywords express user goals and combine page steps. They do not contain locators.
  3. Page keywords (the page objects) know how one page or component works: locators, inputs, clicks.
  4. Libraries are SeleniumLibrary, Browser, RequestsLibrary and your own Python code.

The rule that keeps it healthy: a layer may only use the layer directly below. A test should never contain a locator.

flowchart TD
    T["Tests: scenarios"] --> B["Business keywords"]
    B --> P["Page keywords (page objects)"]
    P --> L["Libraries: Selenium, Browser, Requests"]
    L --> A["Application under test"]

Resource files

Robot shares keywords and variables across files with resource files (.resource, or .robot). They contain *** Settings ***, *** Variables *** and *** Keywords ***, but no tests:

TEXT
# resources/pages/login_page.resource
*** Settings ***
Library    SeleniumLibrary

*** Variables ***
${LOGIN_USER}      id:username
${LOGIN_PASSWORD}  id:password
${LOGIN_BUTTON}    id:login

*** Keywords ***
Submit Credentials
    [Arguments]    ${user}    ${password}
    Input Text        ${LOGIN_USER}        ${user}
    Input Password    ${LOGIN_PASSWORD}    ${password}
    Click Button      ${LOGIN_BUTTON}
TEXT
# resources/business.resource
*** Settings ***
Resource    pages/login_page.resource
Resource    pages/home_page.resource

*** Keywords ***
Log In As
    [Arguments]    ${user}    ${password}
    Open Login Page
    Submit Credentials    ${user}    ${password}
    Home Page Should Be Open    ${user}
TEXT
# tests/login.robot
*** Settings ***
Resource    ../resources/business.resource

*** Test Cases ***
Valid User Can Log In
    Log In As    demo    s3cret

Notice how the test file imports only the business resource. If the login button changes, login_page.resource changes; the test never notices.

A folder layout

TEXT
project/
  tests/
    login.robot
    checkout.robot
  resources/
    business.resource
    pages/
      login_page.resource
      home_page.resource
    api/
      orders_api.resource
  libraries/
    OrderTools.py
  data/
    users.yaml
  results/            (ignored by git)
  requirements.txt
  robot.toml          (default options, Robot 7.0+)

Since Robot 7.0 a robot.toml file stores default options (output directory, variables, excluded tags), replacing long command lines.

Page objects in Python

A page object can also be a Python class. The test-side steps stay readable, and the page class owns the details. This stand-in simulates a login page and a home page with a fake driver; it is the same pattern as the .resource files above.

Python Stand-in: page objects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test layer never mentions an id. If id:login became css:.signin, only LoginPage.BUTTON changes.

Measuring the benefit

This stand-in counts how many places must change when a locator changes: with locators repeated in every test versus kept in one page object.

Python Stand-in: cost of a locator change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Naming and style

  • Name keywords as verbs and goals: Add Product To Cart, Cart Total Should Be.
  • Name checks with Should and getters with Get, like the standard libraries.
  • Use Title Case for keywords and snake_case or UPPER names for variables, consistently.
  • Document each business keyword with [Documentation].
  • Use [Arguments] with defaults (${quantity}=1) instead of copies.
  • Keep keywords short: five to ten lines. If a keyword needs a long IF ladder, move the logic to Python (see custom libraries).

Smells to watch for

Smell Fix
Locators in test files Move to page keywords or variables
Tests that depend on each other Each test sets up and cleans its own data
Sleep everywhere Use explicit waits (see Locators and waiting)
One giant resource file Split by page or feature
Duplicate keywords with tiny differences One keyword with arguments
Business keywords that click Push clicks down to page keywords

You do not need all layers on day one. Start flat, and introduce a layer the moment you copy something a third time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a layered suite, which layer should contain locators?

    Page keywords own the details of one page, so UI changes stay in one place.
  2. 2. What is a resource file?

    Resource files hold shared Settings, Variables and Keywords and are imported with Resource.
  3. 3. Which rule keeps layers healthy?

    Limiting each layer to the next one down keeps changes contained.
  4. 4. A keyword is growing a long chain of IF and FOR blocks. What is usually best?

    Complex logic is easier to write and unit test in Python.

Summary

  • Layer a suite: tests, business keywords, page keywords, libraries.
  • Each layer calls only the one below; tests should not contain locators.
  • Resource files share keywords and variables; organize folders by page, feature and API.
  • Page objects can be .resource files or Python classes.
  • Name keywords as goals, avoid sleeps and duplicates, and add layers when repetition appears.

Next: Project: a login and checkout web suite