Wojtek
February 11, 2014, 9:39am
1
Hello,
i need to parse form data and send them in next request. I wrote sth like this:
def checkInputValue(name: String) =
check(regex(“”“name=”.*“”" + name + “”“[^”]+“.+value=”([^“]+)”“”).whatever.saveAs(name))
.doIf(session => !session(name).validate[String]) {
session.set(name, " ")
}
And in main file:
.exec(http(“display form”)
.get(“”“display.html”“”)
.checkInputValue(“var1”)
.checkInputValue(“var2”)
…
.checkInputValue(“varN”))
.exec(http(“send form”)
.post(“”“send.html”)
.param(“var1”, “${var1}”)
.param(“var1”, “${var2}”)
…
param(“var1”, “${varN}”)
How can I do sth like this?
I try to use only whatever function, but where input value is not set (value tag doesn’t exists) I get an error:
No attribute named X’ is defined
I don’t want to put doif in my scenario always after .check.
Thanks for your help,
Wojtek
Hi,
What about using the following structure :
regex(""“name=”.*""" + name + “”"[^"]+".+value="([^"]+)""")
.transform(_.orElse(Some(defaultValue))).saveAs(name)
cheers
Nicolas
Hello
I realize this is an older post but I was wondering if you could help me out a bit with this. I’m using jsonPath where one of the values I want to get might be null. If it is, I want to default to a String 0.00. I tried to use something similar to your example (Some(“0.00”)) but the compiler says it requires a PartialFunction. I realize this is probably a general Scala question but I can’t seem to find much helpful information
Which version do you use?
So this thread was about Gatling 2.
Sadly, you can’t achieve default values with Gatling 1 API.
Ah okay. Well, bummer
So, a follow up question then . Before I found this post I was trying something similar to the following:
.check(
jsonPath("$.totals[0].tax.amount")
.whatever
.saveAs(“taxAmt”)
)
And then further in the execution plan:
exec(session => {
if (!session.isAttributeDefined(“taxAmt”)) {
session.setAttribute(“taxAmt”, “0.00”)
}
session
})
But when I dump the session I don’t see the taxAmt attribute. Is there a way to programmatically add session attributes besides saveAs
Ah, yes, you’re right, you could do that in 2 steps!
You’re problem is that Session is immutable, as explained in the documentation, so setAttribute returns a new instance.
exec(session => {
if (!session.isAttributeDefined(“taxAmt”)) {
session.setAttribute(“taxAmt”, “0.00”)
} else {
session
}
})
D’oh! I forgot that it was immutable. Thanks for the help!
Hi Nicolas,
I tried using your solution and I am getting below error on this :
00:08:33.596 [ERROR] i.g.c.ZincCompiler$ - C:\Users\dball83\Downloads\gatling-charts-highcharts-bundle-3.3.1-bundle (1)\gatling-charts-highcharts-bundle-3.3.1\user-files\simulations\computerdatabase\ShopSite_Scripts\CheckoutService.scala:76:57: type mismatch;
found : Some[String]
required: PartialFunction[?,?]
.check(jsonPath("$.cartId").transform(_.orElse(Some(“NOTFOUND”))).saveAs(“c_CartId”))
Can you please take a look here
First, you want to use transformOption, not transform, see documentation: https://gatling.io/docs/current/http/http_check#transforming .
Then, there’s a limitation in the checks design in Gatling 3 that forces users to have to make transform input type explicit, see https://github.com/gatling/gatling/issues/3926 . This will be fixed in Gatling 3.4.
Thanks @stephane , This is working for me