在黄瓜中传递功能文件中的名称和对象列表

KRR*_*R16 5 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"}]  | 
Run Code Online (Sandbox Code Playgroud)

我有课作为

Class Student{
     String name;
     String id;
}
Run Code Online (Sandbox Code Playgroud)

步骤定义文件是

    @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

}
Run Code Online (Sandbox Code Playgroud)

如何在功能文件示例中传递此类值。这样就可以在步骤定义函数中检索。

Gra*_*per 2

@Transform这可以通过使用步骤定义中的注释来完成。此外,特征文件中的学生列表字符串看起来像一个Json字符串,因此最容易使用 Gson 进行解析。

相关场景

  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"}] |
Run Code Online (Sandbox Code Playgroud)

定义类

@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);
}
Run Code Online (Sandbox Code Playgroud)

变压器类

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

学生数据对象-

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 + "]";
    }
}
Run Code Online (Sandbox Code Playgroud)