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.

  • 2
    • Can you please provide some information as to why you want to do so? Commented Jul 10, 2017 at 3:39
    • @EugeneS Have updated my question with the basic details of why I want it. Thanks! Commented Jul 10, 2017 at 4:51

    2 Answers 2

    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)

    Sign up to request clarification or add additional context in comments.

    3 Comments

    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!
    You can try by using the concept of xstreams using the XStreamConverter annotation - groups.google.com/forum/#!topic/cukes/goJdfuv8VHY. Use this link to check out how to create xstream converters - x-stream.github.io/converter-tutorial.html.
    Your solution worked. I'll check XStreamConverter out. Huge help! Thanks mate!
    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
    

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.