gatling - Сохраняет все данные ответа в предыдущем вызове API, но как извлечь значение из переменной ответа в следующем вызове API - PullRequest
1 голос
/ 28 апреля 2019

В Гатлинге с использованием сценариев scala. Мне нужно извлекать choiceId для каждого вопроса в соответствии с questionTypes и questionId из предыдущего ответа и произвольно отправлять один выбор в следующем вызове API.

From an API_01 of the response I am saving the questionTypes, questionId's and also saving the whole response data in Resp using json path.
Now according using he no. of question ID's which is an array the loop iterates (if 4 question Id's then there are 4 question types and ll iterate 4 times) and also checks the question type for each iteration and switch is used to perform the function according to the question type.
After going inside the question type I need to answer for which I need ChoiceId's which will differ from question to question (which will have 3 choices or 2 choices or 5 choices)
So I have saved the whole response in the previous API_01 call now I need to extract it and add to API_02 call POST request json file; I have QuestionId and QuestionType

API_01 дляполучить questionID, questionType и ChoiceID

private val Assessment = exec(http("assessment")
    .post("/Test-graphql")
    .headers(commonHeaders)
    .body(ElFileBody("/createAssessment.json"))
    .check(jsonPath("""$.data.assessment.items[*].question.id""").findAll.saveAs("questionIds"))
    .check(jsonPath("""$.data.assessment.items[*].question.type""").findAll.saveAs("questionType"))
    .check(jsonPath("""$.data.assessment""").findAll.saveAs("ResponseData"))
  ) 

Ответ вышеприведенного вызова API

 {
  "data": {
  "assessment": {
  "id": "sample01",
  "items": [
    {
      "question": {
        "id": "sample02",
        "code": "E_01",
        "version": 1,
        "type": "MULTIPLE_SELECTION",
        "language": "E",
        "body": {
          "choices": {
            "minChoice": 1,
            "maxChoice": 7,
            "choiceItems": [

              {
                "choiceId": 2, --> How to get these choiceID for answer Submission
              },
              {
                "choiceId": 3, --> How to get these choiceID 
              },
              {
                "choiceId": 115, --> How to get these choiceID 
              },
              {
                "choiceId": 196, --> How to get these choiceID 
              }
            ]
          },
        },
      },
      "submissions": [

      ],
    }, 

{
      "question": {
        "id": "sample02",
        "code": "E_01",
        "version": 1,
        "type": "FILL_IN_THE_BLANK",
        "language": "E",
        "body": {
          "choices": {
            "minChoice": 1,
            "maxChoice": 7,
            "choiceItems": [

              {
                "choiceId": 20,  --> How to get these choiceID 
              },
              {
                "choiceId": 15,
              },
              {
                "choiceId": 11,
              },
              {
                "choiceId": 156,
              }
            ]
          },
        },
      },
      "submissions": [

      ],
    }

    ]
} } }

Метод во время отправки ответа:

 private val Answers = foreach("${questionId}", "id","i") {

    doSwitch("${questionTypes(i)}")(
      "MULTIPLE_CHOICE" -> answerMCQ,
      "MULTIPLE_SELECTION" -> answerMSQ
      "FILL_IN_THE_BLANK" -> answerFIB
    )

Пример функции ответа

private val answerFIB = exec(
        http("Submit Answer")
          .post("/submitanswer-grapql")
          .headers(commonHeaders)
          .body(ElFileBody("data/AnswerFIB.json"))
          .check(status.is(200))
          .check(jsonPath("$.data.assessmentAnswerSubmit.id").is("${assessmentId}"))
)

Пример ответаFIB.json для представления ответа:

    "answer": {
        "ChoiceId": 354,--> Here I need to provide Choice ID which changes for each question answer
        "answer": "Sample01",
        "type": "FILL_IN_THE_BLANK"
    }
...