2014-02-11 67 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