Gatling for loop in checkif conditional - scala

im really new to scala and gatling testing. What im trying to do is to extract parameters from response and save it to some session variabels. I want to pass map with variable name, and variable path to be extracted.
if(req.method == "GET"){
scn.exec(http(req.url).get("/uri").check(checkIf(!req.additionalParameters.isEmpty){
for(par <- additonalParameters){
jsonPath(par._2).saveAs(par._1)
}
}))
}
Im was trying something like this, but it dosen't compile and im wondering if it's even possible to do.
Thanks for all the help :)

That's not possible this way atm (as of Gatling 3.3.1), checkIf second parameter takes one single value. You need one checkIf per check.

Related

Gatling: Access variables from saved "findAll" list in foreach loop

I'm new to Gatling and Scala, and I had a hopefully quick and basic question about how to access the elements that are saved as from a findAll in the previous request.
The regex in the below code matches multiple button values. I eventually want to find the "max" button value (by something I'll come up with later), and based on that use that button in subsequent requests. However, I'm unable to actually access the values in button_list. In the terminal when I try to print the values, the values don't get substituted and literally print like this for each button:
Button ${count}: ${button}
Button ${count}: ${button}
Here's the snippet producing this:
...
.exec(http("click_ok")
.post("www.foo.com")
.headers(headers_0)
.formParam("_flowExecutionKey", "${flow_execution_key}")
.formParam("_eventId_submit", "${_eventId_submit}")
.check(regex("""foo(.*?)bar""").findAll.saveAs("button_list"))).exitHereIfFailed
.pause(1)
.foreach("${button_list}", "button", "count") {
exec(session => {
println("Button ${count}: ${button}")
session})
}
...
When I see the session print out in the logs, I can see that the buttons have matched and the session contains a list like the following, so I know there are successful matches:
button_list -> List(c11/98/280, c11/98/390)
Anyone have an example or know what I'm doing wrong?
Thanks!
As explained in the official documentation, Gatling Expression Language is not something that magically works anywhere. It only works when passing such String to a Gatling DSL method, not in your own code. You must use the Gatling Session API.

pytest function without return value

I'm trying to do a pytest on a function without return value, but obviously value is None in pytets. I was wondering if there is a solution for that?
here is function which I'm trying to test:
def print_top_movies_url():
for item in movie_link[:100]:
print item.contents[1]
The best thing to do would be to separate getting the top movies and printing them.
For example, you could have a top_movie_urls which looks like this:
def top_movie_urls():
urls = []
for item in movie_link[:100]:
urls.append(item.contents[1])
return urls
(or make it a generator function)
That's easy to test, and wherever you call it, you can now just do something like print('\n'.join(top_movie_urls())).
If you really want to test the output instead, you can use pytest's output capturing to access the output of the tested function and check that.

How to correctly add a variable's value to parameters in Postman?

I'm learning Chrome Postman now and my issue now is:
I need to generate a new value of a parameter for each request.
So each request (I make a POST) must have a unique value of this parameter.
So far I thought to manage it with environment variables and I have done it like this:
I add a new environment variable with a unique value
I use this variable in the "value" field on a parameter
And it doesn't work - I get error 401 Authorization Required.
Seems that the error is not connected to the parameter at all but as soon as I change the parameter and manually input a unique data it works well!
So this will work for me:
Please suggest what I'm doing wrong here and advice how to do it right. Thanks!
Spent some more hours investigating I found my problem!
The problem is the value I put into a variable - it included ":" sign and this sign simply changed my URL.

Gatling - Check multiple values with JsonPath

I am using Gatling 2.1.7 and I cant't find a way to check multiple values with JsonPath.
Lets pretend I have this Json:
{
"messageTyp": "wsopen",
"userId": "1"
}
I do need to check both values.
It is easy to check on of the values:
.check(wsAwait.within(2).expect(1).jsonPath("$..messageType").is("wsopen"))
But because this tests are running in a "feeder"-loop, there are a lot "wsopen" messages, coming back asynchronous. I need to check, that every user receives exactly one "wsopen" message.
I need something like
.check(wsAwait.within(2).expect(1).jsonPath("$..messageType").is("wsopen").and.jsonPath("$..userId").is("${userid}")) // won't compile
Anybody a hint?
You can do it with JSONPath directly:
jsonPath("$[?(#.messageTyp=='wsopen' && #.userId=='1')]").exists

Using a csv feeder to check a count i.e. convert to Int

I am using Gatling 2.0.0-SNAPSHOT (from a few months ago) and I have a CSV file:
search,min_results
test,1000
testing,50
...
I would, ideally, like to check that the number of results are equal or greater than the min_results column, something like:
.get(...)
.check(
jsonPath("$..results")
.count
.is(>= "${min_results}".toInt)
The last line doesn't work as it probably isn't valid Scala but also"${min_results}".toInt tries to convert ${min_results} rather than its value to an Int.
I would settle for just fixing the conversion toInt problem but this might result in a slightly less robust script. However, I could then add a max_results column and use the .in(xx to yy).
Thanks
In this case, toInt definitely won't work, as it would be called before the EL has been resolved.
However, this code snippet would achieve what you want :
.get(...)
.check(
jsonPath("$...results")
.count
.greaterThanOrEqual(session => session("min_results").as[String].toInt))
Here, using as[Int] works as expected, since the min_results attribute has already been resolved from the session at this point.