2013-10-08 28 views
3

我想知道是否有一種方法,我可以訪問示例表格行數據中的一個步驟的方法,而不需要作爲參數傳遞?訪問JBehave示例表中的數據在步驟

故事文件:

Given I am logged in 
When I create a trade 
Then a trade should be created 

Examples: 
|data1|data2| 
|11111|22222| 
|33333|44444| 

步驟文件:

@When("I create a trade") 
public void createTrade(@Named("data1") String data1, @Named("data2") String data2){ 
    //code to create trade using data1 and data2 
} 

以上工作正常,但我想辦法在方法中從實例訪問表中的數據列。 (我想這樣做的原因是因爲所有列可能不會出現在每個故事中的示例表中,並且我發現如果在step方法中將3 * @Named稱爲參數,但其中之一是從實際的例子表中缺少那麼就無法運行。)

@When("I create a trade") 
public void createTrade(){ 
    //check if there is a data1 column, if so get value and do something 
    //check if there is a data2 column, if so get value and do something 
} 

感謝您的幫助

回答

2

您可以實現一個新的參數轉換器,然後通過該表作爲對象。 例如,在我們的項目中,我們建立ExamplesTableConverter(注:有一個箱子JBehave轉換器,我們沒有測試我們):

public class ExamplesTableConverter extends ParameterConverters.ExamplesTableConverter { 
private final ExamplesTableFactory factory; 
private static final String ROW_SEPARATOR = "\n"; 
private static final String FIELD_SEPARATOR = "|"; 

public ExamplesTableConverter(){ 
    this(new ExamplesTableFactory()); 
} 

public ExamplesTableConverter(ExamplesTableFactory factory){ 
    this.factory = factory; 
} 

@Override 
public boolean accept(Type type) { 
    if (type instanceof Class<?>){ 
     return ExamplesTable.class.isAssignableFrom((Class<?>) type); 
    } 
    return false; //To change body of implemented methods use File | Settings | File Templates. 
} 

@Override 
public Object convertValue(String tableAsString, Type type) { 
    System.out.println(tableAsString); 
    String[] rows = tableAsString.split(ROW_SEPARATOR); 
    StringBuffer resultString = new StringBuffer(); 
    resultString.append(rows[0]); 
    resultString.append(ROW_SEPARATOR); 

    for(int i=1; i<rows.length; i++){ 
     String originRow = rows[i]; 
     List<String> rowValues = TableUtils.parseRow(originRow, FIELD_SEPARATOR, true); 

     String translatedRow = translateRow(rowValues); 
     resultString.append(translatedRow); 
     resultString.append(ROW_SEPARATOR); 
    } 
    System.out.println(resultString.toString()); 
    Object table = factory.createExamplesTable(resultString.toString()); 
    return table; 
    //return null; 
} 

private String translateRow(List<String> rowValues) { 
    StringBuffer result = new StringBuffer(FIELD_SEPARATOR); 

    for(String field : rowValues){ 
     try{ 
     result.append(translate(field)); 
     result.append(FIELD_SEPARATOR);} 
     catch (LocalizationException e){ 
      e.printStackTrace(); 
      //Need do something here to handle exception 
     } 
    } 
    return result.toString(); 
} 

}

,那麼你需要的是轉換器加到你的配置:

configuration.parameterConverters().addConverters(new ExamplesTableConverter()); 

創建使用它

@When("create parameters of type $param1 from the next table: $table")  
public void doThis(@Named("param1") String param1, @Named("table") ExamplesTable table) 
的方法

和最後一個,使用它在一個故事:

When create parameters of type type1 from the next table: 
|FirstName|LastName| 
|Donald|Duck| 

這將允許您遍歷表。

下面的博客文章也可以幫助你 Simpler JBehave Examples Table Processing

0

情景: -

Given data insert in DemoSheet 
|demoNumber|demoName| 
|101|Demo1| 
|102|Demo2| 
|103|Demo3| 
|104|Demo4| 
|105|Demo5| 

java類從場景獲取值: -

@Given("data insert in DemoSheet $parameterTable") 
public void setDataToSheet(ExamplesTable parametersTable) 
     throws RowsExceededException, WriteException, IOException { 

    List<String> demonum = new ArrayList<String>(); 
    List<String> demoname = new ArrayList<String>(); 

    for (Map<String, String> row : parametersTable.getRows()) { 
     Iterator it = row.entrySet().iterator(); 
     while (it.hasNext()) { 
      Map.Entry pairs = (Map.Entry) it.next(); 
      if (pairs.getKey().equals("demoNumber")) { 
       demonum.add((String) pairs.getValue()); 
      } else if (pairs.getKey().equals("demoName")) { 
       demoname.add((String) pairs.getValue()); 
      } 
     } 
    } 
    for(String s:demonum) 
    { 
     System.out.println(s.getDemonum); 
     System.out.println(s.getDemoname); 
    } 
} 

我們可以通過獲取多行示例表參數在這裏,我從場景到java類傳遞多行..

相關問題