Regular Expressions in Gatling

This article will show you how to use Regular Expressions in Gatling tests. Our sample XML based web service is http://www.dneonline.com . We are going to use Adding operation of the web service which allows us to add two number and return the result. Then we’ll extract the sum value via regular expression and make a validation.

Define HTTP Protocol

That’s where we define our base url and other configuration like headers, etc. For this example, all we need to have is baseUrl. For this example, we need to set Content-Type header as text/xml.

object Protocols {
    val httpProtocol = http
      .baseUrl("http://www.dneonline.com/")
      .contentTypeHeader("text/xml; charset=utf-8").build
}

Make POST Request to Endpoint

This step is very straight forward. All you need to do is to make a basic POST request. You might want to define the payload in a different variable. For the example, we put it as hard coded.

 def xmlPath = exec(http("Addition Task")
      .post("/calculator.asmx?op=Add")
      .body(StringBody("""<?xml version="1.0" encoding="utf-8"?>
                             <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                               <soap:Body>
                                 <Add xmlns="http://tempuri.org/">
                                   <intA>100</intA>
                                   <intB>200</intB>
                                 </Add>
                               </soap:Body>
                             </soap:Envelope>"""))

Extract Result from Response

You need to use check function to be able to extract data. Then you can use regex function with a valid regular expression. After getting the value(s), we use is function to validate the sum extracted from the response.  Sum is once hard coded like the payload.

 def xmlPath = exec(http("Addition Task")
      .post("/calculator.asmx?op=Add")
      .body(StringBody("""<?xml version="1.0" encoding="utf-8"?>
                             <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                               <soap:Body>
                                 <Add xmlns="http://tempuri.org/">
                                   <intA>100</intA>
                                   <intB>200</intB>
                                 </Add>
                               </soap:Body>
                             </soap:Envelope>"""))
      .check( regex( "(?<=<AddResult>)(.*\\n?)(?=</AddResult>)" ).is("300") ))

Setup Test Configuration

We define our scenario by assigning the test case. Then we configure the virtual user number and ramp up times.

val xml_scenario = scenario("Execute Sample API Test").exec(Task.xmlPath)
setUp(xml_scenario.inject(rampUsers(20) during (10 seconds))).protocols(Protocols.httpProtocol)

Now you can run your Gatling test like a charm.

The whole source code is below:

package example

import scala.concurrent.duration._
import java.util.concurrent.ThreadLocalRandom
import io.gatling.core.Predef._
import io.gatling.http.Predef._

class DemoScenario extends Simulation {

  object Protocols {
    val httpProtocol = http
      .baseUrl("http://www.dneonline.com")
      .contentTypeHeader("text/xml; charset=utf-8").build
  }

  object Task {
    def xmlPath = exec(http("Addition Task")
      .post("/calculator.asmx?op=Add")
      .body(StringBody("""<?xml version="1.0" encoding="utf-8"?>
                             <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                               <soap:Body>
                                 <Add xmlns="http://tempuri.org/">
                                   <intA>100</intA>
                                   <intB>200</intB>
                                 </Add>
                               </soap:Body>
                             </soap:Envelope>"""))
      .check( regex( "(?<=<AddResult>)(.*\\n?)(?=</AddResult>)" ).is("300") ))

  val scenario_xml = scenario("Execute Sample API Test").exec(Task.xmlPath)
  setUp(scenario_xml.inject(rampUsers(10) during (10 seconds))).protocols(Protocols.httpProtocol)
}

Canberk.

1 thought on “Regular Expressions in Gatling”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.