我正在使用黄瓜测试GUI。我需要测试GUI的CRUD操作。
当我编写用于在GUI中创建新实体的方案时,我无法多次运行,因为第二次方案失败,因为我为该实体指定的ID在系统中已经存在(在第一次运行中创建)我第二次运行测试。
我正在测试的系统不允许删除实体。需要以特殊模式启动系统才能删除实体,因此,删除测试后创建的实体不是一种选择。
如果我可以使用随机数作为实体ID,那就太好了。例如:
when user creates a new Branch with following values:
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|
And user searches for a branch by "Branch ID" = "<random_string_1>"
Then branch details should be as following
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|
黄瓜中是否可以做这样的事情?或者,还有其他方法可以实现这一目标吗?
答案 0 :(得分:2)
这是我最后所做的。
将此转换器类添加到测试套件中。
public class RandomStringTransformer extends Transformer<String> {
private static final Map<String, String> RANDOM_STRINGS = new HashMap<>(); //Key -> random string
public static final RandomStringTransformer INSTANCE = new RandomStringTransformer();
@Override
public String transform(String input) {
return transformString(input);
}
public DataTable transform(DataTable dataTable) {
dataTable.getGherkinRows().forEach(dataTableRow -> dataTableRow.getCells().replaceAll(this::transformString));
return dataTable;
}
private String transformString(String input) {
final String[] inputCopy = {input};
Map<String, String> replacements = new HashMap<>();
Matcher matcher = Pattern.compile("(<random_string_[^>]*>)").matcher(input);
while (matcher.find()) {
String group = matcher.group(0);
replacements.put(group, RANDOM_STRINGS.computeIfAbsent(group, key -> Utilities.getNextUniqueString()));
}
replacements.forEach((key, value) -> inputCopy[0] = inputCopy[0].replace(key, value));
return inputCopy[0];
}
}
在步骤def中使用了变压器。
@When("^user creates a branch of name "([^"]*)" with following values$")
public void branchIsCreatedWithDetails(@Transform(RandomStringTransformer.class) String branchName, DataTable fieldValues) {
fieldValues = RandomStringTransformer.INSTANCE.transform(fieldValues);
//Now, fieldValues table values and branchName are replaced with random values if they were in format <random_string_SOMETHING>
}
答案 1 :(得分:1)
Cucumber 3不再支持该变压器注释。因此,您必须替换为常规形式:
@When("^user creates a branch of name "([^"]*)" with following values$")
public void instanceTableIsFilteredForUniqueInstanceBy(String branchName, DataTable fieldValues) {
fieldValues = RandomStringTransformer.INSTANCE.transform(fieldValues);
//Now, fieldValues table values and branchName are replaced with random values if they were in format <random_string_SOMETHING>
}
请阅读以下内容以获取更多迁移信息:http://grasshopper.tech/98/