If else condition inside check in gatling - scala

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")) {
...
}

Related

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.

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.

Protractor - is it possible to do multiple check on an element with a single 'expect' statement?

I wonder if it is possible to chain the checks on an element inside the same 'expect' statement.For example, instead of this:
expect(loginPage.getLoginPageBackground().isPresent()).toBeTruthy();
expect(loginPage.getLoginPageBackground().isDisplayed()).toBeTruthy();
something like this:
expect(loginPage.getLoginPageBackground().isPresent().isDisplayed()).toBeTruthy();
This is just a random example. I know this won't work but I guess you get the concept. If there is any workaround, would like to hear that.
Thanks
Actually exactly for your question you don't need to separately do isPresent and isDisplayed check, since isDisplayed is already does isPresent check inside:
expect(loginPage.getLoginPageBackground().isDisplayed()).toBeTruthy('Optional error message here');
FYI: isDisplayed() throws error in case element does not exist in DOM.
But in case you want to assert other conditions - you can use ExpectedConditions functions - and(), or(), not() to combine various checks together:
const EC = ExpectedConditions
const myElem = $('div')
await browser.wait(
EC.and(
EC.visibilityOf(myElem),
EC.textToBePresentInElement(myElem, 'hello world!'),
),
10000 // timeout
`Optional error message`
)
http://www.protractortest.org/#/api?view=ProtractorExpectedConditions
Answer is NO, you can't do that. Please give the complete context so that I can suggest work a round to achieve what you wanted.

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

In Sinatra, how to make filters dependent of request method?

I've been using filters in Sinatra the way it has been declared in the documentation: with no match string, with a match string or with a match regexp. It has been working fine til now. Now, I have a particular use case. Let's say I have this route:
/resources/1
According to REST, and depending of the request method, this can either be a GET method, PUT method or DELETE method. First question is: How to write filters that are only called when it is a GET request? (currently I'm letting all of them get filtered and only then I test the method. It works, but I don't like it). Second question, and more important: let's say a PUT request like this is triggered:
/resources/
This is of course wrong, because the PUT request has no resource id associated. I would like to know if there is something in Sinatra that enables me to do something like this:
before "/resources/", :method => :put do
error_message
end
just this possibility does not exist (before accepts only one argument). How could I achieve this result at best?
Actually, filters do take conditions. You don't have to use a condition though, you could use a conditional within the filter:
before "/path/" do
if request.request_method == "PUT"
# do something
end
end
If you want to use a condition, I think you'll need to write one, perhaps something like this:
set(:accepted_verbs) {|*verbs|
condition {
verbs.any?{|v| v == request.request_method }
}
}
before "/path/", :accepted_verbs => ["GET","POST"] do
# do something
end
before "/path/", :accepted_verbs => ["PUT"] do
# do something else
end
See conditions for more.