Skip to content

The String library

Chapter lab: Text and datach15-text.zip · 5 KB

This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.

Download ch15-text.zip

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

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

Open 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:

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.

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”
Edit

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} 2

Done 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 String
Library 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} 3
Run

Run:

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

Done 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.

  1. Which keyword returns lines containing a substring?

    Show the answer

    Answer: Get Lines Containing String — The String library's Get Lines Containing String returns the matching lines.

  2. Get Line Count returns…

    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.

All terms in the glossary →