How to use common/shared “blocks” between cucumber features?

后端 未结 2 1720
悲哀的现实
悲哀的现实 2020-12-25 15:20

I\'m new to cucumber, but enjoying it.

I\'m currently writing some Frank tests, and would like to reuse blocks of cucumber script across multiple features - I\'d lik

相关标签:
2条回答
  • 2020-12-25 15:27

    Generally there are 2 approaches:

    Backgrounds

    If you want a set of steps to run before each of the scenarios in a feature file:

    Background:
         given my app has started
         then enter "guest" in "user-field"
         and enter "1234" in "password-field"
         and press "login"
         then I will see "welcome"
    
    Scenario: Some scenario
        then *** here's the work specific to this scenario ***
    
    Scenario: Some other scenario
        then *** here's the work specific to this scenario ***
    

    Calling steps from step definitions

    If you need the 'block' of steps to be used in different feature files, or a Background section is not suitable because some scenarios don't need it, then create a high-level step definition which calls the other ones:

    Given /^I have logged in$/ do
        steps %Q {
             given my app has started
             then enter "guest" in "user-field"
             and enter "1234" in "password-field"
             and press "login"
             then I will see "welcome"
        }
    end
    

    Also, in this case I'd be tempted not to implement your common steps as separate steps at all, but to create a single step definition: (assuming Capybara)

    Given /^I have logged in$/ do
        fill_in 'user-field', :with => 'guest'
        fill_in 'password-field', :with => '1234'
        click_button 'login'
    end
    

    This lends a little bit more meaning to your step definitions, rather than creating a sequence of page interactions which need to be mentally parsed before you realise 'oh, this section is logging me in'.

    0 讨论(0)
  • 2020-12-25 15:31

    A better approach is suggested to use ruby level "methods" to code reuse instead of nested steps from code maintenance and debugging perspective.

    Here is the link to more detail: Reuse Cucumber steps

    0 讨论(0)
提交回复
热议问题