Gatling - Check multiple values with JsonPath - scala

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

Related

What is the correct way to express "select all when nothing is specified in parameter"?

Let's say we have an HTTP endpoint to get all elements by name?
GET /elements?name={name}
{name} can have a value of CSV or be absent
valid:
GET /elements?name=Bill,Mary,Ann
GET /elements?name=Mike
GET /elements
invalid:
GET /elements?name=
Somehow we find out in controller that name is not passed. We know that the contract implies to return all values for elements. Possible decisions on further actions (I've seen in different projects) are:
using a NULL or a "dummy" substitution like a secret char sequence "!#$#%#$" and juggling them in database while building a query
using if (present) { executeQueryA } else { executeQueryB } logic
I am not sure I like either of these approaches, because when there is more than one optional filter these designs become unmaintainable. Something makes me believe that there is a better way to handle the situation.
What would be a proper design on back-end and in database query to handle the case "select all" when nothing is given? Just a general idea and some pseudo-code will be much appreciated.

Protractor - check if element is present and either stop test or continue

I have a Protractor test that pulls various values from the UI and stores them as variables for comparison with values from a different database.
Now this test needs to run against multiple sites BUT of the 25 maximum data points recorded, some sites only have 22.
Clearly the test fails on those "22" sites since the elements are not present.
What I want to achieve is where there's a "22" site, the tests against the not present elements are ignored and the test proceeds to the end. Conveniently, the "missing" elements are the last ones in the spec.
Crudely speaking...
if element-y is not present end test or if element-y is present continue
Grateful if anyone could advise.
Thanks #sergey. I've modified your example as below....
if (!(await element(by.xpath('//*[#id="root"]/div/div[2]/main/div/div/section[5]/div/div/div[1]/section/div/span')).isPresent())) {
console.warn ('Functions are not present, closing the session')
await browser.close()
I get this error:
if (!(await element(by.xpath('//*[#id="root"]/div/div[2]/main/div/div/section[5]/div/div/div[1]/section/div/span')).isPresent())) {
^^^^^^^
SyntaxError: Unexpected identifier
I've tried using a 'var' instead of the actual element, but get the same result.
Thanks
well the best option that I recall is still pretty dirty... you can do something like this
if (!(await element.isPresent())) {
console.warn('Element not present, closing the session')
await browser.close()
}
And then the rest of test cases will fail as session not found or similar error
The reason you can't do anything better because in protractor you can't do conditional test cases based on a Promise-like condition, if that makes sense...

Gatling for loop in checkif conditional

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.

Filter every n:th event

Is there any way of creating a filter that filters every n:th event where n is different for different checks. I.e I would like to specify a field in each check such that I can control the filter frequency for different checks.
I have some checks that run once a day, some checks that runs once an hour and some that runs every minute. Using the same filter where I filter every n:th occurrence would not work for the different checks.
Is there any way of avoiding creating 10 different filters with different frequency?
Edit: I also have to create 10 different handlers, each that uses a different filter. Not a very clean solution and very much duplicated code.
The fine folks at Sensu have implemented what I asked for. It uses replacement tokens in the filter part as well now.
{
"filters": {
"occurrences": {
"negate": true,
"attributes": {
"occurrences": "eval: value > :::check.occurrences|60:::"
}
}
}
}
Have you tried with Mutators ? I really haven't tried them, but according to the description they could help you to manipulate the output of the check before they reach to the handler.
In theory you could manipulate the output to "normalize".
I am afraid is not possible to achieve what you tried with only filters.

If else condition inside check in gatling

I need to implement an if-else condition in my gatling check.
For example:- Below, I might get items array in JSON response as empty. So I need to check if items is empty. I need to execute another set of action. Can you please let me know how can I achieve this?
.check(
jsonPath("$..items[(#.length-1)].id").saveAs("MyAlbumId")
)
As explained on the Gatling ML:
When using a check, the default verify step is exists, so you have to use whatever instead if having your check find nothing is acceptable:
https://github.com/excilys/gatling/wiki/Checks#wiki-verifying
Then, you can use a doIf in order to perform a chain of actions on certain conditions:
https://github.com/excilys/gatling/wiki/Structure-Elements#wiki-do-if
For example:
doIf(session => !session.isAttributeDefined("MyAlbumId")) {
...
}