简体   繁体   中英

How can I import a scala class into another using gatling?

note: I am new to gatling and know almost nothing about Scala.

I am starting the process of converting my load tests from Jmeter to gatling. And I am stuck on how to organize the code base. All the examples that I have been able to find are single file examples.

How can import code from one simulation class into another?

I have this class and test scenario working now:

    package default

import scala.concurrent.duration._

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

class createGuestUser extends Simulation {


    val userPrefix = System.getProperty("userPrefix", "gatling_load_test") + "_" + scala.util.Random.nextInt + "_"
    val password = System.getProperty("password", "1234567")
    val hostname = System.getProperty("hostname", "http://0.0.0.0")
    val blank_headers = Map("Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")


    val httpConf = http
        .baseURL("http://0.0.0.0")

    object GetClientToken {     
        val slash = exec(http("Slash")
        .get("/")
        .headers(blank_headers)
        .check(regex("""var appToken = '(.*)';""").find.saveAs("xGlooApplication")) // var appToken = '60e5814d-9271-43b4-8540-157d1c743651';       
        )
    }
.....

And when I try to import the class into another simulation like this:

    package default

import scala.concurrent.duration._

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

import createGuestUser._

class createAccount extends Simulation {

I get the following error when trying to import.

08:33:57.952 [ERROR] igcZincCompiler$ - /Users/dclements/Dev/Gloo/load_testing/gatling/src/createAccount.scala:9: not found: object createGuestUser 08:33:57.954 [ERROR] igcZincCompiler$ - import createGuestUser._

just to make compiler happy,

amend declaration: class createGuestUser extends Simulation

to: object createGuestUser extends Simulation

and then you can:

import default.createGuestUser._

Simulations should not be dependent each other. I would extract common code to separate classes eg SimulationSetup, ...Scenario

Check the advanced tutorial from the official documentation. There's also a link to sources at the end of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM