2014-10-10 101 views
0

我想使用硒驗證日期。所以我的情況是,Selenium日期比較示例

Scenario: Date validation 
When I set field <field> to value <value> 
And I save the form 
Then I should see error message <message> 

Examples: 
| field | value | message | 
| startDate | 01/01/2014 | End date should be greater than start date | 
| endDate | 01/01/2014 | End date should be greater than start date | 

我填充字段的步驟方法如下所示:

@When("I set field <field> to value <value>") 
public void populateField(@Named("field") String fieldName, @Named("value") String value) { 
    populateFieldValue(fieldName, value); 
} 

我的問題是由於一步方法是通用的,如何設置開始日期,然後結束日期,並在單個例子線顯示錯誤消息。

請建議。

回答

0

可以使用StringListConverter到值列表傳遞給參數

簡單的例子:

Scenario: Date validation 
When I set field <field> to value <value> 
Then I should see error message <message> 

Examples: 
| field | value | message | 
| startDate | 01/01/2014 | End date should be greater than start date | 
| endDate | 01/01/2014 | End date should be greater than start date | 
| startDate,endDate | 01/01/2014, 01/01/2014 | display the error message in a single example line. | 

和測試代碼:

@When("I set field <field> to value <value>") 
public void xxxx(@Named("field") List<String> fields, 
       @Named("value") List<String> values){ 

    System.out.println(">>>>>------------->>>>----------->>>>>"); 
    for(int i = 0; i < fields.size(); i++){ 
     String field = fields.get(i); 
     String value = values.get(i); 
     System.out.println(" --> Set field: " + field + " to value: " + value); 
    } 

} 
@Then("I should see error message <message>") 
public void when(@Named("message") String message){ 
    System.out.println(" -------> message = " + message); 
} 

上述測試生成此結果:

Running story main/resources/test.story 
>>>>>------------->>>>----------->>>>> 
--> Set field: startDate to value: 01/01/2014 
-------> message = End date should be greater than start date 
>>>>>------------->>>>----------->>>>> 
--> Set field: endDate to value: 01/01/2014 
-------> message = End date should be greater than start date 
>>>>>------------->>>>----------->>>>> 
--> Set field: startDate to value: 01/01/2014 
--> Set field: endDate to value: 01/01/2014 
-------> message = display the error message in a single example line.