cucumber / cucumber-ruby

Cucumber for Ruby. It's amazing!
https://cucumber.io
MIT License
5.18k stars 1.12k forks source link

Token is unable to fetch correct value if steps based on it is executed twice in same scenario. #1755

Closed prashantsssh closed 2 months ago

prashantsssh commented 6 months ago

👓 What did you see?

Expected token value for the same step that is getting executed for the second time in the same scenario is different from the expected value. Basically the token value stored during the execution of first step is displayed while execution of same step for the second time.

✅ What did you expect to see?

During execution, for each step fresh token value should be fetched whether the step or token value is same or not.

📦 Which tool/library version are you using?

Ruby-3.2.1 Cucumber-8.0 watir-7.6

🔬 How could we reproduce it?

  1. File name-Gemfile
    source 'https://rubygems.org'
    gem 'bundler', '~> 2.0'
    gem 'cucumber', '~> 8.0.0'
  2. Create a feature file. File name-test.feature

    @token
    Feature: Testing Token
    
    Scenario: Testing Token Functionality For The Same Steps In The Same Scenario
    
    When I should see desired message as 'This goal is scored by PLAYER1.'
    When I should see desired message as 'This goal is scored by PLAYER1.'
  3. Create a step definition class. File name-step_def_steps.rb
    Then /^I should see desired message as '(.+(?:PLAYER)(?:\d+).+)'$/ do |expected_msg|
    actual_msg = 'This goal is scored by JACK REACHER.'
    /^#{expected_msg}$/.match? actual_msg
    end
  4. Create a parametertype class where we have to put the required token. File name-parameter_types.rb

    ParameterType(
    name:        'token',
    regexp:      /.+(?:PLAYER)(?:\d+).+/,
    type:        String,
    transformer: -> (msg) do
    
    puts 'Message fetched by step-> ' + msg # This statement is placed here to check the value of message that msg variable is fetching from the step in feature file.
    
    test_data = {
      :player1 => 'JACK REACHER',
      :player2 => 'JOHN WICK',
    }
    case msg
    when /\PLAYER(?:\d+)/
      msg.gsub!(/PLAYER(?:\d+)/, test_data[:player1])
    else
      raise 'This token is not supported'
    end
    end
    )

    You can execute the feature with the command--> bundle exec cucumber -t @token,

    📚 Any additional context?

As per my opinion, the value of token is stored every time when the operation is performed and if the value of the passed token is same in the second step also then it will display the same value. However if the value of token is different in both steps, then it will work properly. Console outupt-

@token
Feature: Testing Token

  Scenario: Testing Token Functionality For The Same Steps In The Same Scenario # features/test_token.feature:4
Message fetched by step-> This goal is scored by PLAYER1.
    When I should see desired message as 'This goal is scored by JACK REACHER.      # features/step_definitions.rb:1
Message fetched by step-> This goal is scored by JACK REACHER.
    When I should see desired message as 'This goal is scored by JACK REACHER.      # features/step_definitions.rb:1
      This token is not supported (RuntimeError)

When the step When I should see desired message as 'This goal is scored by PLAYER1.' is called again, then the parameter_types transformer (msg), should have This goal is scored by PLAYER1. But the actual message is fetched from the previous step.

QUESTION- Is that how supposed to work or is it a bug??

luke-hill commented 6 months ago

Hi @prashantsssh - It's hard to glean information off of images. Could you please upload in the form of a git repo or a failing test to a repository

prashantsssh commented 6 months ago

Hi @prashantsssh - It's hard to glean information off of images. Could you please upload in the form of a git repo or a failing test to a repository

@luke-hill I have updated my question with sample code so that you can reproduce it. Please have a look.

luke-hill commented 6 months ago

I don't see anything failing in your code execution. Again if you could ideally show us in the form of a failing test or a git repo? It sounds like you are seeing something you're not expecting.

Could you also strip down as much redundant information as possible. see SSCCE.org for information

luke-hill commented 2 months ago

Closing due to inactivity