What if you could change test settings without touching a single line of code?
Why Properties file for configuration in Selenium Java? - Purpose & Use Cases
Imagine you have a test script that needs to run on different websites, browsers, and user accounts. Every time you want to change the browser or URL, you open the code and change values directly inside it.
This means you must edit and recompile your test code for every small change.
Changing values inside the code is slow and risky. You might accidentally break the code or forget to change all places. It's hard to keep track of settings, and sharing tests with others becomes confusing.
Manual updates cause errors and waste time.
Using a properties file lets you keep all settings like URLs, usernames, and browser types outside the code. Your test script reads these values at runtime.
This means you can change configurations anytime without touching the code, making tests flexible and easy to maintain.
String url = "http://testsite.com"; String browser = "chrome";
Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); String url = prop.getProperty("url"); String browser = prop.getProperty("browser");
It enables quick, safe changes to test settings without rewriting code, making automation scalable and less error-prone.
When running tests on a new website version, you just update the URL in the properties file. Your test script automatically uses the new URL without any code changes.
Manual code changes for settings are slow and risky.
Properties files separate configuration from code.
This makes tests easier to update, share, and maintain.
Practice
.properties file in Selenium Java tests?Solution
Step 1: Understand the role of properties files
Properties files are used to keep configuration data separate from code, such as URLs, usernames, and passwords.Step 2: Identify the correct purpose in Selenium tests
Using properties files makes tests flexible and easier to maintain by avoiding hard-coded values.Final Answer:
To store configuration data like URLs and credentials outside the test code -> Option CQuick Check:
Properties file = external config storage [OK]
- Thinking properties files contain test scripts
- Confusing properties files with logs or reports
- Assuming properties files compile code
.properties file in Selenium tests?Solution
Step 1: Identify the class for properties handling
ThePropertiesclass in Java is designed to load and read key-value pairs from.propertiesfiles.Step 2: Differentiate from other classes
WhileFileReaderandFileInputStreamhelp read files,Propertiesspecifically loads and manages properties data.Final Answer:
Properties -> Option AQuick Check:
Java Properties class loads config files [OK]
- Using FileReader alone without Properties
- Confusing BufferedReader with Properties
- Trying to load properties without Properties class
config.properties file contains url=https://example.com?
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load(fis);
System.out.println(prop.getProperty("url"));Solution
Step 1: Load properties from file
ThePropertiesobject loads key-value pairs from the file usingload().Step 2: Retrieve the value for key 'url'
CallinggetProperty("url")returns the value associated with 'url', which is 'https://example.com'.Final Answer:
https://example.com -> Option BQuick Check:
getProperty('url') = 'https://example.com' [OK]
- Expecting the whole line instead of just the value
- Confusing key with value in output
- Not loading the file before getting property
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load();
System.out.println(prop.getProperty("browser"));Solution
Step 1: Check method usage for loading properties
Theload()method requires an InputStream argument, but here it is called without any argument.Step 2: Verify other parts of the code
TheFileInputStreamis correctly created,Propertiesis instantiated, andgetPropertyis spelled correctly.Final Answer:
Missing argument in prop.load() method -> Option AQuick Check:
load() needs InputStream argument [OK]
- Calling load() without argument
- Assuming load() reads file automatically
- Ignoring exceptions from FileInputStream
browser value in a config.properties file. Which approach correctly reads this value and uses it to launch Chrome or Firefox browsers?Solution
Step 1: Load the properties file and read the 'browser' key
UsePropertiesclass to load the file and get the value of 'browser' key.Step 2: Use conditional logic to launch the correct browser driver
If the value is 'chrome', create aChromeDriverinstance; if 'firefox', create aFirefoxDriverinstance.Final Answer:
Load properties, get 'browser' value, then use if-else to create ChromeDriver or FirefoxDriver accordingly -> Option DQuick Check:
Config-driven browser choice = if-else on getProperty('browser') [OK]
- Ignoring properties file and hardcoding browser
- Not using conditional logic for browser selection
- Loading properties but not reading the key
Ready to prove it?
Pass the test and this pattern moves unknown
