DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. How to Use RegEx Extractor in Gatling Projects

How to Use RegEx Extractor in Gatling Projects

Learn more about using the RegEx Extractor to extract data in performance test projects.

Canberk Akduygu user avatar by
Canberk Akduygu
·
Mar. 13, 19 · Tutorial
Like (1)
Save
Tweet
Share
8.07K Views

Join the DZone community and get the full member experience.

Join For Free

Regular 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:

  1. Created a POST command that makes a request
  2. Parseed the response with a regular expression
  3. And made validation

Happy load testing! 

Gatling (software) Web Service Test data

Published at DZone with permission of Canberk Akduygu. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • 5 Factors When Selecting a Database
  • How to Secure Your CI/CD Pipeline
  • Bye-Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: