Configuration management 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 configuration file config = new Properties(); FileInputStream fis = new FileInputStream("config.properties"); config.load(fis); fis.close(); // Setup WebDriver System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Open login page driver.get(config.getProperty("login.url")); } @Test public void testLogin() { // Locate username field and enter username WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameInput.sendKeys(config.getProperty("login.username")); // Locate password field and enter password WebElement passwordInput = driver.findElement(By.id("password")); passwordInput.sendKeys(config.getProperty("login.password")); // Click login button WebElement loginButton = driver.findElement(By.id("loginBtn")); loginButton.click(); // Wait for dashboard page to load wait.until(ExpectedConditions.urlContains("/dashboard")); // Assertions String currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("/dashboard"), "URL does not contain /dashboard"); String expectedTitle = config.getProperty("dashboard.title"); String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, expectedTitle, "Dashboard page title mismatch"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test script uses Selenium WebDriver with TestNG to automate the login functionality.
Setup: The @BeforeClass method loads the configuration file config.properties which contains the login URL, username, password, and expected dashboard title. It then initializes the ChromeDriver and opens the login page.
Test: The test method waits explicitly for the username field to be visible, then enters the username and password read from the config file. It clicks the login button and waits until the URL contains '/dashboard' to confirm navigation.
Assertions: It verifies the URL contains '/dashboard' and the page title matches the expected dashboard title from the config file.
Teardown: The @AfterClass method closes the browser to clean up.
This approach follows best practices by separating configuration data, using explicit waits, and structuring code with TestNG annotations.
Now add data-driven testing with 3 different username and password combinations from the configuration file
Practice
Solution
Step 1: Understand configuration management role
It separates configuration data like URLs, credentials from the test logic.Step 2: Benefits of separation
This separation makes tests easier to maintain and adapt to different environments.Final Answer:
To separate test settings from test code for easier maintenance -> Option AQuick Check:
Configuration management = separation of settings and code [OK]
- Confusing configuration management with test execution
- Thinking it speeds up writing tests directly
- Assuming it reduces assertions
Solution
Step 1: Recall Java constant syntax
In Java, constants use 'public static final' with type and value in double quotes.Step 2: Check each option
public static final String BASE_URL = "https://example.com"; uses correct syntax; others have syntax errors or wrong keywords.Final Answer:
public static final String BASE_URL = "https://example.com"; -> Option CQuick Check:
Java constants = public static final + type + value [OK]
- Using single quotes for strings in Java
- Omitting type declaration
- Using 'const' which is not Java keyword
public class Config {
public static final String ENV = "staging";
public static final String URL = ENV.equals("production") ? "https://prod.example.com" : "https://staging.example.com";
public static void main(String[] args) {
System.out.println(URL);
}
}Solution
Step 1: Evaluate ENV value
ENV is "staging", so ENV.equals("production") is false.Step 2: Apply ternary operator
Since condition is false, URL is assigned "https://staging.example.com".Final Answer:
https://staging.example.com -> Option DQuick Check:
Condition false -> staging URL printed [OK]
- Assuming ENV equals production
- Confusing ternary true/false branches
- Expecting compilation error due to static final
public class Config {
public static final String BASE_URL = https://example.com;
}Solution
Step 1: Check string assignment syntax
In Java, string literals must be enclosed in double quotes.Step 2: Identify missing quotes
The URL is not in quotes, causing a syntax error.Final Answer:
Missing double quotes around the URL string -> Option BQuick Check:
String literals need quotes in Java [OK]
- Forgetting quotes around string values
- Thinking static or final keywords cause error here
- Ignoring Java naming conventions (not error)
Solution
Step 1: Understand maintainability needs
Hardcoding or separate classes cause duplication and errors.Step 2: Use external properties files
Loading settings from files allows switching environments without code changes.Final Answer:
Use external properties files to load environment-specific settings -> Option AQuick Check:
External config files = flexible environment switching [OK]
- Hardcoding URLs inside test code
- Creating duplicate test classes per environment
- Using random URLs which cause flaky tests
Ready to prove it?
Pass the test and this pattern moves unknown
