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

时间:2018-04-18 20:43:39

标签: cucumber

我想在黄瓜的特色文件中传递这样的东西

INSERT INTO public.osoba(
    id_osoba, login, jmeno, prijmeni, heslo_hash)
    VALUES (13, 'test_login', 'test_jmeno', 'test_prijmeni', 'test_heslo_hash');

    ERROR:  column "id_osoba" does not exist
LINE 1: INSERT INTO osoba_zmeny SELECT 'I', now(), id_osoba, NEW.log...
                                                   ^
HINT:  There is a column named "id_osoba" in table "osoba_zmeny", but it cannot be referenced from this part of the query.
QUERY:  INSERT INTO osoba_zmeny SELECT 'I', now(), id_osoba, NEW.login
CONTEXT:  PL/pgSQL function osoba_zmeny() line 10 at SQL statement
SQL state: 42703

我上课了

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"}]  | 

和步骤定义文件是

Class Student{
     String name;
     String id;
}

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

1 个答案:

答案 0 :(得分:1)

这可以通过在stepdefinition中使用@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"}] |

Stefdefinition类

@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);
    }
}

学生数据对象 -

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 + "]";
    }
}