Updates based on answers and comments that include highlighted code
Update 1:
Description: The idea is to take body (string) and send it to the function GetValueOfInputAsString with myElementName (string) which returns the value as string and then save it in the session variable mySessionName so that it can be used in the next request as "${mySessionName}".
I expected this code to work based on answers found here on StackOverflow (like this answer https://stackoverflow.com/a/50827447 ).
exec(
http(myName)
.get(myUrl)
.check(
bodyString.transform(
(body: String, session: Session) => {
session.set("mySessionName", GetValueOfInputAsString(body, myElementName) );
}
)
)
)
However this result in the following error:
type mismatch;
found : (String, io.gatling.core.Predef.Session) => io.gatling.core.session.Session
(which expands to) (String, io.gatling.core.session.Session) => io.gatling.core.session.Session
required: String => ?
(body: String, session: Session) => {
^
What would the code have to be to fulfill the goal in "Description" ?
Original post
I make a request (get or post) and get JSON back containing a HTML form. I need to get the value of various INPUT and SELECT elements within the string.
Example of the response:
{ "Content": "<form><input id=\"MyFieldName\" name=\"MyFieldName\" type=\"hidden\" value=\"hello\" /><input id=\"AnotherField\" name=\"AnotherField\" type=\"hidden\" value=\"world\" /><select id=\"MyDropDown\" name=\"MyDropDown\"><option value=\"123\">First option</option><option selected value=\"456\">Second option</option></select></form>" }
My goal is to create a function that takes two arguments (html-string, element-name) and return the value (from the value-attribute).
It seems like I am the only one to think of doing this since most Scala-Gatling related issues using RegEx is inline in ".check".
I am very new to Scala and Gatling but have programmed in many other languages (Java, C#, JavaScript, VBScript, PHP).
Approach 1 - Using RegEx
Despite being new to this I have managed to create the following (hacky) solution:
def getValueOfInput(data: String, name: String) : String = {
// Variable that contains the return value
var value = "";
// This RegEx gets the specific INPUT-element
var patternLine : Regex = ("(?:name=\"" + name + "\")(?: type=\"hidden\"){0,} value=\"(.*?)\"").r;
// This RegEx gets the VALUE-attribute with attribute name and double quotes
var patternValue : Regex = ("(?:value=\")(.*?)(?:\")").r;
// Get the INPUT-element
var line = patternLine.findFirstIn(data).getOrElse("no match line")
// Get the VALUE-attribute
value = patternValue.findFirstIn(line).getOrElse("no match value")
// Remove the text "value=" and double quotes
value = value.replaceAll("value=|\"", "");
// Return the actual value
return value;
}
If I call getValueOfInput(myJsonString, "MyFieldName")
I get the value "hello" which is the correct value.
There might be a way to get the value in first RegEx but I do not know how.
There are many ways this can fail (0 matches etc) and it currently does not support SELECT-elements which have the value in an option with the "selected"-attribute.
Approach 2 - Converting string to a object
I also tried to find a way to take the string and convert it into an object that can be parsed/traversed like HTML DOM elements using something like ".check(css(...))" which is used when making a request.
Unfortunately I was unable to find a way to convert the string and do not if Scala/Gatling supports this.
.
I hope someone out there knows how to implement this function, or two functions (one for INPUT and one for SELECT), using either RegEx or string-to-object conversion.