JMeter + Groovy Advanced API Automation Framework
1. Objective
Automate REST APIs (POST, GET, PUT, PATCH, DELETE) using Apache JMeter with Groovy
scripting in a scalable and maintainable way.
2. Test Data (CSV)
userId,name,email,age,eligibility
1,John,john@[Link],25,eligible
2,Alice,alice@[Link],17,not_eligible
3,Bob,bob@[Link],35,eligible
3. POST - Create User
{
"id":"${userId}",
"name":"${name}",
"email":"${email}",
"age":"${age}"
}
Groovy Assertion
import [Link]
def res = new JsonSlurper().parseText([Link]())
assert [Link]() == "201"
assert [Link] == [Link]("userId")
4. GET - Fetch User
GET /users/${userId}
Groovy Assertion
import [Link]
def res = new JsonSlurper().parseText([Link]())
assert [Link] == [Link]("name")
5. PUT - Replace User
{
"name":"Updated_${name}",
"email":"updated_${email}",
"age":30
}
6. PATCH - Update User
{
"email":"patched_${email}"
}
7. DELETE - Remove User
DELETE /users/${userId}
8. Eligibility Validation
if ([Link]("eligibility") == "not_eligible") {
[Link]("expectedStatus", "400")
} else {
[Link]("expectedStatus", "200")
}
assert [Link]() == [Link]("expectedStatus")
9. Correlation Example
def json = new [Link]().parseText([Link]())
[Link]("userId", [Link])
10. Best Practices
- Use JSR223 with Groovy - Avoid hardcoded values - Use CSV for data-driven testing - Implement
correlation - Add response time assertions - Integrate with CI/CD tools like Jenkins
11. Execution Command
jmeter -n -t [Link] -l [Link] -e -o report