How to use variable in Cucumber feature files?

5,368 views
Skip to first unread message

Allen Zhang

unread,
Apr 2, 2016, 9:08:59 AM4/2/16
to Cukes
In Robot Framework, you can create a variable file, with all your variable definitions in that file. And in your test cases, you just need to refer to that file, it will replace the variables with the definition in that file. 
So, currently, I want to reuse the Robot framework test cases in my Java cucumber test. Please notice, it is cucumber Java test, not the Cucumber for Ruby. I know ruby has some configuration file, cucumber.yml, but seems Java does not support it.
Does anyone has any experience on it?
Thanks!

Thomas Sundberg

unread,
Apr 2, 2016, 11:04:15 AM4/2/16
to cu...@googlegroups.com
Hi!

On 2 April 2016 at 15:59, Allen Zhang <allen...@gmail.com> wrote:
> In Robot Framework, you can create a variable file, with all your variable
> definitions in that file. And in your test cases, you just need to refer to
> that file, it will replace the variables with the definition in that file.

Are you able to share more information about what you would store in
the variables?

I am imagining that it could be things like host names?

I would consider having property files on the class apth for different
environments and set the current environment using a -D parameter to
my execution.

something on the lines

mvn install -Denv=acc

Where acc would indicate that you are targeting an acceptance test environment.

Would this be a way for you to proceed?

/Thomas

> So, currently, I want to reuse the Robot framework test cases in my Java
> cucumber test. Please notice, it is cucumber Java test, not the Cucumber for
> Ruby. I know ruby has some configuration file, cucumber.yml, but seems Java
> does not support it.
> Does anyone has any experience on it?
> 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.



--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://thomassundberg.wordpress.com/
Twitter: @thomassundberg

Better software through faster feedback

Join me for a Selenium kickstart in Timisoara, Romania in April.
http://mozaicworks.com/public-trainings-and-workshops/selenium-webdriver-test-automation-for-web-applications/

Interested in a BDD Kickstart in Stockholm?
https://cucumber.io/events/bdd-kickstart-stockholm-16

Roberto Lo Giacco

unread,
Apr 5, 2016, 5:37:31 AM4/5/16
to Cukes
Are you aware Cucumber is not an automation testing framework?
I mean, I don't know Robot Framework and just read a couple of pages on it, but it's clearly stated on its main page...

Robot Framework is a generic test automation framework for acceptance testing and acceptance test-driven development

So I guess it would beneficial for you, before starting this endeavor, to understand Cucumber is not meant to be a one-to-one replacement for it...

Having said that, a properties file is probably the best substitute for the variables file you mentioned, even if I don't exactly know how a variable file looks like.

George Dinwiddie

unread,
Apr 5, 2016, 6:41:06 AM4/5/16
to cu...@googlegroups.com
Allen,

On 4/2/16 8:59 AM, Allen Zhang wrote:
> In Robot Framework, you can create a variable file, with all your
> variable definitions in that file. And in your test cases, you just need
> to refer to that file, it will replace the variables with the definition
> in that file.

The short answer is that you give you entity that needs to vary a
logical name. Use that name in the gherkin, and let your step definition
look up the value.

As Thomas and Roberto mentioned, a property file is a common choice
because it's trivial to read in Java. You can write Java code to read
the actual values from anywhere, of course.

> So, currently, I want to reuse the Robot framework test cases in my Java
> cucumber test. Please notice, it is cucumber Java test, not the Cucumber
> for Ruby. I know ruby has some configuration file, cucumber.yml, but
> seems Java does not support it.

Even if you were using Ruby, I don't think you'd get what you want from
the cucumber.yml file. It provides the options to the cucumber runner.

And unless your Robot keywords are in gherkin, you'll need to make some
changes, anyway.

> Does anyone has any experience on it?

Yes, there's lots of cucumber experience here, but not much experience
on your particular situation. Perhaps if you posted a Robot Framework
test we could help you translate it.

- George

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

Chuck van der Linden

unread,
Apr 12, 2016, 6:16:19 PM4/12/16
to Cukes
IMHO wanting to have implementation specific info like that in a FEATURE file is a smell that says you are cuking it wrong.  You should not need things like names of servers/hosts in a feature file in order to express the business value of a feature, or write your given/when/then scenarios.

As George implies, the place for info like that is inside your steps.  The place to read-in a config file, or pull values from env variables is generally inside the ENV file (in the support directory).

For my cucumber work I created a YML file that held things like names of servers in different test environments, what browser to use, and any other info of that sort that the tests need.  I then created a 'readconfig' method that processed the YAML file along with checking for environment variables (which trumped the file) and set the values into constants that were referenced inside step code.  That readconfig method then got called fairly early in the env.rb file

Typically the only place any of us use what you might think of as a variable in feature files is with Scenario Outlines to indicate where values from the examples table fit into the outline.
Reply all
Reply to author
Forward
0 new messages