Madhu
Madhu

Reputation: 253

Step definition from multiple files for same scenario

I have a scenario where its step definitions resides in multiple files. For instance the login step resides in login_steps.rb and a search related step resides in search_steps.rb

Cucumber outputs undefined steps for any step that is not in login_steps.rb. The step definitions are run only when its present in login_steps.rb. Is it required to place all the step definitions of a scenario in the same file?

My folder structure

Project folder
└─ features
   ├─ pages
   ├─ scenarios 
   ├─ step_definitions
   └─ support

Command I used:

cucumber -r features features\scenarios\Test.feature

Upvotes: 2

Views: 3728

Answers (3)

Jon Kern
Jon Kern

Reputation: 3235

This might be a "violation" but I would combine the answers from Whitney Imura and Dave W just to make the answer more clear...

You ask:

"Is it required to place all the step definitions of a scenario in the same file?"

No. You can place your step definitions in logically distinct files within various folders, as you see fit (example below). After all, it is just ruby code.

Essentially Your command is correct for running an individual feature that has step definitions in various other folders...

cucumber -r features features\entities\entity.feature

If you do not run it as above, you will get missing stepdefs... Here I execute tests on a current project as a means to demonstrate:

cucumber
60 scenarios (14 undefined, 46 passed)
409 steps (32 skipped, 26 undefined, 351 passed)

cucumber -r features
60 scenarios (60 passed)
409 steps (409 passed)

As described in the Cucumber documentation, you can arrange your tests to suite your logical breakdown of your features:

|__ features
|   |__ entities
|   |   |__ entity.feature
|   |   |__ step_definitions
|   |       |__ anything.rb
|   |       |__ entity_steps.rb
|   |__ locations
|   |   |__ location.feature
|   |   |__ step_definitions
|   |       |__location_steps.rb
|   |__ sites
|   |   |__ step_definitions
|   |__ step_definitions
|   |   |__ local_assert_steps.rb
|   |   |__ local_crud_response_steps.rb
|   |   |__ local_email_steps.rb
|   |   |__ local_file_steps.rb
|   |   |__ local_script_steps.rb
|   |   |__ local_steps.rb
|   |   |__ local_web_steps.rb
`   |   |__ local_xml_file_steps.rb   
    |__ support
        |__ env.rb
        |__ local_env.rb
        |__ local_transforms.rb

Upvotes: 0

Whitney Imura
Whitney Imura

Reputation: 810

The whole point to Cucumber and the POM is that you have flexibility and do not need to re-write your steps per feature file. This is what my directory structure looks like:

Root
 - features
   - step_definitions
     - step_definition.rb
   - support
      - env.rb
 - lib
  - BasePage.rb
 - feature.feature

Basically, with this directory structure, it doesn't matter where your step definitions are AS LONG AS YOU REQUIRE THE SPECIFIC PAGE YOU'RE REFERENCING (your BasePage.rb file, for example)

require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'pages', 'BasePage')


And /^I do something$/ do
  @page = BasePage.new(@test_env)
  @page.verify_el(css)
end

Upvotes: 1

Dave W
Dave W

Reputation: 21

I'm not familiar with the specifics of RoR and cucumber, but I do use cucumber-jvm. Using steps from different files is supported. Note the documentation https://github.com/cucumber/cucumber/wiki/Cucumber-Backgrounder#where-do-i-put-tests specifically mentions it.

Sorry I can't be more help with the specific issue, but what you are trying to do (use step from different files) is workable.

Upvotes: 0

Related Questions