Playwright + Java · Lesson 9 of 38

0/38 done

Module 9: Java File & Data Handling

9. Configuration Properties & File Management

14 minlesson

Try it yourself

Implement ConfigReader mock helper:

  1. Create a method getOrDefault(Properties props, String key, String defaultValue).
  2. Return props.getProperty(key) if not null, otherwise return defaultValue.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Java Properties Files

Store configuration in config.properties:

properties
base.url=https://targetqa.com
browser=chromium
headless=true
timeout=30

Read dynamically in Java:

java
import java.io.*;
import java.util.Properties;

public class ConfigReader {
    private static Properties properties = new Properties();

    public static void loadConfig(String filePath) throws IOException {
        try (InputStream input = new FileInputStream(filePath)) {
            properties.load(input);
        }
    }

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }
}

Key Takeaways & Walkthrough Summary

Hardcoded values ruin test suites. Professional frameworks store configurations in properties files, read credentials from local secrets, and log artifacts to disk. In this lesson, you will master reading files, loading Properties, and writing test logs. Let's get hands-on!

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 use try-with-resources when opening files?

try (InputStream is = ...) automatically closes the stream even if an exception occurs, preventing file handle leaks.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

Java
Skip