Playwright Testing for Kenyan M-Pesa STK Push and Daraja API
Complete guide to playwright testing for kenyan m-pesa stk push and daraja api with production-ready Playwright patterns and real-world examples.
playwright-v1-49-matrix
Playwright Testing for Kenyan M-Pesa STK Push and Daraja API
Region Focus: This guide targets Kenya-based development contexts.
This guide provides complete, production-ready Playwright patterns for playwright testing for kenyan m-pesa stk push and daraja api.
Introduction
Robust test automation requires domain-specific patterns. This article covers playwright testing for kenyan m-pesa stk push and daraja api with practical code examples you can use immediately in your test suite.
Architecture Overview
graph TD
Test["Playwright Test"] --> App["Application Under Test"]
App --> API["Backend API / Mock"]
API --> Assert["Test Assertions Pass"]Implementation
test('M-Pesa STK push is initiated', async ({ page }) => {
await page.route('**/api/mpesa/stkpush', route =>
route.fulfill({ json: { CheckoutRequestID: 'ws_CO_123', ResponseCode: '0', CustomerMessage: 'Success. Request accepted.' } })
);
await page.goto('/pay/mpesa');
await page.getByLabel('M-Pesa Phone').fill('254712345678');
await page.getByRole('button', { name: 'Pay with M-Pesa' }).click();
await expect(page.getByText('Check your phone for M-Pesa prompt')).toBeVisible();
});Run Tests
npx playwright test --grep "Playwright Testing for"
npx playwright test --uiReference Table
| M-Pesa Feature | API | Flow | Test |
|---|---|---|---|
| STK Push | Daraja 2.0 | Phone prompt | Mock initiate |
| B2C | Daraja 2.0 | Payout | Mock confirm |
| C2B | Daraja 2.0 | Till/Paybill | IPN mock |
Best Practices
getByRole, getByLabel) for resilient, maintainable tests.Common Mistakes
page.waitForTimeout(). Use auto-waiting assertions instead.| Anti-Pattern | Better Approach |
| Hardcoded sleep | await expect(locator).toBeVisible() |
| Testing 3rd party live APIs | Mock with page.route() |
| Shared state between tests | Isolated fixture setup/teardown |
Frequently Asked Questions
How to test M-Pesa payment confirmation polling?
Mock the query status endpoint to return InProgress then Completed to test polling logic.
How to test Safaricom Daraja OAuth token?
Mock the token endpoint and verify your app caches and reuses tokens correctly.
Can Playwright test Kenyan Shilling (KES) formatting?
Verify amounts display as KES 1,234.56 or KSh symbol in Kenyan locale.
How to test M-Pesa timeout handling?
Mock the STK push to time out after 30 seconds and verify your UI shows a retry option.
How to test B2C payouts with Daraja?
Mock the B2C API and verify disbursement status updates in your admin dashboard.
Summary
This guide covered playwright testing for kenyan m-pesa stk push and daraja api. Apply these patterns to build reliable, fast, and maintainable automation for this specific scenario.
Related Articles
About The Author
PlaywrightPad Editorial reports on Chromium engines, E2E test optimizations, and AI integration specifications.
Newsletter
Get weekly browser reports sent directly to your inbox.