2016-11-29 19 views
0

我在小黃瓜獲取黃瓜,JVM步作爲字符串在Java中

Scenario: Correct login should take the user to the next screen 
    Given User is on the login screen 
    When User enters username as "Donald" 
    And User enters password as "Trump" 
    And User clicks the login button 
    Then User should be taken to the next screen 

寫了下面的場景,我有以下步驟黃瓜JVM映射到這個場景

... 
@Given("^User is on the login screen$) 
public void goToLoginScreen(){ 
    //some logic 
} 

如何在映射的步驟定義方法內獲取@Given註釋之間的內容。我有幾個用例,這會有所幫助,但無法找出目前的情況。

回答

0

將括號中的整個步驟包括在內。這將是第一個被捕獲的組。

@Given("^(User is on the login screen)$) 
public void goToLoginScreen(String stepText){ 
    System.out.println(stepText); 
    //some logic 
} 
0

您可以使用捕獲組來捕獲步驟中的變量。例如:

@When("^User enters username as (.*)$") 
public void entersUsernameAs(String username){ 
    //some logic 
} 

您也可以在一個步驟中使用多個變量。例如:

@Given("^User logs in as (.*) with password (.*)$") 
public void usersLogsInAs(String username, String password){ 
    //go to loginpage 
    //enter username 
    //enter password 
    //click login button 
} 

您可以使用不同的捕獲組。我通常使用(.*)作爲字符串,可以是任何東西,整數可以是(\\d+)。 您還可以使用(option|other|else)作爲有限的一組字符串(在此示例中爲"option","other""else"),它只會匹配使用這些字符串的步驟,而不是像(.*)這樣的任何字符串。