Cucumber - using the value generated from the first step to all the other steps

44 views
Skip to first unread message

Gheeno San Pascual III

unread,
Feb 9, 2020, 9:32:20 PM2/9/20
to Cukes
Description : How do you reuse value generated from the first step.

Cucumber: 
Given user is generated 
And user can login 
Then the user can see the profile page. 

Java 
```
UserGenerator.class

String user;
Given("{} is generated")
public String getUserId(String userType) {
// method here to generate the userId
userId = user;
}


Steps.Class

And("{} can login")
public void login(String user) {
// how do I call the value made in the getUserId method 
}

```

What would be the best approach to store a value made from a step and then be used again ( the same value ) on another step?

Thanks for the insight.


George Dinwiddie

unread,
Feb 9, 2020, 9:41:01 PM2/9/20
to cu...@googlegroups.com
Gheno
Try this:

> Java
> ```
> UserGenerator.class
>
> public String getUserId(String userType) {
> // method here to generate the userId
> return userId;
> }
>
>
> Steps.Class
>
> String user;
>
> Given("{} is generated")
> public void generateUser(String userType) {
> user = new UserGenerator().getUserId(userType);
> }
>
> And("{} can login")
> public void login(String user) {
> // how do I call the value made in the getUserId method
> }
>
> ```

- George

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach
----------------------------------------------------------------------

Andrew Premdas

unread,
Feb 10, 2020, 4:46:44 AM2/10/20
to cu...@googlegroups.com
In cucumber there are 3 ways you can communicate between steps. 2 involve looking up things by a reference in subsequent steps, the 3rd uses global variables.

Following examples are in Ruby, I'll leave it up to others to translate to Java.

1. Implicitly:

Your subsequent steps look up the last record in the database of the thing you are interested in, e.g.

Given 'there is a user' do
   create_user
end

When 'the user logs in' do
   user = User.last
   login_user(user)
 end

2. By name:

Given 'there is a user Fred' do
  create_user(name; 'Fred')
end

When 'Fred logs in' do
  user = User.where(name: 'Fred').first
  create_user(user)
end

3. By using a global variable. The variable is global in Cucumber's World object and so can be accessed in other step definitions

Given "I am a user" do
  @i = create_user
end

When "I login" do
  login_user(@i)
end

There are advantages and disadvantages to each approach, and each approach effects the language of the scenarios.

I rarely use 1 now. I use 2 occasionally particularly when talking about interactions between multiples e.g. stuff involving users Fred and Sue. I use 3 most of all, and in particular use it with singleton concepts like i, my, mine etc. However I have to warn you that using globals safely requires great discipline, and its very easy to create a great deal of mess if you do it badly. So I'd suggest using 2 if you can't meet those criteria.

All best

Andrew

--
Posting rules: https://cucumber.io/support/posting-rules
---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/cukes/08761607-321d-4fc8-b357-4c8bf4213f22%40googlegroups.com.


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

georgia tsoutsika

unread,
Feb 10, 2020, 7:44:36 AM2/10/20
to Cukes
You are supposed to implement the 'world' concept and dependency injection.

See more here
https://cucumber.io/docs/cucumber/state/

And a real use examole here
http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer

Gheeno San Pascual III

unread,
Feb 11, 2020, 6:39:58 PM2/11/20
to Cukes
Thank you for the insight Georgia.
PicoContainer did the trick for my project.
Reply all
Reply to author
Forward
0 new messages