How to reuse steps in Cucumber
While working in cucumber many times we find that some piece of code is redundant across multiple step definitions.To make this work a simple solution is callling a step from another step like:
But this approach is problematic from debugging perspective as in case of we need to debug the scripts as it will lead to only first level of step calling which will not be helpful for issues resolution.
Somewhat better way to achieve re usability,robustness and clarity is to simply fall back on language basic primitives -methods.We may encapsulate these small defs inside a module and extract it to "World" and call the method directly from where ever required as below:
Given /^I login successfully$/ step "I login with valid credentials" end
But this approach is problematic from debugging perspective as in case of we need to debug the scripts as it will lead to only first level of step calling which will not be helpful for issues resolution.
Somewhat better way to achieve re usability,robustness and clarity is to simply fall back on language basic primitives -methods.We may encapsulate these small defs inside a module and extract it to "World" and call the method directly from where ever required as below:
#/support/world_extensions.rb module KnowsUser def login visit('/login') fill_in('User name', with: user.name) fill_in('Password', with: user.password) click_button('Log in') end def user @user ||= User.create!(:name => 'user', :password => 'pass') end end World(KnowsUser) #/step_definitions/authentication_steps.rb When /^I login$/ do login end Given /^a logged in user$/ do login end
Comments