Answer
Validating Query Parameters in API Testing
Query parameters are key-value pairs appended to a URL after ?, separated by & for multiple parameters.
Example URL:
https://api.example.com/products?category=electronics&price=500
Here category and price are query parameters.
Validation Checklist
1. Presence Check
Ensure mandatory parameters are present in the request.
// Test missing required query param
given()
.when()
.get("/products") // missing 'category'
.then()
.statusCode(400)
.body("error", equalTo("category is required"));
2. Type Check
Ensure the parameter''s type matches expectations (e.g., price must be a number).
given()
.queryParam("price", "not-a-number")
.when()
.get("/products")
.then()
.statusCode(400);
3. Range or Format Check
Check if value fits within an acceptable range or format.
given()
.queryParam("price", -100) // negative price
.when()
.get("/products")
.then()
.statusCode(400)
.body("error", equalTo("Price must be a positive number"));
4. Special Characters and Edge Cases
Test with empty values, spaces, and special characters.
given()
.queryParam("category", "")
.when()
.get("/products")
.then()
.statusCode(400);
Important Note
If any parameter fails validation, the API should respond with a clear error message. For example, if price is negative: "Price must be a positive number."
