代码之家  ›  专栏  ›  技术社区  ›  iamkenos

Java Cucumber:如何从单个场景中的所有步骤中获取字符串参数值

  •  0
  • iamkenos  · 技术社区  · 9 年前

    对于我的功能文件中的每个场景。

    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
    

    我想要获得的参数:

  • {登录url}
  • {username}
  • btnLogin公司

    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 {
        }
    }
    

    我想这样做的原因是我想操纵字符串(如果可能的话)。e、 g.“{}”中包含的所有数据将在进入实际场景之前转换为其他数据。

  • 2 回复  |  直到 9 年前
        1
  •  5
  •   Grasshopper    9 年前

    您可以使用 @Transform

    为此,您需要创建一个包含字符串修改逻辑的类,并返回修改后的值。

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

    接下来,您需要在方法参数前面使用@Transform注释将此类包括在stepdefinition中。

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

    这将为您提供新的转换字符串。您可以使用它从初始字符串创建对象。(实际上这就是它的用途)

        2
  •  0
  •   Eugene S    9 年前

    黄瓜 Sceanario

    Feature: Login action
    
      Background:
        Given these parameters are using within this scenario: "{login url}", "{username}", "password", "btnLogin"
    
        When "{login url}" is open