How to pass a value from one step to another cucumber step

3,936 views
Skip to first unread message

Vinoth Kannan

unread,
Jun 22, 2016, 9:32:13ā€ÆAM6/22/16
to Cukes
Say I have a step which generates a data and now I have another cucumber step where I need to pass that data and to get some expected result.

Is there any way for this other than saving a variable as global and storing the value in it

Richard Lawrence

unread,
Jun 22, 2016, 10:44:20ā€ÆAM6/22/16
to cu...@googlegroups.com
On Wed, Jun 22, 2016 at 7:32 AM, Vinoth Kannan <vinothk...@gmail.com> wrote:
Say I have a step which generates a data and now I have another cucumber step where I need to pass that data and to get some expected result.

Is there any way for this other than saving a variable as global and storing the value in it

Which language are you using? Can you share a concrete example of the two step defs you'd like to share state between?

Richard
Ā 

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vinoth Kannan

unread,
Jun 23, 2016, 12:52:17ā€ÆAM6/23/16
to Cukes
Thanks Richard for your reply.

i am using java with cucumber.

the below is a example step :

Given I go to "LoginPage.url" URL
And create an applicationĀ 
And search the application created


I am creating an application and once it gets created it generates an application number ex: APP123 ,
now i want to copy this number and enter in the search text box and should verify that the application is displayed.

Thanks in advance



Andrew Premdas

unread,
Jun 23, 2016, 3:18:01ā€ÆAM6/23/16
to cu...@googlegroups.com
You have two scenarios here,

The first is that you can create an application e.g.

Given I am logged in
When I create an application
Then my application should be created

You can confirm that this works by looking at the application number.

The second scenario is that once an application is created you can search for it.

Given I have an application
When I search for my application
Then I should find my application

Now you could create you application in exactly the same way as in the first scenario, or you could bypass the UI and do it in a more direct manner.

Now you have an alternative way of getting the application name to be searched, you can just get the last application added to the database, ask for its name and then search on that name e.g.

When "I search for my application" do
Ā  search_for_application name: Application.last.name
end

One other way to pass information in steps is to use a name when creating things: e.g.

Given I have an application wibble
When I search for the application wibble
Then I should find the application wibble

In this case your step defs can use the name (wibble) to do things e.g.

create_application name: 'wibble'
search_for_application name: 'wibble'
should_see_search_result name: wibble

some general rules

- global variables are the most powerful, most convenient and most dangerous mechanism,
- names in features are the least powerful, least convenient and the safest mechanism
- getting last (only) entry in the database is somewhere inbetween

All best

Andrew

note: Code examples are ruby'ish

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
------------------------
Andrew Premdas

Matthew

unread,
Jun 23, 2016, 5:50:06ā€ÆPM6/23/16
to Cukes
You also have the option of using the DI framework (by default PicoContainer (I think thats still the case) in Java) to pass around the state. Ā The webdriver example shows using this in a technical sense I believe, but you can also put the state of your 'world' (i.e. the test) in to injected objects. Ā Possibly slightly better than full global objects.

Cheers,

Matt
Ā 
Message has been deleted

Vinoth Kannan

unread,
Jun 26, 2016, 10:11:01ā€ÆAM6/26/16
to Cukes
thanks matthew.

I am new to cucumber , has no idea about pico container. can some one please explain it or share some link to walk through it.

Is there any other way to perform this .

Thanks in advance friends.Ā 

Vinoth Kannan

unread,
Jun 27, 2016, 10:27:18ā€ÆAM6/27/16
to Cukes
Can someone Please help me on this.

Richard Lawrence

unread,
Jun 27, 2016, 11:33:10ā€ÆAM6/27/16
to cu...@googlegroups.com
This excerpt from the excellent "Cucumber for Java" book covers dependency injection in step definitions with Pico Container:Ā https://media.pragprog.com/titles/srjcuc/simplify.pdf. (Which, by the way, I found by Googling "cucumber picocontainer" and looking at the first page of results. There's decent documentation online, but it does require a little searching.)

I'd recommend getting the whole book if you're going to be doing much work with Cucumber in Java.

Hope that helps,

Richard

On Mon, Jun 27, 2016 at 8:27 AM, Vinoth Kannan <vinothk...@gmail.com> wrote:
Can someone Please help me on this.

Roberto Lo Giacco

unread,
Jun 29, 2016, 4:46:14ā€ÆAM6/29/16
to Cukes

Il giorno lunedƬ 27 giugno 2016 16:27:18 UTC+2, Vinoth Kannan ha scritto:
Can someone Please help me on this.



If both stepdefs are defined on the same class you can just use a private member variable to store the necessary information.

Dependency Injection is not needed in this case, but it will be needed to share information between stepdefs defined on separate classes.

cucumbe...@gmail.com

unread,
Jun 30, 2016, 5:41:49ā€ÆAM6/30/16
to Cukes
Hi Richard,

Thanks, i am not able to find about PICO container from your link..please help

Roberto thanks for your suggestion, it will help me

Paolo Ambrosio

unread,
Jul 1, 2016, 1:57:35ā€ÆAM7/1/16
to cu...@googlegroups.com
On Thu, Jun 30, 2016 at 10:41 AM, <cucumbe...@gmail.com> wrote:

> Hi Richard,
>
> Thanks, i am not able to find about PICO container from your link..please
> help

Just read the PDF that Richard linked and you'll understand. After you
read the PDF, you should consider but the book: it's an amazing
resources for people that are starting with Cucumber (and not just for
those).
Reply all
Reply to author
Forward
0 new messages