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

2

3

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] i.g.c.ZincCompiler$ - /Users/dclements/Dev/Gloo/load_testing/gatling/src/createAccount.scala:9: not found: object createGuestUser 08:33:57.954 [ERROR] i.g.c.ZincCompiler$ - import createGuestUser._

Yclept answered 31/12, 2015 at 4:3 Comment(1)
i would suggest to start class name from upper caseAlbanese
A
2

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 e.g. SimulationSetup, ...Scenario

Albanese answered 21/4, 2017 at 22:12 Comment(1)
I would not use default package but in e.g. simulations.Albanese
B
-1

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

Bunkmate answered 31/12, 2015 at 9:7 Comment(4)
Thanks for the pointer. But the advanced tutorial does not start to break apart the test suite into multiple files. For example: It would be great if I could re-use the Browse object in another unrelated scenario.Yclept
Maybe it used to have this use case in the docs? gatling.io/docs/1.5.6/user_documentation/tutorial/…Yclept
I got it working following the instructions from the previous gatling docs on advanced usage. If you change your answer to point there I'll accept it.Yclept
@Yclept you can answer your own question, too. I'd love to see your solution, since I'm running into the same problem.Joliejoliet

© 2022 - 2024 — McMap. All rights reserved.