How to Read Configuration from Properties Files Using PropertiesHelpers in the Selenium Automation Framework
Use the static PropertiesHelpers utility class in anhtester/automationframeworkselenium to load, retrieve, and update Java .properties files without writing manual I/O code.
The Automation Framework Selenium (hosted at anhtester/automationframeworkselenium) centralizes environment-specific settings—such as URLs, browser types, and wait times—inside standard Java properties files. The PropertiesHelpers class, located at src/main/java/com/anhtester/helpers/PropertiesHelpers.java, exposes static methods that handle file loading, UTF-8 encoding conversion, and runtime updates, letting test classes access configuration through simple one-line calls.
Understanding the PropertiesHelpers Architecture
The PropertiesHelpers utility manages a static java.util.Properties instance cached in the private field properties. This design ensures that once files are loaded, subsequent reads operate from memory rather than hitting the disk repeatedly. The class also maintains a linkFile string that stores the absolute path to the default configuration file, resolved via SystemHelpers.getCurrentDir().
All methods are static, so you never instantiate the class; you simply call PropertiesHelpers.methodName() from any test class.
Loading Properties Files
The framework supports two loading strategies depending on whether you need the entire configuration suite or just the primary defaults.
Load All Configuration Files
The loadAllFiles() method merges multiple properties files into a single Properties object. It reads the hard-coded list including config.properties, data.properties, and CRM locator files from src/test/resources/config/.
// Load and merge all framework properties at suite startup
PropertiesHelpers.loadAllFiles();
Load Only the Default File
If your test requires only the primary configuration, call setDefaultFile(). This loads src/test/resources/config/config.properties and initializes the linkFile reference used by later write operations.
// Initialize with only the default config file
PropertiesHelpers.setDefaultFile();
Both methods automatically resolve absolute paths using the project directory, ensuring compatibility across different execution environments.
Retrieving Configuration Values
Fetch any property value using the getValue(String key) method. The implementation follows a lazy-loading pattern: if properties is null, it automatically invokes setDefaultFile() before looking up the key.
// Retrieve the CRM application URL
String crmUrl = PropertiesHelpers.getValue("URL_CRM");
driver.get(crmUrl);
// Get timeout settings
String timeout = PropertiesHelpers.getValue("PAGE_LOAD_TIMEOUT");
Internally, getValue performs three steps:
- Checks if the static
propertiesfield is initialized; if not, loads the default file - Retrieves the raw value using
properties.getProperty(key) - Returns the string processed through
LanguageUtils.convertCharset_ISO_8859_1_To_UTF8to ensure proper UTF-8 encoding for international characters
Updating Properties at Runtime
To modify configuration values dynamically—useful for switching browsers or toggling feature flags mid-suite—use the setValue(String key, String value) method. This writes the updated key-value pair back to the file referenced by linkFile using Properties.store().
// Override the browser type for the current test execution
PropertiesHelpers.setValue("BROWSER", "firefox");
// Verify the update immediately
String currentBrowser = PropertiesHelpers.getValue("BROWSER");
System.out.println("Browser configured as: " + currentBrowser);
The method ensures the default file is loaded before attempting any write operation, preventing null pointer exceptions.
Complete Test Implementation Example
Below is a practical test class demonstrating the full lifecycle: loading configuration, reading values, and updating settings at runtime.
import com.anhtester.helpers.PropertiesHelpers;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ConfigurationExampleTest extends BaseTest {
@BeforeClass
public void initializeConfig() {
// Load all properties files once before any test runs
PropertiesHelpers.loadAllFiles();
// Navigate using the configured URL
String applicationUrl = PropertiesHelpers.getValue("URL_CRM");
driver.get(applicationUrl);
}
@Test
public void verifyDynamicConfiguration() {
// Read original value
String originalBrowser = PropertiesHelpers.getValue("BROWSER");
System.out.println("Original browser: " + originalBrowser);
// Update configuration at runtime
PropertiesHelpers.setValue("BROWSER", "edge");
// Confirm the change persists in the properties object
String updatedBrowser = PropertiesHelpers.getValue("BROWSER");
assert updatedBrowser.equals("edge") : "Browser configuration update failed";
}
}
Configuration File Locations
The framework expects properties files in specific resource directories:
src/test/resources/config/config.properties– Primary framework settings (browser, URLs, timeouts)src/test/resources/config/data.properties– Auxiliary test data and credentialssrc/test/resources/objects/crm_locators.properties– UI element locators for the CRM module
All paths are resolved relative to the project root using SystemHelpers.getCurrentDir(), making the framework portable across CI/CD pipelines and local development environments.
Summary
- PropertiesHelpers (
src/main/java/com/anhtester/helpers/PropertiesHelpers.java) provides static methods for properties management in the anhtester/automationframeworkselenium repository. loadAllFiles()merges multiple configuration files;setDefaultFile()loads only the primary config.getValue(key)retrieves UTF-8 decoded values and auto-initializes the properties cache if empty.setValue(key, value)writes changes back to disk, enabling runtime configuration overrides.- All file paths are resolved dynamically via
SystemHelpers.getCurrentDir(), ensuring cross-platform compatibility.
Frequently Asked Questions
How does PropertiesHelpers handle file encoding?
The getValue method passes every retrieved value through LanguageUtils.convertCharset_ISO_8859_1_To_UTF8, ensuring that special characters and non-ASCII text stored in ISO-8859-1 format are correctly converted to UTF-8 before returning to the test code.
Can I load custom properties files not defined in the framework?
The current implementation in PropertiesHelpers.java hard-codes the file list within loadAllFiles(). To load additional custom files, you would need to extend the class or modify the loadAllFiles() method to accept a file path parameter, as the static properties field is private.
Is PropertiesHelpers thread-safe for parallel test execution?
Because the properties field is static and shared across the JVM, concurrent updates via setValue() in parallel threads could lead to race conditions. For thread-safe configuration in parallel execution, load properties once in a @BeforeSuite method and treat the configuration as read-only during test runs.
What happens if I call getValue before loading any files?
The getValue method implements lazy initialization: if the static properties variable is null, it automatically invokes setDefaultFile() to load src/test/resources/config/config.properties before attempting to retrieve the requested key.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →