Selenium日期比较示例

时间:2014-10-10 09:30:14

标签: selenium selenium-webdriver jbehave

我正在尝试使用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);
}

我的问题是,由于步骤方法是通用的,如何设置开始日期,然后结束日期并在单个示例行中显示错误消息。

请建议。

1 个答案:

答案 0 :(得分: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.