The String library
Chapter lab: Text and data
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch15-text.zip && unzip -q ch15-text.zip && rm ch15-text.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch15-text.zip -OutFile ch15-text.zip; Expand-Archive ch15-text.zip -DestinationPath .; Remove-Item ch15-text.zipOpen the folder in VS Code with code rfe-ch15-text (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.
Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.
What's inside
Directoryrfe-ch15-text/
Directory.vscode/
- extensions.json
Directorychecks/
- smoke.robot
Directorysample_data/
- app.log
- catalog.xml
- orders.csv
- results.xml
- settings.cfg
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
The String standard library slices and searches text. On the bundled log
sample_data/app.log, you can filter lines and count them — a common
verification when checking logs for errors. Get Lines Containing String keeps
only the matching lines; Get Line Count counts them.
Task 1 — Count the error lines in the log
Section titled “Task 1 — Count the error lines in the log”Goal: create checks/string.robot that reads the log, filters it to the
ERROR lines, and asserts how many there are.
Here is the same filter-and-count over a small built-up string (no file needed). Adapt it to read the log instead.
Worked example — adapt it, don’t copy it
*** Settings ***Library String
*** Test Cases ***Count Matching Lines ${text}= Catenate SEPARATOR=\n apple error banana error ${matches}= Get Lines Containing String ${text} error ${count}= Get Line Count ${matches} Should Be Equal As Integers ${count} 2Done when
the suite imports String and defines Count Error Lines In The Log.
Hint 1
Read the log first — you’ll need OperatingSystem too:
${log}= Get File sample_data/app.log.
Hint 2
Filter to the error lines with Get Lines Containing String ${log} ERROR,
count with Get Line Count, and assert the count is 3.
Solution
*** Settings ***Library StringLibrary OperatingSystem
*** Test Cases ***Count Error Lines In The Log ${log}= Get File sample_data/app.log ${errors}= Get Lines Containing String ${log} ERROR ${count}= Get Line Count ${errors} Should Be Equal As Integers ${count} 3Task 2 — Run it green
Section titled “Task 2 — Run it green”Run:
uv run robot checksrobot checkspoetry run robot checksDone when
the log has exactly three error lines.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
Which keyword returns lines containing a substring?
The String library's
Get Lines Containing Stringreturns the matching lines.Show the answer
Answer:
Get Lines Containing String— The String library'sGet Lines Containing Stringreturns the matching lines.Get Line Countreturns…It counts the lines in a string — handy after filtering.
Show the answer
Answer: the number of lines — It counts the lines in a string — handy after filtering.
Key terms
- Stringconcept
- Standard library for text: split, match, replace, count lines.
- Get Lines Containing Stringkeyword
- Return the lines of a string that contain a substring.