How to reuse the same steps from different definitions classes?

51 views
Skip to first unread message

oleg manzhos

unread,
May 29, 2018, 4:51:09 AM5/29/18
to Cukes
Hi, guys.

I've got the following question.
I have some step, @When("menu is opened"). Menu button is in the header of the page and thus is displayed on all pages in application flow. And I need to call it several times during test from different pages. So, I need to re-use the same step "menu is opened" in several different classes. 

How can I do that? It is not allowed to create Common definition classes and extend others definition classes from them. It is not allowed to have duplicated steps across different definition classes. And I don't want to rename steps according to classes that are calling thise steps like (menu is opened from Product selection page", "menu is opened from product type page". It is copiing of the same code.

Are there any elegant ways to do that?

Thanks! 

Koen Prins

unread,
May 29, 2018, 6:37:55 AM5/29/18
to cu...@googlegroups.com
Sorry, why is it not "allowed"? Who will arrest you?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anoop Halder

unread,
May 29, 2018, 7:10:24 AM5/29/18
to cu...@googlegroups.com
Hi oleg manzhos ,
You cannot create the duplicate step-definition for the same step, but you are open to use that step multiple times on your another scenarios of same or any other feature file.
Just write the step-definition for your step( @When("menu is opened") ) once and use it multiple times.



Thanks..


Warm Regards,

Anoop Halder
Software Engineer
Mob : +91-8605841949



--

oleg manzhos

unread,
May 29, 2018, 8:20:47 AM5/29/18
to Cukes
Thanks for this answer. I know it. But the problem is that I need to use it in different definition classes and, as you wrote, duplication is not allowed

вторник, 29 мая 2018 г., 13:10:24 UTC+2 пользователь Anoop Halder написал:
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.

oleg manzhos

unread,
May 29, 2018, 8:22:04 AM5/29/18
to Cukes
Cucumber prevents a project from running if duplicated steps are used inside different definitions. So, no one will neither arrest me, nor run my project :)

вторник, 29 мая 2018 г., 12:37:55 UTC+2 пользователь Koen Prins написал:
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.

oleg manzhos

unread,
May 29, 2018, 8:23:07 AM5/29/18
to Cukes
I resolve my problem by creating separate definition class for menu. Now I can call it from any place of my application.

вторник, 29 мая 2018 г., 10:51:09 UTC+2 пользователь oleg manzhos написал:

George Dinwiddie

unread,
May 29, 2018, 8:53:02 AM5/29/18
to cu...@googlegroups.com
Oleg,
Have you tried pushing an openMenu() method to a page object?

- George

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

Andrew Premdas

unread,
May 29, 2018, 9:58:03 AM5/29/18
to cu...@googlegroups.com
Sorry chaps, isn't the OP just asking for a ways to share step definitions between features when Cuking with Java? I'm sure this is pretty straightforward but as I only Cuke with Ruby I can't provide the technical details. Could somebody do that and help out Oleg please, cheers

--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



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

Thomas Sundberg

unread,
May 29, 2018, 3:02:08 PM5/29/18
to Cukes
Hi!

On 29 May 2018 at 15:57, Andrew Premdas <apre...@gmail.com> wrote:
> Sorry chaps, isn't the OP just asking for a ways to share step definitions
> between features when Cuking with Java? I'm sure this is pretty
> straightforward but as I only Cuke with Ruby I can't provide the technical
> details. Could somebody do that and help out Oleg please, cheers
>

I think you are right Andrew.

Sharing state will probably solve the problems Oleg is facing with
ambiguous step definitions.

The solution in the Java world is to use dependency injection, DI.
Cucumber supports many different dependency injection frameworks. I
have blog posts about three of them.

If the project doesn't use any dependency framework, PicoContainer is
rather easy to get started with.
Read more here:
http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer

If the project already uses Spring, I recommend using Spring for
sharing state between steps as well. A blog post with n example is
here: http://www.thinkcode.se/blog/2017/06/24/sharing-state-between-steps-in-cucumberjvm-using-spring

Finally, if the project uses Guice:
http://www.thinkcode.se/blog/2017/08/16/sharing-state-between-steps-in-cucumberjvm-using-guice

Oleg, I hope this will help you get on the right track.

/Thomas
>> email to cukes+un...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> ------------------------
> Andrew Premdas
> blog.andrew.premdas.org
>
> --
> 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.



--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://www.thinkcode.se/blog
Twitter: @thomassundberg

Better software through faster feedback

Koen Prins

unread,
May 30, 2018, 3:12:40 AM5/30/18
to cu...@googlegroups.com
I can assert that these blogs helped me get DI working in one of my projects and solved the problem of sharing steps.


>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> ------------------------
> Andrew Premdas
> blog.andrew.premdas.org
>
> --
> 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

> For more options, visit https://groups.google.com/d/optout.



--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://www.thinkcode.se/blog
Twitter: @thomassundberg

Better software through faster feedback
--
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+unsubscribe@googlegroups.com.

Anoop Halder

unread,
May 30, 2018, 5:26:59 AM5/30/18
to cu...@googlegroups.com
Let me explain it-

================================
FeatureFile1.feature- ( Page 1 )

Scenario: Feature1 scenario1
@When("menu is opened")  ----------It will refer  StepDefFeature1.java- >menu_is_opened () 

Scenario: Feature1 scenario2
@When("menu is opened")   ---------- Allowed to reuse, It will refer  StepDefFeature1.java- >menu_is_opened () 
================================

StepDefFeature1.java-

@When("^menu is opened$")
public void menu_is_opened () {
System.out.println("Run - Step when Feature1 Scenario1 menu is opened ");
}

=========================================================================

FeatureFile2.feature- ( Page 2 )

Scenario: Feature2 scenario1
@When("menu is opened") -------- Allowed to reuse, it will refer the code  StepDefFeature1.java-> menu_is_opened ()
================================

StepDefFeature2.java-

@When("^menu is opened$") ------------- This is not allowed because it already has its method in " StepDefFeature1.java" file
public void menu_is_opened_Page2 () {
System.out.println("Run - Step when Feature1 Scenario1 menu is opened ");
}

@When("^menu is opened$") ------------- This is not allowed because it already has its method in " StepDefFeature1.java" file
public void menu_is_opened () {
System.out.println("Run - Step when Feature1 Scenario1 menu is opened ");
}


================================

If the code you have written in  StepDefFeature1.java file is usable for another page(where you want to reuse) as you said, you just need to put it in your scenario for that page feature file,
like I have done above-

 " FeatureFile1.feature- Scenario: Feature1 scenario2 "
 " FeatureFile2.feature- Scenario: Feature1 scenario1 "


I hope it will help..





Warm Regards,

Anoop Halder
Software Engineer
Mob : +91-8605841949



To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.

George Dinwiddie

unread,
May 30, 2018, 9:05:41 AM5/30/18
to cu...@googlegroups.com
You can also share steps WITHOUT dependency injection. The step
definitions are all available to any feature file, unless you mess up
the classpath when executing cucumber-jvm. In
https://github.com/gdinwiddie/EquineHoroscope I use DI only for making
the same step definition work two different ways (either 1. directly
through an API or 2. through a browser driver and the GUI) depending on
the need.

There must be more to this problem than has been revealed, as sharing
steps is not usually a problem.

- George

On 5/30/18 3:12 AM, Koen Prins wrote:
> I can assert that these blogs helped me get DI working in one of my
> projects and solved the problem of sharing steps.
>
> On Tue, May 29, 2018 at 9:01 PM, Thomas Sundberg <t...@kth.se
> <mailto:t...@kth.se>> wrote:
>
> Hi!
>
> On 29 May 2018 at 15:57, Andrew Premdas <apre...@gmail.com
> >> email to cukes+un...@googlegroups.com
> <mailto:cukes%2Bunsu...@googlegroups.com>.
> >> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
> >
> >
> >
> >
> > --
> > ------------------------
> > Andrew Premdas
> > blog.andrew.premdas.org <http://blog.andrew.premdas.org>
> >
> > --
> > Posting rules: http://cukes.info/posting-rules.html
> <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
> <mailto:cukes%2Bunsu...@googlegroups.com>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
> --
> Thomas Sundberg
> M. Sc. in Computer Science
>
> Mobile: +46 70 767 33 15
> Blog: http://www.thinkcode.se/blog
> Twitter: @thomassundberg
>
> Better software through faster feedback
>
> --
> Posting rules: http://cukes.info/posting-rules.html
> <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
> <mailto:cukes%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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
> <mailto:cukes+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Reply all
Reply to author
Forward
0 new messages