Test Overview
This test reads a URL from a properties file and verifies that the browser navigates to the correct page. It checks the page title to confirm successful navigation.
This test reads a URL from a properties file and verifies that the browser navigates to the correct page. It checks the page title to confirm successful navigation.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.*; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ConfigPropertiesTest { static WebDriver driver; static Properties props = new Properties(); @BeforeAll public static void setup() throws IOException { FileInputStream fis = new FileInputStream("config.properties"); props.load(fis); System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testNavigateToUrlFromProperties() { String url = props.getProperty("url"); driver.get(url); String title = driver.getTitle(); Assertions.assertEquals("Example Domain", title); } @AfterAll public static void teardown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Load properties file 'config.properties' from file system | Properties object contains key 'url' with value 'https://example.com' | — | PASS |
| 2 | Set system property for ChromeDriver and launch Chrome browser | Chrome browser window opens, ready for commands | — | PASS |
| 3 | Navigate browser to URL read from properties file | Browser loads 'https://example.com' page | — | PASS |
| 4 | Get page title from browser | Page title is 'Example Domain' | Assert page title equals 'Example Domain' | PASS |
| 5 | Close browser and quit WebDriver | Browser window closes, WebDriver session ends | — | PASS |
.properties file in Selenium Java tests?.properties file in Selenium tests?Properties class in Java is designed to load and read key-value pairs from .properties files.FileReader and FileInputStream help read files, Properties specifically loads and manages properties data.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"));Properties object loads key-value pairs from the file using load().getProperty("url") returns the value associated with 'url', which is 'https://example.com'.Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load();
System.out.println(prop.getProperty("browser"));load() method requires an InputStream argument, but here it is called without any argument.FileInputStream is correctly created, Properties is instantiated, and getProperty is spelled correctly.browser value in a config.properties file. Which approach correctly reads this value and uses it to launch Chrome or Firefox browsers?Properties class to load the file and get the value of 'browser' key.ChromeDriver instance; if 'firefox', create a FirefoxDriver instance.Pass the test and this pattern moves unknown