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