0
0
Selenium Javatesting~5 mins

Configuration management in Selenium Java

Choose your learning style9 modes available
Introduction

Configuration management helps keep test settings organized and easy to change. It makes tests flexible and reusable.

When you want to run the same test on different browsers like Chrome and Firefox.
When you need to change the website URL without editing every test.
When multiple testers share the same test scripts but use different settings.
When you want to keep sensitive data like usernames and passwords separate from test code.
When you want to run tests in different environments like development, staging, and production.
Syntax
Selenium Java
public class Config {
    public static final String BASE_URL = "https://example.com";
    public static final String BROWSER = "chrome";
    public static final int TIMEOUT = 10;
}
Use constants or properties files to store configuration values.
Keep configuration separate from test logic for easy updates.
Examples
This example sets the base URL and browser type for tests.
Selenium Java
public class Config {
    public static final String BASE_URL = "https://testsite.com";
    public static final String BROWSER = "firefox";
}
This example loads configuration from a properties file, allowing easy changes without code edits.
Selenium Java
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
    props.load(fis);
}
String url = props.getProperty("baseUrl");
Sample Program

This test uses configuration to choose the browser and URL. It opens the page and prints the title.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

class Config {
    public static final String BASE_URL = "https://example.com";
    public static final String BROWSER = "chrome";
}

public class TestLogin {
    public static void main(String[] args) {
        WebDriver driver;
        switch (Config.BROWSER.toLowerCase()) {
            case "chrome":
                driver = new ChromeDriver();
                break;
            case "firefox":
                driver = new FirefoxDriver();
                break;
            default:
                throw new IllegalArgumentException("Unsupported browser: " + Config.BROWSER);
        }
        driver.get(Config.BASE_URL);
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
    }
}
OutputSuccess
Important Notes

Always keep sensitive data like passwords out of code and use secure storage.

Use descriptive names for configuration keys to avoid confusion.

Test configuration changes carefully to avoid breaking tests.

Summary

Configuration management separates settings from test code.

It makes tests easier to maintain and run in different environments.

Use constants or external files to store configuration values.