Skip to content

Assertions and auto-waiting

Chapter lab: Browser foundationsch08-web-foundations.zip · 27 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch08-web-foundations.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch08-web-foundations.zip && unzip -q ch08-web-foundations.zip && rm ch08-web-foundations.zip

Open the folder in VS Code with code rfe-ch08-web-foundations (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Also needed for this lab

Install Node.js (an LTS release), then run once — it downloads a browser engine (large):

Terminal window
uv run rfbrowser init chromium
Terminal window
rfbrowser init chromium
Terminal window
poetry run rfbrowser init chromium

Start the RF Demo Shop in its own terminal and keep it running while you work:

Terminal window
uv run python demo_app/serve.py
Terminal window
python demo_app/serve.py
Terminal window
poetry run python demo_app/serve.py
What's inside
  • Directoryrfe-ch08-web-foundations/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorydemo_app/
      • api.py
      • serve.py
      • store.py
      • web.py
    • Directoryresources/
      • shop.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Browser getter assertions do two jobs at once: they read a value and assert it, and they auto-wait — retrying until the assertion holds or the timeout passes. Get Text data-testid=result-count == 4 waits for the count to become 4. You almost never need Sleep.

Get Element Count asserts how many elements match a locator — handy for counting cards, rows, or results.

Task 1 — Assert the catalog contents with getter assertions

Section titled “Task 1 — Assert the catalog contents with getter assertions”
Edit

Goal: create checks/catalog.robot with a small Sign In helper keyword, then a test that asserts the catalog reports four products and shows four cards.

Here is the same shape using a helper and getter assertions, as a different user. Adapt it to primary_user and assert the result-count.

Worked example — adapt it, don’t copy it

*** Settings ***
Library Browser
Resource ../resources/shop.resource
*** Keywords ***
Sign In
New Browser chromium headless=True
New Context
New Page ${BASE_URL}/login
Fill Text data-testid=login-username reviewer_user
Fill Text data-testid=login-password learning-password
Click data-testid=login-submit
*** Test Cases ***
Catalog Renders Its Cards
Sign In
Get Text data-testid=catalog-title contains Catalog
Get Element Count data-testid=catalog-card == 4
Close Browser

Done when

the test uses Get Element Count.

Hint 1

Put the four sign-in steps into a Sign In keyword under *** Keywords ***, then call it as the first line of the test.

Hint 2

Assert the reported count and the card count: Get Text data-testid=result-count == 4 and Get Element Count data-testid=catalog-card == 4.

Solution
*** Settings ***
Library Browser
Resource ../resources/shop.resource
*** Keywords ***
Sign In
New Browser chromium headless=True
New Context
New Page ${BASE_URL}/login
Fill Text data-testid=login-username primary_user
Fill Text data-testid=login-password learning-password
Click data-testid=login-submit
*** Test Cases ***
Catalog Shows Four Products
Sign In
Get Text data-testid=result-count == 4
Get Element Count data-testid=catalog-card == 4
Close Browser
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the test Catalog Shows Four Products passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. Get Text data-testid=result-count == 4 will…

    Show the answer

    Answer: retry until the text equals 4 or the timeout passes — Browser getter assertions auto-wait: they retry until the assertion holds or the timeout is reached — no manual sleeps.

  2. Which keyword counts matching elements?

    Show the answer

    Answer: Get Element Count — Get Element Count <locator> == <n> asserts how many elements match.

Key terms

getter assertionsconcept
Get Text ... == value and friends auto-wait — no Sleep needed.
Get Element Countkeyword
Assert the number of elements matching a locator.

All terms in the glossary →