加特林-在上一个API调用中保存了整个响应数据,但如何在下一个API调用中从响应变量中提取值

时间:2019-04-28 15:45:05

标签: scala gatling scala-gatling gatling-jsonpath

在使用Scala脚本的加特林机中。我需要根据上一个响应中的问题类型和问题ID提取每个问题的选择ID,并在下一个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获取问题ID,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}"))
)

示例AnswerFIB.json以提交答案:

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

0 个答案:

没有答案
相关问题