2014-02-11 49 views
1

如何在黄瓜JVM的两步之间传递值?<Cucumber-JVM>在黄瓜步骤之间传递值

在下面的情况下,我想访问提供的用户名当在步骤中的步骤。 如何在黄瓜JVM的两个步骤之间传递值?目前我通过将该值保存到公共变量中来访问这些值。该方法是否正确(或)我可以通过其他方式访问这些步骤之间的方法?

场景:

鉴于用户 当用户输入用户名为user1和密码PASS1 登录页面并点击登录按钮 然后登录后页面显示

@When("^user enters username as ([^\"]*) and password as ([^\"]*)$") 
public void enterLoginDetails(String username,String password){ 
driver.findElement(By.id("username")).sendKeys(username); 
driver.findElement(By.id("password")).sendKeys(password); 
} 

在下面步骤定义,我想从上一步定义访问用户名

@Then("^post login page is displayed$") 
public void postLoginValidation(){ 
// i would like access username and verify username is displayed 
} 

预先感谢

+0

寻找一个回答这个问题。你能找出除了以下答案 –

+0

以外的任何可能的重复:https://stackoverflow.com/questions/26422470/good-practice-to-pass-variables-between-cucumber-jvm-steps – Marit

+0

可能的重复[在cucumber-jvm步骤之间传递变量的最佳做法](https://stackoverflow.com/questions/26422470/good-practice-to-pass-variables-between-cucumber-jvm-steps) – Marit

回答

1
public your class { 

    private String usr; 

    @When("^user enters username as ([^\"]*) and password as ([^\"]*)$") 
    public void enterLoginDetails(String username,String password){ 

     usr = username; 
     ... 
    } 

    @Then("^post login page is displayed$") 
    public void postLoginValidation(){ 

     //do something with usr 

    } 

} 
+0

任何其他替代方法来传递值在步骤之间不使用变量。 – user3188928

0

您可以使用变量共享状态,如Bala所建议的。

Java中的另一个解决方案是使用依赖注入。 Cucumber-JVM支持许多不同的依赖注入框架。 其中之一是春天。可以使用注释在需要的地方提供依赖关系。如果您的项目已经在使用Spring,那么使用Spring是一个不错的选择。否则,它可能太大而且很麻烦。

Spring的一个简单易用的替代方法是使用PicoContainer。

有关或者了解更多信息,请看看:

http://www.thinkcode.se/blog/2017/06/24/sharing-state-between-steps-in-cucumberjvm-using-spring

http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer