如何解决 Gatling 中的类型不匹配错误?

How to resolve type mismatch error in Gatling?

我在 "Login" 请求.formParam 中传递 session => 时出错。谁能帮忙?谢谢:-)

这是我的脚本,

// Random user login logout
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.core.validation._

class mamLoginLogout extends Simulation {

    val testServerUrl = System.getProperty("testServerUrl", "https://url")
    val username = System.getProperty("username", "TestUser")
    val password = System.getProperty("password", "passwd")
    val userCount = Integer.getInteger("userCount", 10).toInt
    val accountname = System.getProperty("accountname", "testrp")
    val endNum = Integer.getInteger("EndNumber", 10).toInt

    val httpProtocol = http
        .baseURL(testServerUrl)

    val scn = scenario("Login => Browse => Logout")
        .exec(http("Login")
            .post("/ma/a/" + accountname + "/login")
            .headers(headers_6)
            .formParam("username", session => username +  ThreadLocalRandom.current.nextInt(endNum))
            .formParam("password", session => password)
            // This script Runs perfact if I use below formParam (So its clear the problem is in above formParam with session =>, I think it conflicts with below session which is used in .foreach())
            //.formParam("username", username)
            //.formParam("password", password)
            )

    .exec(http("Browse")
            .get("/ma/assets/type=media%ofset=1%3Blimit=8")
            .headers(headers_12)
            .check(jsonPath("$..keyframeId").findAll.saveAs("IdList"))
            )

    .foreach("${IdList}", "id") {
            doIf(session => session("id").as[String] != "-1")
            {
                exec(http("Set_Keyframes")
                .get("/ma/keyframes/${id};width=185;height=103")
                )
            }
    }

    .exec(http("Logout")
        .get("/ma/logout")
        .headers(headers_80))       

    setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}

编译时失败,错误如下:

3674 [main] ERROR io.gatling.compiler.ZincCompiler$ - scala:68: type mismatch;
 found   : String("${IdList}")
 required: ?{def apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
 and method stringToExpression in object Predef of type [T](string: String)(implicit evidence: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T]
 are possible conversion functions from String("${IdList}") to ?{def apply: ?}

3674 [main] ERROR io.gatling.compiler.ZincCompiler$ -       .foreach("${IdList}", "id") {
3684 [main] ERROR io.gatling.compiler.ZincCompiler$ -                ^
3695 [main] ERROR io.gatling.compiler.ZincCompiler$ - scala:68: type mismatch;
 found   : String("id")
 required: ?{def apply: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps
 and method stringToExpression in object Predef of type [T](string: String)(implicit evidence: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T]
 are possible conversion functions from String("id") to ?{def apply: ?}
3695 [main] ERROR io.gatling.compiler.ZincCompiler$ -       .foreach("${IdList}", "id") {
3695 [main] ERROR io.gatling.compiler.ZincCompiler$ -                             ^
3994 [main] ERROR io.gatling.compiler.ZincCompiler$ - scala:53: not found: value ThreadLocalRandom
3995 [main] ERROR io.gatling.compiler.ZincCompiler$ -           .formParam("username", session => username +  ThreadLocalRandom.current.nextInt(endNum))
3995 [main] ERROR io.gatling.compiler.ZincCompiler$ -                                                         ^
4047 [main] ERROR io.gatling.compiler.ZincCompiler$ - three errors found
4048 [main] DEBUG io.gatling.compiler.ZincCompiler$ - Compilation failed (CompilerInterface)

已解决。忘记添加

import java.util.concurrent.ThreadLocalRandom

进口java.util.concurrent.ThreadLocalRandom:-)