Frank
Frank

Reputation: 15

Cucumber: Step Definitions multiple Optional Group - Capture in same step

I'm trying to write a step that will match the following steps that are similar and capture parameters:

Step1: And I delete the filter(s) using the "UI"

Step2: Then I delete the filter(s) using the "API" for "doc-browser" context

Step3: I delete the filter(s) using the "API" for "doc-browser" context with user "file_user2"

I don't want to create 3 separate steps, they all start with I delete the filter using the #{arg} and the last 2 just extend on that.

I thought this was going to accomplish it:

And(/^I delete the filter\(s\) using the "([^"]*)"(?: for "([^"]*)" context| with user "([^"]*)")?/) do |delete_method, context, user| case delete_method when 'API' if user.nil? SearchFilters.new.delete_global_local_filters(delete_method, api_context_val: context) else SearchFilters.new.delete_global_local_filters(delete_method, { api_context_val: context, username: user }) end when 'UI' SearchFilters.new.delete_global_local_filters(delete_method, filter_name: @filter_name) end end

However, I'm not capturing my username.

Is it possible to accomplish having one step definition that captures all 3 variations and still captures my arguments?

Upvotes: 0

Views: 569

Answers (1)

diabolist
diabolist

Reputation: 4099

Just duplicating my mailing list answer in case anyone else is interested in this question.

I understand you don't want to use three different steps, but in this case you really should because

  1. The implementation of each step is a clear one liner, so you are removing a case statement.
  2. The regex for this step is horrible and you will be removing that
  3. Each individual step provides the opportunity to simplify the parameters being passed, you have 2 API and one UI paramter which can just be removed entirely (if I understand the code correctly you can remove all the params and regexs)

This is a clear case where a little bit of repetition is a price well worth paying for a simpler implementation.

Reducing the number of step definitions by using regex's and params is often an anti-pattern.

All best

Andrew

Upvotes: 1

Related Questions