问题
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.
回答1:
This can be done by using the @Transform annotation in the stepdefinition. Also the student list string in the feature file looks like a Json 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 + "]";
}
}
来源:https://stackoverflow.com/questions/49908720/passing-a-name-and-list-of-object-in-feature-file-in-cucumber