0

I'm looking for a way to get all the parameters that are being passed in each step before entering the actual scenario for each scenario in my feature file.

Sample feature file:

Feature: Login action
  Background:
    When "{login url}" is open

  Scenario: Registered user provides valid username and password
    Given user enters username "{username}" and password "test password"
    And user clicks on "btnLogin"
    Then user is logged in

Parameters I want to get:

  • {login url}
  • {username}
  • password
  • btnLogin

    What I tried so far:

    I have tried using a common hook that will be automatically used by all of my scenarios:

    public class ScenarioHook {
    
        public ScenarioHook() {
        }
    
        @Before
        public void setupScenario(Scenario scenario) throws InterruptedException {
        //Here I am currently watching the {scenario} object and I can see all the steps
        //but I still dont know where to get the passed parameter values.
        }
    
        @After
        public void teardownScenario() throws InterruptedException {
        }
    }
    

    UPDATE 1: The reason why I want to do this is I want to manipulate the strings (if possible). e.g. all data enclosed in "{}" will be transformed to something else before entering the actual scenario.

  • iamkenos
    • 1,446
    • 8
    • 24
    • 49

    2 Answers2

    5

    You can use the @Transform annotation to change the value of the parameter to the step definition.

    For this you will need to create a class which contains the logic of the string modification and will return the modified value.

    public class StringTransformer extends Transformer<String>{
    
        public String transform(String value) {     
            return "transformed "+value;
        }    
    }
    

    Next you need to include this class in your stepdefinition using the @Transform annotation in front of the method argument.

       @When("^Login with (.*?)$")
            public void helloHere(@Transform(StringTransformer.class) String userName)
        {
                System.out.println("TEXT --- " + userName);
        }
    

    This should give you the new transformed string. You can use this to create objects from your initial string. (Actually that is what it is used for)

    Grasshopper
    • 8,908
    • 2
    • 17
    • 32
    • Thanks! I'll try this immediately. I'll accept your answer if I find it working but would really appreciate if you can tell whether there's a way to do it without having to prefix each parameter with the @Transform annotation. Thanks again! – iamkenos Jul 10 '17 at 14:58
    • You can try by using the concept of xstreams using the XStreamConverter annotation - https://groups.google.com/forum/#!topic/cukes/goJdfuv8VHY. Use this link to check out how to create xstream converters - http://x-stream.github.io/converter-tutorial.html. – Grasshopper Jul 10 '17 at 15:13
    • Your solution worked. I'll check XStreamConverter out. Huge help! Thanks mate! – iamkenos Jul 10 '17 at 15:22
    0

    Cucumber Sceanario class will not provide you this information as I don't think all steps are actually being loaded and parsed before starting executing the scenario but parsed one by one during the scenario execution. One option I can think of is to create a background step where you will manually include all parameters involved in the scenario. For example:

    Feature: Login action
    
      Background:
        Given these parameters are using within this scenario: "{login url}", "{username}", "password", "btnLogin"
    
        When "{login url}" is open
    
    Eugene S
    • 6,709
    • 8
    • 57
    • 91