Bird
Raised Fist0
Selenium Javatesting~20 mins

Properties file for configuration in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Properties File Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this Java code reading a properties file?
Given the following Java code snippet that reads a properties file, what will be printed if the properties file contains
url=https://example.com
timeout=30
?
Selenium Java
Properties prop = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
    prop.load(fis);
} catch (IOException e) {
    System.out.println("Error loading properties");
}
System.out.println(prop.getProperty("url") + "," + prop.getProperty("timeout"));
Anull,null
Burl,timeout
Chttps://example.com,30
DError loading properties
Attempts:
2 left
💡 Hint
Check how Properties.getProperty() returns values from the loaded file.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the loaded property value?
You have loaded a properties file with a key "browser" set to "chrome". Which assertion correctly checks this in a JUnit test?
Selenium Java
Properties prop = new Properties();
prop.setProperty("browser", "chrome");
AassertNull(prop.getProperty("browser"));
BassertEquals("chrome", prop.getProperty("browser"));
CassertFalse(prop.getProperty("browser").equals("chrome"));
DassertTrue(prop.getProperty("browser") == "chrome");
Attempts:
2 left
💡 Hint
Use assertEquals to compare string values in JUnit.
❓ locator
advanced
1:30remaining
Identify the best locator to find the properties file in a Selenium Java project
In a Selenium Java project, where should the properties file be placed to be loaded with this code snippet?
FileInputStream fis = new FileInputStream("src/test/resources/config.properties");
APlace config.properties inside src/test/resources folder
BPlace config.properties inside src/main/java folder
CPlace config.properties inside src/test/java folder
DPlace config.properties inside the project root folder
Attempts:
2 left
💡 Hint
The path in FileInputStream is relative to the project root.
🔧 Debug
advanced
2:00remaining
Why does this code throw FileNotFoundException when loading properties?
Consider this code snippet:
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load(fis);

The file config.properties exists in src/test/resources but the code throws FileNotFoundException. Why?
AFileInputStream requires absolute path, relative paths are invalid
BProperties.load() cannot read files from resources folder
CThe properties file must have .txt extension to be loaded
DThe relative path "config.properties" does not point to src/test/resources, so file is not found
Attempts:
2 left
💡 Hint
Check the working directory and relative path used in FileInputStream.
❓ framework
expert
2:30remaining
How to load properties file using class loader in Selenium Java framework?
Which code snippet correctly loads a properties file named "config.properties" placed in src/test/resources using the class loader in a Selenium Java framework?
A
Properties prop = new Properties();
try (InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties")) {
    prop.load(input);
}
B
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("src/test/resources/config.properties");
prop.load(fis);
C
Properties prop = new Properties();
InputStream input = new FileInputStream("config.properties");
prop.load(input);
D
Properties prop = new Properties();
prop.load("config.properties");
Attempts:
2 left
💡 Hint
Use getResourceAsStream to load files from classpath safely.

Practice

(1/5)
1. What is the main purpose of using a .properties file in Selenium Java tests?
easy
A. To compile Java source files automatically
B. To write the test cases in a different programming language
C. To store configuration data like URLs and credentials outside the test code
D. To store screenshots taken during test execution

Solution

  1. 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.
  2. Step 2: Identify the correct purpose in Selenium tests

    Using properties files makes tests flexible and easier to maintain by avoiding hard-coded values.
  3. Final Answer:

    To store configuration data like URLs and credentials outside the test code -> Option C
  4. Quick Check:

    Properties file = external config storage [OK]
Hint: Properties files hold config, not code or screenshots [OK]
Common Mistakes:
  • Thinking properties files contain test scripts
  • Confusing properties files with logs or reports
  • Assuming properties files compile code
2. Which Java class is commonly used to load a .properties file in Selenium tests?
easy
A. Properties
B. FileReader
C. BufferedReader
D. FileInputStream

Solution

  1. Step 1: Identify the class for properties handling

    The Properties class in Java is designed to load and read key-value pairs from .properties files.
  2. Step 2: Differentiate from other classes

    While FileReader and FileInputStream help read files, Properties specifically loads and manages properties data.
  3. Final Answer:

    Properties -> Option A
  4. Quick Check:

    Java Properties class loads config files [OK]
Hint: Use Properties class to load .properties files [OK]
Common Mistakes:
  • Using FileReader alone without Properties
  • Confusing BufferedReader with Properties
  • Trying to load properties without Properties class
3. Given the following code snippet, what will be the output if the 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"));
medium
A. null
B. https://example.com
C. url=https://example.com
D. FileNotFoundException

Solution

  1. Step 1: Load properties from file

    The Properties object loads key-value pairs from the file using load().
  2. Step 2: Retrieve the value for key 'url'

    Calling getProperty("url") returns the value associated with 'url', which is 'https://example.com'.
  3. Final Answer:

    https://example.com -> Option B
  4. Quick Check:

    getProperty('url') = 'https://example.com' [OK]
Hint: getProperty(key) returns the value for that key [OK]
Common Mistakes:
  • Expecting the whole line instead of just the value
  • Confusing key with value in output
  • Not loading the file before getting property
4. Identify the error in the following code snippet that tries to load a properties file:
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load();
System.out.println(prop.getProperty("browser"));
medium
A. Missing argument in prop.load() method
B. FileInputStream path is incorrect
C. Properties object not instantiated
D. getProperty method is misspelled

Solution

  1. Step 1: Check method usage for loading properties

    The load() method requires an InputStream argument, but here it is called without any argument.
  2. Step 2: Verify other parts of the code

    The FileInputStream is correctly created, Properties is instantiated, and getProperty is spelled correctly.
  3. Final Answer:

    Missing argument in prop.load() method -> Option A
  4. Quick Check:

    load() needs InputStream argument [OK]
Hint: Always pass InputStream to load() method [OK]
Common Mistakes:
  • Calling load() without argument
  • Assuming load() reads file automatically
  • Ignoring exceptions from FileInputStream
5. You want to switch browsers in your Selenium tests by changing only the browser value in a config.properties file. Which approach correctly reads this value and uses it to launch Chrome or Firefox browsers?
hard
A. Use getProperty('browser') but always launch ChromeDriver regardless of value
B. Hardcode browser choice in test code and ignore properties file
C. Load properties but never read 'browser' key, just launch FirefoxDriver
D. Load properties, get 'browser' value, then use if-else to create ChromeDriver or FirefoxDriver accordingly

Solution

  1. Step 1: Load the properties file and read the 'browser' key

    Use Properties class to load the file and get the value of 'browser' key.
  2. Step 2: Use conditional logic to launch the correct browser driver

    If the value is 'chrome', create a ChromeDriver instance; if 'firefox', create a FirefoxDriver instance.
  3. Final Answer:

    Load properties, get 'browser' value, then use if-else to create ChromeDriver or FirefoxDriver accordingly -> Option D
  4. Quick Check:

    Config-driven browser choice = if-else on getProperty('browser') [OK]
Hint: Use if-else on property value to pick browser driver [OK]
Common Mistakes:
  • Ignoring properties file and hardcoding browser
  • Not using conditional logic for browser selection
  • Loading properties but not reading the key
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard