How to access prop.getProperty from any Project Class

Asked

Viewed 116 times

0

I created a class "Messages" and put the Method public Properties getProp() inside it, because I want to access the file config.properties from any other class of 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 don’t know how to do this. So as I’ve tried several ways 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 parameter value Config.strBrowserType that’s in the file config.properties, within this method getTitle() that is part of the class Utilities?

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

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?

  • You cannot access the method anywhere or cannot read the file configuration ?

  • I can read the contents of the file, I can’t access the method from another class. I wanted the getProp method that lies within the Messages class below to be accessible in any other class. public class Messages { public Properties getProp() throws Ioexception { Properties props = new Properties(); Fileinputstream file = new Fileinputstream("./Pubproject/properties/config.properties"); props.load(file); turn props; } } .

1 answer

1

You can do it two ways:

1st Way - Static

Leave your way static

public static Properties getProp() throws IOException

use it anywhere in the application this way

Messages.getProp();

Disadvantage: Every time 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 is the same way.

Messages.getProp();
  • If you can explain the synchronized and the static would be interesting to your answer

  • Hello Erick... good morning. To use in another class I am doing the following: I instate the Messages class in the class I want to use //*** INSTANCES ************************************************
 Messages wd = new Messages();

 wd.getProp(); Would that be right? Thank you very much.

  • You don’t need to create an interface, just use Messager.getProp();

  • 1

    It worked... all right. Thank you very much.

  • If it helped you can mark the answer as accepted =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.