Bird
Raised Fist0
Selenium Javatesting~15 mins

Configuration management in Selenium Java - Build an Automation Script

Choose your learning style10 modes available
Verify login functionality using configuration management for test data
Preconditions (2)
Step 1: Open the browser and navigate to the login page URL
Step 2: Read username and password from the configuration file
Step 3: Enter the username into the username input field
Step 4: Enter the password into the password input field
Step 5: Click the login button
Step 6: Wait for the dashboard page to load
✅ Expected Result: User is successfully logged in and dashboard page is displayed
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the dashboard page title is as expected
Best Practices:
Use Page Object Model to separate page locators and actions
Use explicit waits to wait for elements or page load
Load test data from an external configuration file (e.g., properties file)
Use TestNG annotations for setup and teardown
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Hardcoding test data like username and password inside the test code
Using Thread.sleep() instead of explicit waits
Locating elements with brittle XPath expressions
Not closing the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations from the configuration file

Show Hint

Practice

(1/5)
1. What is the main purpose of configuration management in Selenium Java testing?
easy
A. To separate test settings from test code for easier maintenance
B. To write test cases faster by skipping setup
C. To increase the number of test cases automatically
D. To reduce the number of test assertions

Solution

  1. Step 1: Understand configuration management role

    It separates configuration data like URLs, credentials from the test logic.
  2. Step 2: Benefits of separation

    This separation makes tests easier to maintain and adapt to different environments.
  3. Final Answer:

    To separate test settings from test code for easier maintenance -> Option A
  4. Quick Check:

    Configuration management = separation of settings and code [OK]
Hint: Think: settings outside code for easy updates [OK]
Common Mistakes:
  • Confusing configuration management with test execution
  • Thinking it speeds up writing tests directly
  • Assuming it reduces assertions
2. Which of the following is a correct way to define a constant for a base URL in Selenium Java?
easy
A. final BASE_URL = https://example.com;
B. static String BASE_URL = 'https://example.com';
C. public static final String BASE_URL = "https://example.com";
D. const BASE_URL = "https://example.com";

Solution

  1. Step 1: Recall Java constant syntax

    In Java, constants use 'public static final' with type and value in double quotes.
  2. Step 2: Check each option

    public static final String BASE_URL = "https://example.com"; uses correct syntax; others have syntax errors or wrong keywords.
  3. Final Answer:

    public static final String BASE_URL = "https://example.com"; -> Option C
  4. Quick Check:

    Java constants = public static final + type + value [OK]
Hint: Remember Java constants need public static final [OK]
Common Mistakes:
  • Using single quotes for strings in Java
  • Omitting type declaration
  • Using 'const' which is not Java keyword
3. Given the following code snippet, what will be printed?
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);
    }
}
medium
A. https://prod.example.com
B. null
C. Compilation error
D. https://staging.example.com

Solution

  1. Step 1: Evaluate ENV value

    ENV is "staging", so ENV.equals("production") is false.
  2. Step 2: Apply ternary operator

    Since condition is false, URL is assigned "https://staging.example.com".
  3. Final Answer:

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

    Condition false -> staging URL printed [OK]
Hint: Check condition in ternary operator carefully [OK]
Common Mistakes:
  • Assuming ENV equals production
  • Confusing ternary true/false branches
  • Expecting compilation error due to static final
4. Identify the error in this configuration code snippet:
public class Config {
    public static final String BASE_URL = https://example.com;
}
medium
A. BASE_URL should not be static
B. Missing double quotes around the URL string
C. final keyword is not allowed here
D. Class name should be lowercase

Solution

  1. Step 1: Check string assignment syntax

    In Java, string literals must be enclosed in double quotes.
  2. Step 2: Identify missing quotes

    The URL is not in quotes, causing a syntax error.
  3. Final Answer:

    Missing double quotes around the URL string -> Option B
  4. Quick Check:

    String literals need quotes in Java [OK]
Hint: Strings always need double quotes in Java [OK]
Common Mistakes:
  • Forgetting quotes around string values
  • Thinking static or final keywords cause error here
  • Ignoring Java naming conventions (not error)
5. You want to run the same Selenium tests on different environments (dev, test, prod) without changing code. Which configuration management approach is best?
hard
A. Use external properties files to load environment-specific settings
B. Hardcode URLs in test methods and change before each run
C. Write separate test classes for each environment
D. Use random URLs in tests to cover all environments

Solution

  1. Step 1: Understand maintainability needs

    Hardcoding or separate classes cause duplication and errors.
  2. Step 2: Use external properties files

    Loading settings from files allows switching environments without code changes.
  3. Final Answer:

    Use external properties files to load environment-specific settings -> Option A
  4. Quick Check:

    External config files = flexible environment switching [OK]
Hint: Use external files for easy environment changes [OK]
Common Mistakes:
  • Hardcoding URLs inside test code
  • Creating duplicate test classes per environment
  • Using random URLs which cause flaky tests
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