swood
swood

Reputation: 101

Cucumber: Is it possible for a step to detect tags?

I am in the midst of writing a test suite for a password management page. For the scenarios, the majority should not actually change the password, but some do. I have used the tag @changePassword so that I can optionally run those scenarios or not.

The problem I run into is trying not to write duplicate steps if possible.

Simplified sample scenarios:

@changePassword
Scenario: successful change
  Given the Manage Password page is loaded
  And a new password is generated
  When the old password is entered
  And the new password is entered
  And the confirm password is entered
  And the OK button is clicked
  Then the password has changed

Scenario: failed change (missing confirm)
  Given the Manage Password page is loaded
  And a new password is generated
  When the old password is entered
  And the new password is entered
  And the OK button is clicked
  Then the password change fails

The majority of the steps are identical between the two versions, the main variance that I am concerned with is the And a new password is generated step. In the first scenario, I want the new password to be saved as the user's password. In the second scenario I want the new password discarded at the end.

Something like: (psuedo-code)

And /^a new password is generated$/ do
  old password = user's password
  new password = generate random new password
  confirm password = new password
  if tag @changePassword is present
    user's password is set as the new password
  end
end

Is there anyway to make this possible? I can write a second step like And a new password to be saved is generated or something, but for readability and for the non-tech savvy co-workers, using the same step is the better option. (I have found in the past using different phrases to describe similar processes, that to the user accomplishes the exact same thing, has caused confusion. Trying to avoid workplace confusion if possible.)

Side note: Using Cucumber with Ruby (with Watir), if that makes any difference (does it?)

Upvotes: 1

Views: 2881

Answers (1)

Dave
Dave

Reputation: 474

It's an ugly solution, but can you use a tagged hook to set a variable and then an if statement in the method that saves/doesn't save based on the value of that variable?

Upvotes: 2

Related Questions