在黄瓜的特征文件中传递名称和对象列表 [英] passing a name and List of object in feature file in cucumber
本文介绍了在黄瓜的特征文件中传递名称和对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在黄瓜的功能文件中传递类似的内容
功能:在XLR CD API上测试不同的请求
方案:检查用户是否可以访问学生应用程序
方案大纲:创建新学生当我通过提供信息studentcollege< studentcollege>创建新学生时,验证是否添加了该学生
。 studentList< studentList>
然后,我确认具有< student>创建
示例:
|学生学院|学生列表|
| abcd | [{student_name: student1, student_id: 1234},{student_name: student1, student_id: 1234}]] |
我上课为
班级学生{
字符串名称;
字符串ID;
}
步骤定义文件为
@When( ^当我通过提供信息来创建新学生时StudentCollege(。*)studentList(。*)$)
中获得的值在此处获取值pre>
public void generatetudent(String studentOwner,List< Student> listOfstudent){
//需要从功能文件
}
如何在特征文件示例中传递此类值。因此可以在步骤定义功能中检索。
解决方案这可以通过使用
@Transform
批注来完成步进定义。此外,功能文件中的学生列表字符串看起来像Json
字符串,因此最容易使用Gson进行解析。
相关方案
方案大纲:创建新学生当我通过提供信息studentcollege< studentcollege>创建新学生时,验证是否添加了该学生
。 studentList< studentList>
示例:
|学生学院|学生列表|
| abcd | [{{ student_name: student111, student_id: 1234},{ student_name: student222, student_id: 5678}]] |
Stefdefinition class
@When( ^我通过提供信息studentcollege(。*?)studentList(。*?)$创建新学生。
public void iCreateANewStudentByProvidingTheInformation(String arg1,@Transform(StudentListTransformer。 class)List< Student> arg3){
System.out.println(arg1);
System.out.println(arg3);
}
变压器舱
公共类StudentListTransformer扩展了Transformer< List< Student>> {
@Override
public List< Student> transform(String value){
//样本json-[{'name':'student100','id':'1234'},{'name':'student200','id':'5678 '}]
返回new Gson()。fromJson(value,ArrayList.class);
}
}
学生dataobject-
公共班级学生{
私有字符串名称;
私人字串ID;
public String getName(){
返回名称;
}
public void setName(String name){
this.name = name;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
@Override
public String toString(){
return Student [name = + name +,id = + id +] ;
}
}
I want to pass something like this in feature file in cucumber
Feature: Testing different requests on the XLR CD API Scenario: Check if the student application can be accessed by users Scenario Outline: Create a new student & verify if the student is added When I create a new student by providing the information studentcollege <studentcollege> studentList <studentList> Then I verify that the student with <student> is created Examples: | studentcollege | studentList | | abcd | [{student_name": "student1","student_id": "1234"},{student_name": "student1","student_id": "1234"}] |
I have class as
Class Student{ String name; String id; }
and step definition file is
@When("^When I create a new student by providing the information studentCollege (.*) studentList (.*)$") public void generatestudent(String studentOwner, List<Student> listOfstudent) { // need to fetch values in here from whatever is given in feature file }
how to pass such values in feature file Example. so that can be retrieved in step definition function.
解决方案This can be done by using the
@Transform
annotation in the stepdefinition. Also the student list string in the feature file looks like aJson
string, so easiest to parse using Gson.Relevant scenario
Scenario Outline: Create a new student & verify if the student is added When I create a new student by providing the information studentcollege <studentcollege> studentList <studentList> Examples: | studentcollege | studentList | | abcd | [{"student_name": "student111","student_id": "1234"},{"student_name": "student222","student_id": "5678"}] |
Stefdefinition class
@When("^I create a new student by providing the information studentcollege (.*?) studentList (.*?)$") public void iCreateANewStudentByProvidingTheInformation(String arg1, @Transform(StudentListTransformer.class)List<Student> arg3) { System.out.println(arg1); System.out.println(arg3); }
Transformer class
public class StudentListTransformer extends Transformer<List<Student>>{ @Override public List<Student> transform(String value) { //Sample json -- [{'name': 'student100','id': '1234'},{'name': 'student200','id': '5678'}] return new Gson().fromJson(value, ArrayList.class); } }
Student dataobject-
public class Student { private String name; private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public String toString() { return "Student [name=" + name + ", id=" + id + "]"; } }
这篇关于在黄瓜的特征文件中传递名称和对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文