2014-03-25 53 views
11

我有一個特性文件,該文件是如下:黃瓜場景輪廓和示例與通用的步驟定義

Scenario Outline: Create ABC 

    Given I open the application 

    When I enter username as <username> 

    And I enter password as <password> 

    Then I enter title as <title> 

    And press submit 


Examples: 

| username | password | title | 

| Rob  | xyz1  | title1 | 

| Bob  | xyz1  | title2 | 

這責成我有步驟的定義,每個值的。我可以改爲具有可以被映射爲

示例部分每個用戶名或密碼或標題值

通用步驟定義。的

即不是說

@When("^I enter username as Rob$") 
public void I_enter_username_as_Rob() throws Throwable { 
    // Express the Regexp above with the code you wish you had 
    throw new PendingException(); 
} 

我可以進入

@When("^I enter username as <username>$") 
public void I_enter_username_as_username(<something to use the value passed>) throws Throwable { 
    // Express the Regexp above with the code you wish you had 
    throw new PendingException(); 
} 
+0

怎麼能這樣呢?它必須是一些舊的黃瓜插件,現在輪廓得到很好的支持,並且只在的情況下產生一個單一的通用步進功能。無需更改爲「」 – user1559625

+0

當通過JUnit執行時,仍然在CucumberJVM 1.2.5上發生。所以用引號。 :-) –

回答

23

你應該使用這種格式

Scenario Outline: Create ABC 

    Given I open the application 
    When I enter username as "<username>" 
    And I enter password as "<password>" 
    Then I enter title as "<title>" 
    And press submit 

將產生

@When("^I enter username as \"([^\"]*)\"$") 
public void I_enter_username_as(String arg1) throws Throwable { 
    // Express the Regexp above with the code you wish you had 
    throw new PendingException(); 
} 

arg1現在將傳遞您的用戶名/值。

+0

謝謝你的工作! – trial999

+2

謝謝!我還想解釋一下,「([^ \」] *)是一個正則表達式,它尋找一個以引號開始的字符串「,並以未定義的其他字符數結尾(星號*提供)。因此,如果我們看一下「」,我們可以看到它以引號開始「。希望可以幫助某人。作爲@hipokito澄清的 – hipokito

+1

,作爲**替代方式**所以將@When(」^我輸入用戶名爲\ 「(。+)\」 $「)' – DaddyMoe

0

黃瓜會自動給出控制檯中缺失的步驟。只需進行幹運行,並在控制檯中顯示缺少的步驟。

@RunWith(Cucumber.class) 
@CucumberOptions(plugin = { "pretty" }, features = { "<path_to_feature>" }, 
    glue = { "<optional_steps_location_java_file>" }, dryRun = true, 
    tags = { "<optional_NOT_req_for_now>" }) 
public class RunMyCucumberTest { 

} 

See for more Cucumber options