我正在研究放心的 API 测试框架。我有不同的 API 调用来发布/放置。 使用自由标记模板创建我的 post/put 调用。我需要为每个请求输入不同的值来测试不同的场景。我可以成功地在模板中传递一个值,但是当需要更多时不能这样做。需要一些循环,但不知道如何。 下面是我的功能文件和放心类的代码。
Scenario Outline: to post data
# Given user hit post button
When user sends the as follow
| key | values |
| guid | abcd |
| first_name | testname |
|last_name| lastname|
Then user gets success results for "<name>"
Examples:
| name |
| test7 |
Step definition for line 2
@When("user sends the as follow")
public void userSendsTheAsFollow(DataTable table) throws IOException {
List<Map<String, String>> rows = table.asMaps(String.class, String.class);
for (Map<String, String> columns : rows){
post(columns.get("key"), columns.get("values"),table);
}
}
///////////
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.cucumber.datatable.DataTable;
import org.json.JSONObject;
public class FreeMarkerTest {
public FileInputStream file;
public String payload;
JSONObject freemarkerObject;
File temp;
public JSONObject freemarker(String key, String value) {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_20);
try {
//Load template from source folder
Template template = cfg.getTemplate("path to tepmlate/JsonTemplates/postTemplate.ftl");
// Build the data-model
Map<String, String> data = new HashMap<>();
//put guid in file
data.put(key, value);
// data.put("first_name", "name");
// Console output
Writer out = new OutputStreamWriter(System.out);
template.process(data,out);
out.flush();
temp = File.createTempFile("freemarker",".json",new File("/path to folder/target/temp"));
System.out.println("Temp file created at location: " + temp.getAbsolutePath());
Writer file = new FileWriter (new File("path to folder/target/temp/"+temp.getName()));
System.out.println("name file created at location: " + "path to folder/target/temp"+temp.getName());
template.process(data, file);
file.flush();
file.close();
payload = new String(Files.readAllBytes(Paths.get("target/temp/"+temp.getName())));
JSONObject freemarkerObject = new JSONObject(payload);
return freemarkerObject;
} catch (IOException | TemplateException e) {
e.printStackTrace();
return freemarkerObject;
}
}
}
///postTemplate.ftl
{
"guid": "${guid}",
"contact_details": {
"first_name": "${first_name}",
"last_name": "${last_name}",
"email": "abcd@test.com",
"phone": "02112231223"
}
}
请帮我把 guid、first_name 和 last_name 放在文件中。
提前致谢