Accessing global variables in cucumber-jvm

1,883 views
Skip to first unread message

Rakesh

unread,
Dec 1, 2011, 5:39:50 PM12/1/11
to cu...@googlegroups.com
Hi,

I'm experimenting with cucumber-jvm as a way to write selenium tests.

The issue I have is that I want to start the browser once per test suite, not per test.

The only way I can think of doing this is to have some static class instantiating the browser and then my step definitions using this class to get a reference to the driver.

Seems ugly and wanted to know if there was a better way to achieve this?

Thanks

Rakesh

aslak hellesoy

unread,
Dec 1, 2011, 6:37:27 PM12/1/11
to cu...@googlegroups.com
On Fri, Dec 2, 2011 at 9:39 AM, Rakesh <rakesh.m...@gmail.com> wrote:
> Hi,
> I'm experimenting with cucumber-jvm as a way to write selenium tests.
> The issue I have is that I want to start the browser once per test suite,
> not per test.
> The only way I can think of doing this is to have some static class

What's a static class?

> instantiating the browser and then my step definitions using this class to
> get a reference to the driver.
> Seems ugly and wanted to know if there was a better way to achieve this?

I would recommend dependency injection. See
https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-webbit-websockets-selenium
I just modified it a little to do what you're asking for:
https://github.com/cucumber/cucumber-jvm/commit/ed160d3070bcced399457cd171cfc59ce991f466

HTH,
Aslak

> Thanks
> Rakesh
>
> --
> 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
> http://groups.google.com/group/cukes?hl=en.
>

Robert

unread,
Dec 2, 2011, 1:53:52 PM12/2/11
to cu...@googlegroups.com
Aslak,

Thanks for sharing!! I've been wondering about this as well, recently.

Rakesh

unread,
Apr 16, 2012, 5:50:50 PM4/16/12
to cu...@googlegroups.com
Hi,

I wanted to resurrect this thread because I am not sure its still valid. Some threads recently have just confused me even more about state between tests.

I have cucumber-jvm running with picocontainer wiring up my step defs before each scenario and thats fine.

However, I need to talk to the database to clean up and insert test data too. Problem is instantiating a connection to the database takes time and I'd rather set up a pool once and then have the stepdefs ask for a connection.

How can I achieve this?

Rakesh

Aaron VonderHaar

unread,
Apr 16, 2012, 5:58:15 PM4/16/12
to cu...@googlegroups.com


On Apr 16, 2012 2:51 PM, "Rakesh" <rakesh.m...@gmail.com> wrote:
>
> Hi,
>

> I wanted to resurrect this thread because I am not sure its still valid. Some threads recently have just confused me even more about state between tests.
>
> I have cucumber-jvm running with picocontainer wiring up my step defs before each scenario and thats fine.
>
> However, I need to talk to the database to clean up and insert test data too. Problem is instantiating a connection to the database takes time and I'd rather set up a pool once and then have the stepdefs ask for a connection.
>
> How can I achieve this?

Create a connection pool class that manages/pools/caches the connections in the way you want, and then inject an instance of that into your stepdefs instead of directly injecting a database or connection object.

Cheers,
--Aaron V.

Rakesh

unread,
Apr 16, 2012, 6:02:01 PM4/16/12
to cu...@googlegroups.com
Hi,

well the only way I can think of doing it is by creating a global singleton along the lines of:

Database.getConnection()

The getConnection method would check to see if the pool already exists and create it if not otherwise return a connection.

Is that the only way?

Rakesh

Aaron VonderHaar

unread,
Apr 16, 2012, 6:09:58 PM4/16/12
to cu...@googlegroups.com
On Mon, Apr 16, 2012 at 3:02 PM, Rakesh <rakesh.m...@gmail.com> wrote:
> Hi,
>
> well the only way I can think of doing it is by creating a global singleton
> along the lines of:
>
> Database.getConnection()
>
> The getConnection method would check to see if the pool already exists and
> create it if not otherwise return a connection.
>
> Is that the only way?

Not knowing your specific case, I'd imagine it'd go something like this:

Make a ConnectionPool class...

public class ConnectionPool {
private Connection conn;
public Connection getConnection() {
if (conn == null) {
conn = /* create the connection however you want */;
}
return conn;
}
}

Then inject it in your stepdefs w/ PicoContainer...

public class MyStepdefs {
private final ConnectionPool pool;

public MyStepdefs(ConnectionPool pool) {
this.pool = pool;
}

//... steps that use pool.getConnection() to get a connection ...
}

--
--Aaron V.
[ http://github.com/avh4 ]

Rakesh

unread,
Apr 17, 2012, 4:54:32 AM4/17/12
to cu...@googlegroups.com
Hi Aaron,

nope, that will give me the behaviour of creating a connection per scenario which is what I am trying to avoid. I think I have no choice but to resort to statics:

public class MyConnectionPool {
   private static List<Connection> connections;

   public Connection getConnection() {

      if(connections.size() > 0 {
         // return one of the connections
      } else {
         // intitalise pool with some connections
      }

   }

As you can see, there's a fair bit of logic required. I'll probably have to Use C3P0 and wrap it.

Rakesh
Reply all
Reply to author
Forward
0 new messages