Properties file for configuration in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.io.FileInputStream; import java.io.IOException; import java.time.Duration; import java.util.Properties; public class LoginTest { private WebDriver driver; private WebDriverWait wait; private Properties config; @BeforeClass public void setUp() throws IOException { // Load properties file config = new Properties(); FileInputStream fis = new FileInputStream("config.properties"); config.load(fis); fis.close(); // Setup WebDriver driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testLoginWithProperties() { driver.get("http://example.com/login"); // Enter username WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameInput.sendKeys(config.getProperty("username")); // Enter password WebElement passwordInput = driver.findElement(By.id("password")); passwordInput.sendKeys(config.getProperty("password")); // Click login button WebElement loginButton = driver.findElement(By.id("loginBtn")); loginButton.click(); // Wait for URL to change to dashboard wait.until(ExpectedConditions.urlToBe("http://example.com/dashboard")); // Assert URL String currentUrl = driver.getCurrentUrl(); Assert.assertEquals(currentUrl, "http://example.com/dashboard", "User should be redirected to dashboard after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test class uses Selenium WebDriver with TestNG to automate login using credentials from a properties file.
setUp(): Loads the config.properties file using Properties class and initializes the ChromeDriver and explicit wait.
testLoginWithProperties(): Opens the login page, waits for username input to be visible, enters username and password from the properties file, clicks login, waits until the URL changes to the dashboard URL, then asserts the URL is correct.
tearDown(): Closes the browser after the test finishes.
Explicit waits ensure elements are ready before interacting. Using properties file keeps credentials outside the code for easy updates.
Now add data-driven testing with 3 different username and password combinations from the properties file
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
