Re: [Cucumber] Nested steps with examples

663 views
Skip to first unread message
Message has been deleted

Oscar Rieken

unread,
Jan 4, 2016, 3:12:20 PM1/4/16
to cu...@googlegroups.com

On Mon, Jan 4, 2016 at 1:55 PM, Vlad Wbox <vlad...@gmail.com> wrote:
Hi!
I have several steps that use data from Examples section
i.e.:(my actual scenario is different)

Scenario Outline: Log in
When I enter "<Username>"
And I enter "<Password>"
Then I see "<Message>"
Examples:
|Username|Password|Message|
|aaaaaaaa|bbbbbbbb|OK          |

I want to make a step: "I log in" ,that will have nested steps like that:

When /^I log in$/ do
steps %Q{
When I enter "<Username>"
And I enter "<Password>"
Then I see "<Message>"
}
end
Are you sure you really want to do this? why not just have a login method. 
doing it this way essentially every scenario that calls this step is dependent on the Message Validation. 

 


Scenario Outline: Log in
When I log in
Examples:
|Username|Password|Message|
|aaaaaaaa|bbbbbbbb|OK          |


But when i do that, the data from Examples section is not being passed.
Is there a way to do that?
Thanks!

--
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

unread,
Jan 4, 2016, 3:44:20 PM1/4/16
to cu...@googlegroups.com
On 4 January 2016 at 18:55, Vlad Wbox <vlad...@gmail.com> wrote:
Hi!
I have several steps that use data from Examples section
i.e.:(my actual scenario is different)

Scenario Outline: Log in
When I enter "<Username>"
And I enter "<Password>"
Then I see "<Message>"
Examples:
|Username|Password|Message|
|aaaaaaaa|bbbbbbbb|OK          |

I want to make a step: "I log in" ,that will have nested steps like that:

When /^I log in$/ do
steps %Q{
When I enter "<Username>"
And I enter "<Password>"
Then I see "<Message>"
}
end


Scenario Outline: Log in
When I log in
Examples:
|Username|Password|Message|
|aaaaaaaa|bbbbbbbb|OK          |


But when i do that, the data from Examples section is not being passed.
Is there a way to do that?
Thanks!


You really really don't want to do this. Nested steps are evil. Instead write a helper method

module LoginStepHelper
 def login(user)
    ...
  end
end
World LoginStepHelper

When "I login" do
   login @i
end

and use a Given to set up the user e.ge

Given "I am an admin" do
  @i = create_admin_user
end

and add a method in a helper to create the admin user.

You can see detailed examples of this approach at https://github.com/diabolo/another_cuke_rails. Notice how simple the features/scenarios are, and how all the difficult stuff is done lower down the stack in step helper modules where you can use a programming language. Programming languages are great at doing difficult things, features are rubbish at that sort of stuff.

As someone who has in depth experience of using nested steps, living with a quagmire of nested steps and spending weeks removing/refactoring layers and layers of nested steps, I strongly recommend you don't ever use them!

All best

Andrew
 

--
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
Message has been deleted

Andrew Premdas

unread,
Jan 5, 2016, 10:26:44 AM1/5/16
to cu...@googlegroups.com
As I said, been there, done that, you're heading for a terrible mess if you keep following this pattern. Step re-use is highly overated. Different steps calling the same helper method is the way forward.

Anyhow good luck

On 4 January 2016 at 21:03, Vlad Wbox <vlad...@gmail.com> wrote:
The reason I need it, is that I use those steps as steps on their own in other features.
And definitions are already written for them.
So i just want to reuse them for some specific scenarios.

I actually did it another way:

Scenario Outline: Log in
When I log in, "<Username>", "<Password>", "<Message>"
Examples:
|Username|Password|Message|
|aaaaaaaa|bbbbbbbb|OK          |

and definition

When /^I log in, "([^\"]*)", "([^\"]*)", "([^\"]*)"$/ do |x,y,z|
steps %Q {
I enter "#{x}"
I enter "#{y}"
I see "#{z}"
}
end


--
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.

aslak hellesoy

unread,
Jan 5, 2016, 10:57:59 AM1/5/16
to Cucumber Users
I wonder if the underlying issue here is that some people don't know how to reuse code using regular methods.

TBH I would like to remove the "calling steps from steps" feature completely from all Cucumber implementations and replace that with a blog post and/or documentation explaining how to use methods/functions to reuse code.

WDYT?

Richard Lawrence

unread,
Jan 5, 2016, 11:03:24 AM1/5/16
to cu...@googlegroups.com
On Tue, Jan 5, 2016 at 8:57 AM, aslak hellesoy <aslak.h...@gmail.com> wrote:
I wonder if the underlying issue here is that some people don't know how to reuse code using regular methods.

TBH I would like to remove the "calling steps from steps" feature completely from all Cucumber implementations and replace that with a blog post and/or documentation explaining how to use methods/functions to reuse code.

WDYT?

Rather than removing it, could you give a warning with a link to that documentation? I suspect many people have quite a bit of this in their step definitions, and removing it would be pretty disruptive. You don't want your whole test suite breaking on an upgrade.

Richard

aslak hellesoy

unread,
Jan 5, 2016, 11:09:17 AM1/5/16
to Cucumber Users
On Tue, Jan 5, 2016 at 4:02 PM, Richard Lawrence <richard....@agileforall.com> wrote:


On Tue, Jan 5, 2016 at 8:57 AM, aslak hellesoy <aslak.h...@gmail.com> wrote:
I wonder if the underlying issue here is that some people don't know how to reuse code using regular methods.

TBH I would like to remove the "calling steps from steps" feature completely from all Cucumber implementations and replace that with a blog post and/or documentation explaining how to use methods/functions to reuse code.

WDYT?

Rather than removing it, could you give a warning with a link to that documentation? I suspect many people have quite a bit of this in their step definitions, and removing it would be pretty disruptive. You don't want your whole test suite breaking on an upgrade.


Oh yeah, I wouldn't just pull the rug under people's feet - we've got about a million users!

It would be a deprecation notice and there would be several releases after that before it's removed for good. Maybe a year from now.

Aslak
Reply all
Reply to author
Forward
Message has been deleted
0 new messages