首页 > 解决方案 > Gatling - 无法解析为正确的 Uri,缺少方案

问题描述

我有基本的 Gatling 脚本,我尝试在其中登录应用程序,仅此而已。
出于某种原因,我得到了错误:

j.l.IllegalArgumentException: "https://my-site.com/"login could not be parsed into a proper Uri, missing scheme  

在我看来,它已附加在 baseUrl 引号的末尾。

这是我的模拟课:

package simulations.stage

import io.ecx.steps.{Config, Login}
import io.gatling.core.Predef._
import io.gatling.http.Predef._

import scala.concurrent.duration._

class RampUsersLoadSimulations extends Simulation{

  val httpConf = http.baseUrl(Config.baseUrl)
    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
    .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36")
    .inferHtmlResources()
    .acceptEncodingHeader("gzip, deflate, br")

  before {
    println(s"Running for: ${Config.baseUrl}")
  }

  val login = scenario("Scenario: Login to the storefront")
    .exec(Login.login(Config.accounts))

  setUp(
    login.inject(
      nothingFor(5 seconds),
      atOnceUsers(Config.userCount),
      rampUsers(Config.userCount) during(Config.rampDuration seconds)
    ).protocols(httpConf)
  ).maxDuration(Config.testDuration seconds)
}  

这是我的登录步骤实现:

package io.ecx.steps

import io.gatling.core.Predef._
import io.gatling.http.Predef._

object Login {

  def login(accountsPath: String) = {
    val accounts = csv(accountsPath).random

    feed(accounts)
      .exec(http("Open login page")
      .get("login")
      .check(status.is(200)))
      .pause(2)
      .exec(http("Log in with credentials to the storefront")
        .post("j_spring_security_check")
        .formParam("email", "${username}")
        .formParam("password", "${password}"))
      .pause(5)
  }
}  

这是我的配置步骤实现:

package io.ecx.steps

import com.typesafe.config.ConfigFactory

object Config {

  private def getProperty(propertyName: String, defaultValue: String) = {
    Option(System.getenv(propertyName))
      .orElse(Option(System.getProperty(propertyName)))
      .getOrElse(defaultValue)
  }

  def userCount: Int = getProperty("USERS", "5").toInt
  def rampDuration: Int = getProperty("RAMP_DURATION", "10").toInt
  def testDuration: Int = getProperty("DURATION", "12").toInt
  def environment:String = getProperty("ENVIRONMENT", "STAGE")

  val conf = ConfigFactory.load()
  val accounts = conf.getString("accounts")
  val baseUrl = if (environment == "STAGE") conf.getString("baseUrlStage") else conf.getString("baseUrlProd")
}  

在我的属性文件中,我有:

#COMMON
accounts=data/stage/accounts.csv

#STAGE
baseUrlStage="https://my-site.com/"

#PROD
baseUrlProd="TBD"

有人可以帮忙吗:)
谢谢!

标签: scalagatling

解决方案


错误是明确的:"https://my-site.com/"login无法解析为正确的 Uri,缺少方案。

实际上,您的属性文件中的值已损坏。

而不是baseUrlStage="https://my-site.com/",你应该有baseUrlStage=https://my-site.com/


推荐阅读