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

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.

Related

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.

No output for map function in playground

I have these very simple lines of code:
var friends = ["Mike", "Marika", "Andreas", "Peter", "Sabine"]
friends.map{
println("Hallo \($0)!")
}
This works fine in a program but I get no output in a playground.
It only tells me the count of the elements and how many times to function needs to run. But it does not write the strings.
Is it me or is this a bug in Xcode?
It's not a bug in Xcode. While your map code will print to the standard out (hit cmd-opt-enter to reveal the output in the assistant editor on the right), stylistically you should avoid using map for this. You would be better off with a for...in loop:
for friend in friends {
println("Hallo \(friend)")
}
If you quick-look the results this time, you'll see a more helpful result:
(note, I've switched the quick look to the list view, which shows every result, rather than just the last one)
Why is this working differently? It's because map isn't really for running arbitrary code against your array. It's more specifically for transforming (i.e. mapping) your array into another array. So suppose instead of printing a list of friends, you wanted a list of greetings, you could do this:
let greetings = friends.map { friend in
"Hallo \(friend)"
}
greetings will now be set to a new array of 5 strings, one for each name, of the form "Hallo <name>". map is taking a closure that takes a string, and maps it to a new string.
So what is happening when you write friends.map { println("...") } is that map is calling the closure, and getting the result of the expression in the closure, and populating a new array with it. Since println returns Void, the result of that expression each time is Void. And it is that Void that Xcode is displaying (as "(0 elements)" which is how the UI displays Void).
Instead, with the for-loop, Xcode knows that a stand-alone println who's value isn't being used should be interpreted not as a result, but as what got output to the standard out, so that's what it does.

What is [ ] in google chrome developer console?

While testing code in the google chrome developer console, i get
[]
and sometimes i get "".
The later shows up,when i think there not such strings available with the current selector combinations.But i still couldn't figure out the meaning of the former [] square brakets.
Please help.
With the information that you've given, all we can do is make assumptions. However, when you're logging things to the console, [] is an empty array, whereas "" is an empty string.
[] are returned whenever you jquery returns empty object. i.e. you selector expression cant locate what you are trying to search.
whereas "" is just simple string
i looked into your given site..
when i tried :
$('.four columns alpha') i get object[] (which means there jquery is returning empty object)
but when you write correct expression like :
$('.four.columns') you will get array of Div's which can be used like object.
Hope i'm able to make you understand. if any doubts do write.
And $('.four columns alpha') this is not the right way to select div's with more than one css class right way is to do something like below:
$('.four.columns.alpha')

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.