需要使用java在自由标记模板中添加多个字段
我正在研究rest-assured API测试框架。我有不同的API调用来post/put。 使用免费标记模板创建我的post/put调用。我需要为每个请求输入不同的值来测试不同的场景。我可以在模板中成功传递一个值,但需要更多值时无法传递。需要一些循环,但不知道如何。 下面是我的功能文件和重新设置的类的代码需要使用java在自由标记模板中添加多个字段,java,automation,automated-tests,cucumber,freemarker,Java,Automation,Automated Tests,Cucumber,Freemarker,我正在研究rest-assured API测试框架。我有不同的API调用来post/put。 使用免费标记模板创建我的post/put调用。我需要为每个请求输入不同的值来测试不同的场景。我可以在模板中成功传递一个值,但需要更多值时无法传递。需要一些循环,但不知道如何。 下面是我的功能文件和重新设置的类的代码 Scenario Outline: to post data # Given user hit post button When user sends the as follow
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"
}
}
场景大纲:发布数据
#给定用户点击post按钮
当用户发送以下信息时
|关键值|
|guid | abcd|
|第一名|测试名|
|姓氏|
然后,用户将获得“”的成功结果
示例:
|名字|
|测试7|
第2行的步骤定义
@当(“用户发送如下信息”)
public void usersendstasfollow(DataTable表)引发IOException{
列表行=table.asMaps(String.class,String.class);
用于(映射列:行){
post(columns.get(“键”)、columns.get(“值”)、表);
}
}
///////////
导入java.io.*;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.*;
导入freemarker.template.Configuration;
导入freemarker.template.template;
导入freemarker.template.TemplateException;
导入io.Cumber.datatable.datatable;
导入org.json.JSONObject;
公共类自由市场测试{
公共文件输入流文件;
公共字符串有效载荷;
JSONObject Freemarker对象;
文件温度;
公共JSONObject freemarker(字符串键、字符串值){
配置cfg=新配置(配置.版本2\u 3\u 20);
试一试{
//从源文件夹加载模板
Template Template=cfg.getTemplate(“到tepmlate/JsonTemplates/postmplate.ftl的路径”);
//建立数据模型
映射数据=新的HashMap();
//将guid放入文件中
数据输入(键、值);
//数据输入(“名”、“名”);
//控制台输出
Writer out=新的OutputStreamWriter(System.out);
模板、流程(数据、输出);
out.flush();
temp=File.createTempFile(“freemarker”,“.json”,新文件(“/path to folder/target/temp”);
System.out.println(“在以下位置创建的临时文件:+Temp.getAbsolutePath());
Writer file=newfilewriter(新文件(“文件夹路径/target/temp/”+temp.getName());
System.out.println(“在以下位置创建的名称文件:“+”文件夹路径/target/temp”+temp.getName());
模板、流程(数据、文件);
flush()文件;
file.close();
有效负载=新字符串(Files.readAllBytes(path.get(“target/temp/”+temp.getName()));
JSONObject freemarkerObject=新JSONObject(有效负载);
返回freemarkerObject;
}捕获(IOException | TemplateException e){
e、 printStackTrace();
返回freemarkerObject;
}
}
}
///postTemplate.ftl
{
“guid”:“${guid}”,
“联系方式详情”:{
“first_name”:“${first_name}”,
“姓氏”:“${last_name}”,
“电子邮件”:abcd@test.com",
“电话”:“02112231223”
}
}
请帮我把guid,名字和姓氏放在文件里
提前谢谢