Search code examples
gatlingscala-gatling

Gatling: Extracting multiple groups in .check(regex


I'am try to use single .check(regex to extract multiple values. Code below represent extracting 3 groups.

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

After this I'am trying to use extracted values separately (e.g. description._1 as Tuple3, or description(1) as Collection). But it's always fails.

This works, but maybe there is more convenient way to do this (like 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)

Trying this

.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

This line

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

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

Solution

  • the gatling EL supports tuples so you can use calls like

    "${description._1}"
    

    to access the product, for example

    To get the value in order to use it somewhere other than in a dsl call that takes an Expression, you can just retrieve it in a session action (where you can't use EL)

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