Careeroza
Strings for Test Data
Methods You Will Use Daily
| Method | Use in tests |
|---|---|
equals / equalsIgnoreCase | compare the actual text with the expected text |
contains | title contains "ShopEase" |
trim | remove spaces around text read from the page |
replace / replaceAll | turn "₹1,299" into "1299" |
split | break "Showing 1-24 of 120" into parts |
startsWith / endsWith | URL starts with https |
Example
Example
Cleaning text read from a product card and converting the price into a number:
public class Main {
public static void main(String[] args) {
String title = " Nike Revolution 7 Running Shoes ";
String priceText = "₹1,299.00";
String resultText = "Showing 1-24 of 120 results";
System.out.println("[" + title.trim() + "]");
System.out.println("Contains Running: " + title.contains("Running"));
double price = Double.parseDouble(priceText.replace("₹", "").replace(",", ""));
System.out.println("Price as number: " + price);
String[] words = resultText.split(" ");
int total = Integer.parseInt(words[3]);
System.out.println("Total results: " + total);
System.out.println("Same text? " + "Add to Cart".equalsIgnoreCase("ADD TO CART"));
}
}Output
[Nike Revolution 7 Running Shoes] Contains Running: true Price as number: 1299.0 Total results: 120 Same text? true
Note: Never compare strings with ==. It compares object references. Always use equals().