How to Use RegEx Extractor in Gatling Projects
Learn more about using the RegEx Extractor to extract data in performance test projects.
Join the DZone community and get the full member experience.
Join For FreeRegular expressions can be very powerful with string manipulation. They are fast and flexible but a little bit hard to learn. To be able to extract data in performance test projects, the regular expression is a good fit. Gatling supports regular expression usage for data extraction. Let’s see how we can integrate this approach into our Gatling projects.
How to Extract RegEx in Gatling Script?
For this example, we are going to use an XML web service hosted on www.dneonline.com
There is a calculator web service where you can do addition, subtraction, multiplication, and division operations.
Execute POST Command
First, we need to post body data to our web service. We create an exec object that will make a post operation with a body. Body type can be changed according to your web services nature. We are using StringBody
, as we are going to post a string. The XML payload is hardcoded for this example. But you can always create a variable to store the payload.
def get_task = 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>"""))
This scenario will work like a charm. But that’s not enough. We need to validate the response.
We can do different response validations, including status and content validation. This time, we will validate the content.
Extract the Data
Secondly, you need to add the check
method. This method is where you will make all the validations. Then, we add gatling regex
method inside of it with a valid regular expression.
def get_task = 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>)" )))
Validate the Data
Whenever you run tests, you will see that the test has passed. What is missing here is the validation. We use the is
method to make the validation against the extracted value. As our test data is hardcoded, we can also hardcode the result.
def get_task = 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") ))
So, let's review what we did step by step:
- Created a POST command that makes a request
- Parseed the response with a regular expression
- And made validation
Happy load testing!
Published at DZone with permission of Canberk Akduygu. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments