Discussion:
[Cucumber] Define a step for multiple keywords
(too old to reply)
Jean-Charles DURAND
2018-01-12 09:28:02 UTC
Permalink
Hi,

I am a newbie with Cucumber CPP, but I have a solid experience with
Specflow for C# .Net.
One interesting concept with specflow is that, using attributes above you
step definitions, you can define a step for multiple keywords (Given and
When for example), or define two sentence for the same step definition.

[When(@"I perform a simple search on '(.*)'")]
[When(@"I search for '(.*)'")]public void WhenIPerformASimpleSearchOn(string searchTerm)
{
...
}



I would like to know if there is a way to do the same thing with cucumber
CPP, other than duplicate the step definition?

Thanks for your help,
Jean-Charles
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrew Premdas
2018-01-14 09:41:36 UTC
Permalink
In Cucumber the keyword for the step definition is irrelevant to the call
so if you have a step defined as

Given "I foo" do
...
end

You can call it with

When I foo

Generally its better not to do this and too use tense to differentiate
between Given, When and Then e.g.

Given I have foo'd
When I foo
Then I should have foo'd

You will often want two sentences to do the same thing in Cucumber. The
best way to do this is to duplicate the step definition, but at the same
time simplify the contents of the step definition so a single method call
to an external helper. So with your example (but with Ruby code because its
been 20+ years since I did CPP)

When "I search for '(.*)' " |q|
search_for(q)
end

When "I perform a simple search on '(.*)'" do |q|
search_for(q)
end

and then somewhere else

module SearchStepHelper
def search_for(search_term)
# do your search here
end
end
World SearchStepHelper

is just fine.

There is no problem at all having lots of step definitions that work this
way as you are not repeating yourself (i.e. defining how something is done
more than once) by making a call.

I don't know the actual mechanism for calling external methods in step
definitions with Cucumber CPP, hopefully someone else can chip in with
that, or you can ask a separate question if you can't find out.

All best

Andrew
Post by Jean-Charles DURAND
Hi,
I am a newbie with Cucumber CPP, but I have a solid experience with
Specflow for C# .Net.
One interesting concept with specflow is that, using attributes above you
step definitions, you can define a step for multiple keywords (Given and
When for example), or define two sentence for the same step definition.
{
...
}
I would like to know if there is a way to do the same thing with cucumber
CPP, other than duplicate the step definition?
Thanks for your help,
Jean-Charles
--
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.
--
------------------------
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...