【问题标题】:Gatling: Extracting multiple groups in .check(regexGatling:在 .check(regex) 中提取多个组
【发布时间】:2019-06-26 13:03:33
【问题描述】:

我尝试使用单个 .check(regex 提取多个值。下面的代码表示提取 3 个组。

 val goToProduct = http("""GoTo_${product}""")
.get("""${product}""")
.headers(headers_0)
.check(regex("""name="([\s\S]+?)" value="(.+)" id="(.+)"""").ofType[(String,String,String)].saveAs("description")

在此之后,我尝试单独使用提取的值(例如,description._1 作为 Tuple3,或 description(1) 作为 Collection)。但总是失败。

这可行,但也许有更方便的方法来做到这一点(如 val._1)

session("description").validate[(String, String, String)].map { case 
(prod_name, prod_value, prod_id) =>
session.setAll("prod_name" -> prod_name, "prod_value" -> prod_value, 
"prod_id" -> prod_id)

试试这个

.exec { session => 
println(session("${description._1}").as[String]) 
session }

Will give an error: 'hook-1' crashed with 'j.u.NoSuchElementException: No attribute named '${description._1}' is defined', forwarding to the next one

这一行

println(session("description").as[String])

Shows Tuple3: (addtocart_37.EnteredQuantity,1,/addproducttocart/details/37/1)

【问题讨论】:

    标签: gatling scala-gatling


    【解决方案1】:

    gatling EL 支持元组,因此您可以使用类似的调用

    "${description._1}"
    

    例如访问产品

    要获取该值以便在接受表达式的 dsl 调用之外的其他地方使用它,您可以在会话操作中检索它(您不能使用 EL)

    exec(session => {
      println(session("description").as[(String, String, String)]._1)
      session
    })
    

    【讨论】:

    • 我正在尝试将此值打印到控制台。这是否可以将其作为值传递(例如 .get("${description._1}") ?因为在控制台中它显示错误(我已经编辑了问题)。
    • 更新的答案 - 你很接近。因为 gatling 会话是一个 Map(String->Any) ,所以您需要先指定要提取一个元组,然后才能使用 ._1 语法
    • 谢谢。我想知道为什么,当我打印“描述”时,它会在 () 中打印 3 个值。现在我明白了。
    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多