To use configuration file in Selenium Webdriver

package testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import utility.configReader;

public class googleTestwithConfigFile {
	
	@Test
	public void chromeTest() throws Exception
	{	
		configFile config = new configFile();
		
		System.setProperty("webdriver.chrome.driver", config.getChromePath());
		
		WebDriver driver = new ChromeDriver();
		
		driver.get(config.getApplicationURL());
		
		driver.quit();
	}

}

Configuration File

package utility;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class configReader {
	
	Properties pro;
	
	public configFile()
	{
		try {
			File source = new File ("./configuration/configproperties");
			
			FileInputStream input = new FileInputStream(source);
			
			pro = new Properties();
			
			pro.load(input);
			
		} catch (Exception exp) {
			
			System.out.println("Exception is: ---"+exp.getMessage());
		} 
	}
	public String getChromePath()
	{
		String path = pro.getProperty("ChromeDriver");
		
		return path;
	}
	public String getApplicationURL()
	{
		return pro.getProperty("URL");
	}

}

configPropertiesFile

ChromeDriver = E:\\Selenium\\chromedriver.exe

URL=http://www.google.com

 

One comment

  • Constructor should be named same as the name of the class. So, public configFile() should be public configReader()

Leave a Reply

Your email address will not be published. Required fields are marked *