[JVM] the best way to load properties

879 views
Skip to first unread message

Javix

unread,
Sep 25, 2012, 4:58:11 AM9/25/12
to cu...@googlegroups.com

What is the best way to load properties file so that to use needed properties (like host url user name, etc.) all around the steps definitions when using Selenium WebDriver?.
I define some profiles in POM file as follows:
...
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber.options>--format pretty</cucumber.options>               
    </properties>
<profiles>
        <profile>
            <id>dev</id>  
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <host>htp:some.host</host>
                <admin.username>dmin</admin.username>
                <admin.password>dmin</admin.password>
            </properties>                                 
        </profile>  
<profiles>
....

I created a property file names app.properties in my_app/src/main/resources.

And then I'd like to get a property as follows (by calling somewhere the 'initProperties' method):

private Properties properties;

private void initProperties(){

properties = new Properties();
        try {
            properties.load(getClass().getResourceAsStream("/app.properties"));
        } catch (IOException ex) {
            Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

If I define the 'initProperties' method in a 'before' hook somewhere, so it will be run before each scenario? Right? It seems as not very perfect, as normally, the properties should be loaded only once.
Thank you.

MIC

unread,
Sep 25, 2012, 5:08:12 AM9/25/12
to cu...@googlegroups.com
just define a variable to mark it if you had loaded property!


boolean loaded = false

if(! loaded){

Javix

unread,
Sep 25, 2012, 5:15:32 AM9/25/12
to cu...@googlegroups.com
I'm trying to use SharedDriver instance as in supplied cucumber-jvm examples (defined in java-webbit-websockets-selenium' folder). So would it be correct to initialize the properties once in SharedDriver constructor:

public SharedDriver() {
        super(REAL_DRIVER);
        initProperties();
        REAL_DRIVER.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }   
   
    private void initProperties(){
        properties = new Properties();
        try {
            properties.load(getClass().getResourceAsStream("/app.properties"));
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }

And get the properties later in no matter which Stepdefinition class as follows:
class MyStepDefinitions {

private Properties properties;

public MyStepDefinitions(SharedDriver driver) {
        this.properties = driver.getProperties();
        this.driver = driver;       
    }

}

Like that I'd be able to use properties all around the step definitions in the class. Or there is a better solution?

aslak hellesoy

unread,
Sep 25, 2012, 7:09:11 AM9/25/12
to cu...@googlegroups.com


On Tuesday, September 25, 2012, Javix wrote:
I'm trying to use SharedDriver instance as in supplied cucumber-jvm examples (defined in java-webbit-websockets-selenium' folder). So would it be correct to initialize the properties once in SharedDriver constructor:

public SharedDriver() {
        super(REAL_DRIVER);
        initProperties();
        REAL_DRIVER.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }   
   
    private void initProperties(){
        properties = new Properties();
        try {
            properties.load(getClass().getResourceAsStream("/app.properties"));
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }

And get the properties later in no matter which Stepdefinition class as follows:
class MyStepDefinitions {

private Properties properties;

public MyStepDefinitions(SharedDriver driver) {
        this.properties = driver.getProperties();
        this.driver = driver;       
    }

}

Like that I'd be able to use properties all around the step definitions in the class. Or there is a better solution?

Looks ok to me except that initProperties shouldn't log exceptions, it should declare throws IOException, as should the SharedDriver ctor.

Never catch an exception if you can't recover from the situation. It's better to let it bubble through and fail.

Aslak
 
--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
 
 

aslak hellesoy

unread,
Sep 25, 2012, 7:13:56 AM9/25/12
to cu...@googlegroups.com


On Tuesday, September 25, 2012, Javix wrote:
Nothing wrong with loading it several times unless you _know_ it's a performance bottleneck (I bet it's more than 5 orders of magnitude faster than your total execution time). Premature optimisation is the root of all evil. Keep it simple.

Aslak

Javix

unread,
Sep 25, 2012, 7:15:20 AM9/25/12
to cu...@googlegroups.com
Thank you all for the precious guide lines :).
Reply all
Reply to author
Forward
0 new messages