New to scala/gatling, need to remove env variables from git upload

Hi everyone, I have a working graphql test that passes in four headers that shouldn’t be publicly exposed. I have this running locally and I want to share it with my team on github. In other languages, I create an .env and .env.example file. I upload the .env.example file with empty values for the key pairs. I ask my team to contact me for the values to put in their .env file. I put my full key value pairs in the .env file and then I put .env in .gitignore

I don’t know how to do something like this for scala. If I do my normal .env file and then try to pass the parameter like such

.header(“uid”, $UID)

the file does not recognize $UID

Google/S.O searches say to use JAVA_OPTS but they don’t say how to use java opts.

I tried adding it to the run configuration, but the run configuration doesn’t recognize my test because it’s .scala instead of .class
I tried this but no luck :https://devqa.io/parameterize-gatling-variables/
I tried this too https://stackoverflow.com/questions/53068918/passing-command-line-parameters-in-gatling-sbt-project
but adding anything into my class seems to create 500 errors as the test doesn’t expect vals outside of functions. I guess that’s my biggest problem in general reading answers on line, is even when I get the answer, I have no idea where to copypasta the code into. In my class, in another file? No idea

Here’s my test with the params and company info abstracted so I’m not blasting it out to the world:

package specs

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class GraphQLSimulation extends Simulation {

val httpProtocol = http
.baseUrl(“http://api.staging..com”) // Here is the root for all relative URLs
.header(“uid”, “<uid”)
.header(“access-token”, “<access_token>”)
.header(“expiry”, “”)
.header(“client”, “”)
.header(“token-type”, “Bearer”)
.header(“app”, “mover”)

val scn = scenario(“GraphQLScenario”)
.exec(http(“request_1”)
.post("/graphql"))
.pause(2)

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

Hello,

I would like to make some things clear because lots of people have a preconceived idea that they need to know Scala in order to use Gatling. They don’t.

First, Scala runs on a JVM and can use any Java API.
Then, since Gatling 3.7, Gatling provides a Java DSL, which I would really recommend for newcomers as Java knowledge is way more widespread than Java’s.

So your question has actually nothing to do with being new to Gatling or new to Scala.
It’s simply “how do I resolve environment variables in Java?”, which is something that you can definitely figure out for yourself with a quick Google search.

Hope it helps,

Thanks. I’m still spinning a bit. I’m not a developer and have never used java in a distributed environment where I’ve had to do env variables as everything’s always been on my own machine. The google search results in a lot of responses around system variables and setting $PATH/$JAVA_HOME but not so much on creating your own variables and where to do that. I’m seeing some hints to do it in the pom file but I seem to have gone down a bad road there. I was seeing other hints to do it in the terminal, but that seems focused on system variables again. There’s hints that dotenv might work similar to python, but I’m also hitting a wall there. I seem to be going a little bit in every direction and confusing myself more.

I guess I should also point out that while researching my echo $JAVA_HOME came up empty, which hasn’t blocked me from running tests so I haven’t worried about it. To read system environment vars (think these are different than custom env variables but it’s a start), I set $JAVA_HOME to Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/home and this seems to now have broken the previously working gatling test

julie.laursen@Julie’s-MacBook-Pro gatling-POC % mvn gatling:test
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
julie.laursen@Julie’s-MacBook-Pro gatling-POC % echo $JAVA_HOME
Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/home

Ok, I think I am as close as I can be :

https://gatling.io/docs/gatling/guides/passing_parameters/

So I changed my code to this:

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

import scala.concurrent.duration._
class GraphQLSimulation extends Simulation {

val expiry = Integer.getInteger(“expiry”)

val httpProtocol = http
.baseUrl(“http://api.staging..com”)
.header(“uid”, )
.header(“access-token”, “”)
.header(“expiry”, expiry:1)
.header(“client”, “”)
.header(“token-type”, “Bearer”)
.header(“app”, “mover”)

val scn = scenario(“GraphQLScenario”)
.exec(http(“request_1”)
.post("/graphql"))
.pause(2)

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

Expiry is an integer. I assumed this would work and I would run the test like this mvn gatling:test -Dexpiry=12345

Except .header(“expiry”, expiry:1) has a red squiggly line under it and the error message is ‘cannot upcast integer to 1’. No idea what’s going on here.

What are you trying to achieve with this expiry:1. It’s not valid Scala syntax.

It’s a header that I thought was required but it actually looks like I can leave it off and the POST will still be successful. So, I’ll remove it from my code. However, in attempts to write my graphql query into my test, I ended up trying to import gatling-core and gatling-http in my pom.xml and somehow ended up in a dependency issue. I still haven’t got to the point where I’m extrapolating out the environment variables just due to my general newbie-ness around system variables and non-system variables and using Java for this. Being a non-developer, I have a hard time sometimes implementing a stack overflow answer as I just generally don’t know where to implement it more than anything else.


Error: Option --simulations-folder failed when given '/Users/julie.laursen/Workspace/Performance/Gatling2/gatling-POC/src/test/scala'. 'io.gatling.core.config.GatlingPropertiesBuilder io.gatling.core.config.GatlingPropertiesBuilder.simulationsDirectory(java.lang.String)'
Try --help for more information.

Working on that today

I thought this was needed as header but it looks like it’s not required so I can omit that from my code.

Still having issues understanding what I’m reading on google/stack overflow about java variables and specifically where to implement them (i.e. is System.getEnv() something I put in my test file? etc) and whether I’m going down the wrong path reading about system variables when I want custom variables. However, I now have a new error to contend with before I can get back to that:

Error: Option --simulations-folder failed when given ‘/Users/julie.laursen/Workspace/Performance/Gatling2/gatling-POC/src/test/scala’. ‘io.gatling.core.config.GatlingPropertiesBuilder io.gatling.core.config.GatlingPropertiesBuilder.simulationsDirectory(java.lang.String)’

Will update soon.