API mocking is one of the most powerful features in QAPlay, enabling you to test your application's behavior under various scenarios without needing a live backend. Whether you're testing error handling, simulating slow networks, or developing features before the API is ready, QAPlay makes it effortless.
Why API Mocking Matters
Traditional API testing often requires:
- A fully functional backend environment
- Complex setup to trigger specific scenarios
- Waiting for backend teams to implement endpoints
- Manual manipulation of databases to create test conditions
With API mocking, you can control the entire testing environment, making your tests faster, more reliable, and completely independent of backend availability.
Creating Your First Mock in QAPlay
Step 1: Capture Network Traffic
First, launch QAPlay and start a browser session:
- Select your preferred browser (Chromium, Firefox, or WebKit)
- Enter your application URL
- Click "Launch & Record"
As you interact with your application, QAPlay automatically captures all HTTP requests in the Network tab.
Step 2: Select a Request to Mock
Browse through the captured requests and find the API call you want to mock. Look for:
- API endpoints returning user data
- Authentication requests
- Data fetching operations
- Any request you want to customize
Click on the request to expand its details and review the original response.
Step 3: Create the Mock
Click the "Create Mock" button. QAPlay automatically populates:
- URL Pattern: The exact URL of the request
- Method: HTTP method (GET, POST, etc.)
- Status Code: Original status (usually 200)
- Response Body: The actual response data
- Headers: Response headers from the original request
Customizing Mock Responses
Now comes the interesting part—customizing your mock to test specific scenarios.
Testing Success Scenarios
URL: https://api.example.com/users/123
Status: 200
Response:
{
"id": 123,
"name": "Test User",
"email": "test@example.com",
"premium": true
}
Testing Error Scenarios
Change the status code to 404, 500, or any error code and customize the response body:
Status: 404
Response:
{
"error": "User not found",
"code": "USER_NOT_FOUND"
}
Simulating Network Delays
Add a delay (in milliseconds) to simulate slow networks or timeout scenarios. This is perfect for testing loading states and timeout handling.
Advanced Mock Patterns
URL Pattern Matching
Instead of exact URLs, you can use patterns to match multiple endpoints:
- Exact match: https://api.example.com/users/123
- Pattern match: https://api.example.com/users/* (matches any user ID)
- Query parameters: Include or exclude specific parameters
Method-Specific Mocks
Create different mocks for the same URL but different HTTP methods:
- GET /api/users: Return user list
- POST /api/users: Return created user with 201 status
- DELETE /api/users: Return 204 no content
Managing Your Mocks
Toggle Mocks On/Off
Every mock has a toggle switch. This allows you to:
- Temporarily disable a mock without deleting it
- Compare behavior with and without the mock
- Keep a library of mocks for different test scenarios
Import/Export Mocks
Share mocks with your team:
- Click "Export Mocks" to save them as JSON
- Share the file with team members
- They can import using "Import Mocks"
This ensures everyone has access to the same test data and can reproduce issues consistently.
Real-World Use Cases
1. Feature Development
Start building UI features before the backend APIs are ready. Create mocks with expected response structures and develop against them.
2. Error Handling Testing
Test how your app handles various error scenarios without complex backend setup:
- Network timeouts (use delays)
- 400 Bad Request responses
- 401 Unauthorized (test login flows)
- 500 Internal Server Errors
- Rate limiting (429 Too Many Requests)
3. Edge Case Testing
Create mocks for rare scenarios that are hard to reproduce:
- Empty result sets
- Maximum pagination limits
- Special characters in data
- Extremely long strings
4. Performance Testing
Simulate slow APIs to test your application's performance under poor network conditions.
Best Practices
- Organize mocks by feature: Use descriptive names like "user-profile-success" or "payment-failure-insufficient-funds"
- Version your mocks: Export regularly and commit to version control
- Document mock purposes: Add comments in the response body explaining what the mock tests
- Keep mocks realistic: Use real-world data structures and response times
- Test both paths: Create mocks for success AND failure scenarios
Common Pitfalls to Avoid
- Forgetting to disable mocks: Always check if mocks are active when debugging unexpected behavior
- Overl complicated patterns: Start with exact matches, add patterns only when needed
- Not testing without mocks: Periodically test against real APIs to ensure your mocks are still accurate
Next Steps
Now that you understand API mocking in QAPlay, try creating mocks for your own application. Start simple with a single GET request, then gradually expand to more complex scenarios.
In our next post, we'll explore how to combine API mocking with session recording to create comprehensive end-to-end tests that are both reliable and easy to maintain.