How to access prop.getProperty from any Project Class

0

I created a " Messages " class and put the public Properties getProp() method inside it, because I want to access the config.properties file from any other class in my project.

/**
 * @author Alang
 * Criado em 18/05/2017
 */
package com.pub.Utilities;

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

public class Messages {

    public Properties getProp() throws IOException
    {

        Properties props = new Properties();
        FileInputStream file = new FileInputStream("../PubProject/properties/config.properties");
        props.load(file);
        return props;

    }

}

The problem that is occurring is that I do not know how to do this. So as I've tried several ways it always gives a different error.

For example I created another class called: public class Utilities {...} and within this class I have several methods.

How can I load the value of the Config.strBrowserType parameter that is in the config.properties file, within this getTitle() method that is part of the Utilities class?

Here is my Utilities class with only the getTitle(String strTestStep) method.

package com.pub.Utilities;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

/**
 * @author Alang
 *
 */
public class Utilities {

    //*** INSTACES  *****************************
    Properties prop = getProp();

    //*** VARIABLES *****************************
    private WebDriver wd;


    public void getBrowserTitle(String strTestStep){

        String strExpectedTitle = prop.getProperty("Config.strBrowserType");
        String strActualTitle = wd.getTitle();

        if (strExpectedTitle.equals(strActualTitle){
            System.out.println(strTestStep + " - PASSED: Application was loaded according expected");
        }else{
            System.out.println(strTestStep + " - FAILED: Application wasn't loaded according expected");
        }

    }

}

How can I solve this problem?

    
asked by anonymous 18.05.2017 / 04:17

1 answer

1

You can do it in two ways:

1st Way - Static

Leave your method static

public static Properties getProp() throws IOException

Use it anywhere in the application this way

Messages.getProp();

Disadvantage: Whenever you call this method you will be creating objects in memory (Properties and FileInputStream) unnecessarily, so I suggest the 2nd Way

2nd Way - Singleton

public final class Messages {

      private Properties props = null;

public synchronized static Properties getProp() throws IOException {
    if(props == null) {
        props = new Properties();
        FileInputStream file = new FileInputStream("../PubProject/properties/config.properties");
        props.load(file);
        return props;
    }
    return props;

}
}

To use it is the same way.

Messages.getProp();
    
18.05.2017 / 16:44