Discussion:
[Cucumber] [JVM] the best way to load properties
(too old to reply)
Javix
2012-09-25 08:58:11 UTC
Permalink
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.
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
MIC
2012-09-25 09:08:12 UTC
Permalink
just define a variable to mark it if you had loaded property!


boolean loaded = false

if(! loaded){
try {

properties.load(getClass().getResourceAsStream("/app.properties"));
} catch (IOException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE,
null, ex);
}
}
Post by Javix
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?.
...
<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
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.
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
Javix
2012-09-25 09:15:32 UTC
Permalink
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?
Post by MIC
just define a variable to mark it if you had loaded property!
boolean loaded = false
if(! loaded){
try {
properties.load(getClass().getResourceAsStream("/app.properties"));
} catch (IOException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE,
null, ex);
}
}
Post by Javix
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?.
...
<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
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.
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
aslak hellesoy
2012-09-25 11:09:11 UTC
Permalink
Post by Javix
I'm trying to use SharedDriver instance as in supplied cucumber-jvm
examples (defined in java-webbit-websockets-selenium' folder). So would it
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
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
Post by Javix
Post by MIC
just define a variable to mark it if you had loaded property!
boolean loaded = false
if(! loaded){
try {
properties.load(getClass().**getResourceAsStream("/app.**
properties"));
} catch (IOException ex) {
Logger.getLogger(MyClass.**class.getName()).log(Level.**SEVERE,
null, ex);
}
}
Post by Javix
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?.
...
<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
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.
--
-- 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
To unsubscribe from this group, send email to
group at https://groups.google.com/d/forum/cukes?hl=en
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
aslak hellesoy
2012-09-25 11:13:56 UTC
Permalink
Post by Javix
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?.
...
<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
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.
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
Post by Javix
Thank you.
--
-- 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
To unsubscribe from this group, send email to
group at https://groups.google.com/d/forum/cukes?hl=en
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
Javix
2012-09-25 11:15:20 UTC
Permalink
Thank you all for the precious guide lines :).
Post by Javix
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?.
...
<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
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.
--
-- 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 cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org To unsubscribe from this group, send email to cukes+***@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
Loading...