问题
I want to write a function to format Vaadin messages. These messages have the format
108|0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]
The first number is the length then there some kind of session id (later called vaadin_security_key) followed by the payload. (In this example I set the value "Agent00232" to the textfield with the connector id "0_11_12_13_login_username") I wrote a function like this:
def sendMessage(name: String, vaadinCommand: String) = {
def createMessage(session: Session) = {
val message = session("vaadin_security_key").as[String] + "\u001d" + vaadinCommand
val message2 = ELCompiler.compile(message)(classTag[String])(session).toString
val message3 = message2.substring(8, message2.length - 2)
val len = message3.length
val completeMessage = len.toString() + "|" + message3
completeMessage
}
exec(
ws(name)
.sendText(createMessage)
.check(wsAwait
.within(Vaadin.defaultTimeout)
.until(1)
.regex("""(.+)""")
.saveAs("lastResult")))
.exec { session =>
println(session("lastResult").as[String])
session
}
}
I'd like to use this method like this and use EL expressions in the string:
exec(Vaadin.sendMessage("setting Name", "[[\"0_11_12_13_login_username\",\"v\",\"v\",[\"text\",[\"s\",\"${username}\"]]]]"))
Therefore I have to evaluate the String before calculating the length. The method ELCompiler.compile()... returns a Expression[String]. The problem is that I need this string and concat it with the calculated length. When I do message2.toString it returns
Success(0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]])
and therefor I have to use substring(8, message2.length - 2) to get the evaluated payload (with the security key) to calculate the length of it.
Is there a better (more elegant) way to extract the String out of the expression then the use of substring(...)
?
回答1:
- Don't explicitly pass a classTag to ELCompiler.compile. If you really were to use this yourself, use ELCompiler.compile[String].
- ELCompiler.compile returns an Expression[String], alias a function of Session to Validation[String], so if you apply it, you get a Validation[String] (because the evaluation could fail), hence toString prints the Success, not the value it contains. What you want is to map this Validation container.
- You're compiling the expression over and over again, on each function execution. Move it outside the function.
- Beware that session("lastResult").as[String] would crash if lastResult could not be saved. Use validate instead.
- Use triple quotes so you don't have to escape the inner double quotes.
The first part of your code would then look like:
import io.gatling.core.session._
import io.gatling.core.session.el._
def sendMessage(name: String, vaadinCommand: String) = {
val message = ("${vaadin_security_key}\u001d" + vaadinCommand).el[String]
def createMessage = message.map { resolvedMessage =>
resolvedMessage.length + "|" + resolvedMessage
}
exec(
ws(name)
.sendText(createMessage)
.check(wsAwait
.within(Vaadin.defaultTimeout)
.until(1)
.regex("""(.+)""")
.saveAs("lastResult")))
.exec { session =>
println(session("lastResult").validate[String])
session
}
}
Then you'd call it with:
exec(Vaadin.sendMessage("setting Name", """[["0_11_12_13_login_username","v","v",["text",["s","${username}"]]]]"""))
来源:https://stackoverflow.com/questions/26344554/getting-the-string-out-of-a-gatling-expression