Reading and Writing Properties Files

Copy input.properties

Copy output.properties


package com.readingwriting.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import utility.Helper;

public class PropertiesDemo {
    static WebDriver driver;
    public static void main(String[] args) {
        //Reading from a Property File
        //Before running the Script create a file input.properties and 
        //give some Keys and values pairs seperated by =
        try {
            Properties prop = new Properties();
            FileInputStream fis = new FileInputStream("input.properties");
            prop.load(fis);
            
            driver = Helper.launchBrowser(prop.getProperty("browser"));
            driver.get(prop.getProperty("url"));
            
            driver.findElement(By.id(prop.getProperty("usernameTextId")))
                                            .sendKeys(prop.getProperty("username"));
            
            driver.findElement(By.id(prop.getProperty("passwordTextId")))
                                            .sendKeys(prop.getProperty("password"));
            
            driver.findElement(By.id(prop.getProperty("loginButtonId"))).click();
            
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        //Writing to a Properties File
        try {
            Properties prop = new Properties();
            FileOutputStream fos = new FileOutputStream("output.properties");
            prop.setProperty("google""www.google.co.in");
            prop.setProperty("facebook""www.facebook.com");
            prop.setProperty("selenium""www.seleniumhq.org");
        
            prop.store(fos, "Important Links");
            
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
}

No comments:

Post a Comment