What is Properties?
It is mainly used to store configuration parameters of an application. Each parameter is stored as a pair of strings i.e. both key and value are of type string.
Advantage of using Properties?
If any value changes in the properties file then we don’t need to recompile the java class
How to use Properties Files with Selenium?
- It can be used to store all the configuration parameters like environment details, user credentials, database details, etc.… instead of hard coding it in the scripts
- It can be used as an object repository where we can store all the elements and their identifiers
Example:
public class FirstTest { @Test public void OpenBrowser() { String driverPath = PropertyFileUtils.loadFrameworkProperties().getProperty("chrome.driver.path"); String url = PropertyFileUtils.loadApplicationProperties().getProperty("application.url"); WebDriver driver; System.setProperty("webdriver.chrome.driver", driverPath); driver = new ChromeDriver(); driver.get(url); System.out.println(driver.getTitle()); Assert.assertTrue("Page title is not correct",driver.getTitle().equals("Google")); driver.quit(); } }
public class PropertyFileUtils { public static Properties loadProperties(String path){ Properties properties = new Properties(); FileInputStream file = null; try { file = new FileInputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } try { properties.load(file); } catch (IOException e) { e.printStackTrace(); } return properties; } public static Properties loadApplicationProperties(){ Properties properties = loadProperties("src\\test\\resources\\application.properties"); return properties; } public static Properties loadFrameworkProperties(){ Properties properties = loadProperties("src\\test\\resources\\framework.properties"); return properties; } }
application.properties
application.url=https://www.google.com
framework.properties
chrome.driver.path=src/main/resources/drivers/chromedriver.exe