【问题标题】:Build Dynamic json request using Gatling使用 Gatling 构建动态 json 请求
【发布时间】:2020-04-17 06:41:25
【问题描述】:

我正在尝试使用 csv 文件中的数据构建随机 json 请求。我已经定义了我的 .scenario 函数如下

但是当我将日志级别设置为跟踪时,我看到所有请求都具有相同的值。有什么我错过的吗?

def getScenario () = {
         scenario("Create API Promotions")
        .exec(
            http("Create Request")
            .post(createApiURL)
            .headers(headers_1)
            .body(StringBody(getCreateRequest))
            .check(status.is(200))
            )
    }


    def getCreateRequest: String = {
         val data = s"""
          {
            "Specification":{  

                "Item":${getItems()}
            }
          }
          """.stripMargin
          data   
    }

def getItems (): String = {
        val record: Map[String, Any] = getItemsFromCSV()
        val code: String = record("Code").toString
        val clientDataType: String = record("Type").toString

        val clientData =    (
                                ("Code" -> code) ~
                                ("Type" -> clientDataType)                                 
                            )
        val targetJson = List(clientData);
        return compact(render(targetJson))
    }

    def getItemsFromCSV() : Map[String, Any] = {

            val items: Seq[Map[String, Any]] = csv("../resources/create/items.csv").readRecords
            return promoTarget(getRandomNumber(0, items.length-1)) 
    }

【问题讨论】:

    标签: gatling scala-gatling


    【解决方案1】:

    Gatling EL 提供了一个内置函数 (jsonStringify()),可以将对象正确格式化为 JSON 值(用双引号包裹字符串、处理 null 等)

    此外,我认为您可以使用馈线来获取 csv 值。

    val items = csv("../resources/create/items.csv").random
    
    scenario("Create API Promotions")
      .feed(items)
      .exec(buildPostData)
      .exec(http("Create Request")
        .post(createApiURL)
        .headers(headers_1)
        .body(StringBody("${postData.jsonStringify()}"))
        .check(status.is(200))
      )
    
    def buildPostData: Expression[Session] = session => {
      val postData: Map[String, Any] =
          Map(
            "Code" -> session("Code").as[String],
            "Type" -> session("Type").as[String]
          )
      session.set("postData", postData)
    }
    

    官方文档链接:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多