Assertions and auto-waiting
Chapter lab: Browser foundations
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch08-web-foundations.zip && unzip -q ch08-web-foundations.zip && rm ch08-web-foundations.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch08-web-foundations.zip -OutFile ch08-web-foundations.zip; Expand-Archive ch08-web-foundations.zip -DestinationPath .; Remove-Item ch08-web-foundations.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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):
uv run rfbrowser init chromiumrfbrowser init chromiumpoetry run rfbrowser init chromiumStart the RF Demo Shop in its own terminal and keep it running while you work:
uv run python demo_app/serve.pypython demo_app/serve.pypoetry run python demo_app/serve.pyWhat'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”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 BrowserResource ../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 BrowserDone 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 BrowserResource ../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 BrowserTask 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone 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.
Get Text data-testid=result-count == 4will…Browser getter assertions auto-wait: they retry until the assertion holds or the timeout is reached — no manual sleeps.
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.
Which keyword counts matching elements?
Get Element Count <locator> == <n>asserts how many elements match.Show the answer
Answer:
Get Element Count—Get Element Count <locator> == <n>asserts how many elements match.
Key terms
- getter assertionsconcept
Get Text ... == valueand friends auto-wait — no Sleep needed.- Get Element Countkeyword
- Assert the number of elements matching a locator.