Thursday, 16 January 2014

Selenium Data Driven | Properties File

The Properties file lets you store the keywords for data driven tests. Here, the Properties file acts as a data-source; it is used to handle small data files.

1| Open Eclipse IDE
2| Expand the project you are working
3| Right click on the src folder
4| New > Other... > General > File
5| Name the file with extension .properties. e.g., config.properties
6| Copy & paste the following and replace the keywords

search=keyword1,keyword2,keyword3

7| Save the properties file now.

Snippet

import java.util.ResourceBundle;
import java.util.StringTokenizer;

driver = new FirefoxDriver();
baseUrl = "http://xyz.com";

ResourceBundle bundle = ResourceBundle.getBundle("config");
String Channel = bundle.getString("search");
StringTokenizer st = new StringTokenizer(Channel, ",");
while (st.hasMoreTokens()) {
String value = st.nextToken();
driver.get(baseUrl + "/");
driver.findElement(By.id("")).click();
driver.findElement(By.id("")).sendKeys(value);
}


Alternate way to fetch Properties file

#Example1
import java.io.FileInputStream;
import java.util.Properties;

driver = new FirefoxDriver();
driver.get("http://xyz.com/");
FileInputStream fs = new FileInputStream("C:\\config.properties");
Properties prop = new Properties();
prop.load(fs);

String value = prop.getProperty("search");
System.out.println(prop.getProperty("search"));
driver.findElement(By.id("")).click();
driver.findElement(By.id("")).sendKeys(value);


#Example2
import java.io.FileInputStream;
import java.util.Properties;

driver = new FirefoxDriver();
driver.get("http://xyz.com/");
FileInputStream fs = new FileInputStream("C:\\config.properties");
Properties prop = new Properties();
prop.load(fs);

System.out.println(prop.getProperty("path"));
String dd = prop.getProperty("search");
System.out.println(prop.getProperty("search"));
driver.findElement(By.id(prop.getProperty("path"))).click();
driver.findElement(By.id("")).sendKeys(dd);

Let the config.properties File be
path=//*[@id=’clickme’]
search=keyword1

4 comments:

  1. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete
  2. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  3. I think, this is really very well and information here about this blog.
    1bhk Flats for Rent in T Nagar

    ReplyDelete