1

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.

  • For one year, Gatling has been providing a Java API. If you feel like Scala is getting in your way, you should probably switch to Java. – Stéphane LANDELLE Oct 11 '22 at 23:14
  • @StéphaneLANDELLE I would most likely have chosen Java had I the option of starting over. Unfortunately there is already Scala code in use and I have to continue down this path. I was hoping you had a solution for approach number 2 where I get to use something similar to .check for easy access to elements? – LiveLongAndProsper Oct 11 '22 at 23:49

1 Answers1

0

Here's what I suggest:

step 1: use a jsonPath or jmesPath check to get the JSON-unescapedContent value.

step 2: use the transform check step to extract the value attribute from the HTML string. In the function you would pass in there, you could indeed use a regex (make sure to create it outside of the function to it's not compiled on each execution) or an HTML parser, such as Lagarto (which is the one Gatling's css checks use underneath)

Stéphane LANDELLE
  • 6,076
  • 2
  • 10
  • 12
  • I have added "Update 1" to the original post since I needed room for code example and syntax highlighting. What would the code have to be to fulfill the goal in "Description" ? – LiveLongAndProsper Oct 13 '22 at 09:41