Playwright + Java · Lesson 3 of 38

0/38 done

Module 3: Strings

3. String Manipulation & Text Validation

14 minlesson

Your task

Implement cleanAndExtractId:

  1. Trim the rawMessage string.
  2. If rawMessage starts with "ID:", return the substring following "ID:" trimmed.
  3. Otherwise return an empty string.

Write it in the editor, then press Check answer (Ctrl + Enter).

String Methods for QA Engineers

  1. equals() & equalsIgnoreCase(): Always use .equals() instead of == for String content comparisons.
  2. contains(): Validating that a banner contains an expected keyword.
  3. trim(): Removing leading and trailing whitespace from UI text.
  4. split(): Extracting dynamic values like order confirmation numbers.
java
String bannerText = "  Order #98432 placed successfully!  ";
String cleaned = bannerText.trim();
boolean hasOrder = cleaned.startsWith("Order #");
String orderId = cleaned.split("#")[1].split(" ")[0]; // "98432"

Key Takeaways & Walkthrough Summary

Every web page contains text: headers, order IDs, confirmation toasts, and table cells. In this lesson, you will master Java String methods like equals, contains, trim, and split to reliably parse and assert UI texts. Let's begin!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why does == fail when comparing Java Strings extracted from web elements?

== compares memory addresses (object references), not characters. Web pages create new String objects in memory, so always use str1.equals(str2).

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip