Api MonitoringIntermediate

E-commerce API Testing using Playwright

Learn how to robustly test e-commerce APIs including cart operations, checkout flows, and payment processing using Playwright.

ObserveOne Team
3 min read

E-commerce platforms rely heavily on complex API interactions to handle inventory, shopping carts, and secure payments. Ensuring these APIs work flawlessly is critical for revenue. In this guide, we'll explore how to use Playwright for high-performance API testing specifically tailored for e-commerce workflows.

Why Playwright for E-commerce APIs?#

While Playwright is famous for browser automation, its powerful API testing capabilities make it a perfect hybrid tool for e-commerce:

  1. Unified Context: Share authentication state between UI and API tests.
  2. Speed: Execute API calls significantly faster than UI interactions.
  3. Reliability: Validate backend logic independent of frontend flakiness.

Key E-commerce Test Scenarios#

Validate that product search returns correct stock levels and pricing.

test("Search API returns relevant products with stock", async ({ request }) => {
const response = await request.get("/api/v1/products/search", {
params: { q: "headphones", in_stock: true },
});
expect(response.ok()).toBeTruthy();
const data = await response.json();
expect(data.items.length).toBeGreaterThan(0);
data.items.forEach((item) => {
expect(item.inventory_count).toBeGreaterThan(0);
expect(item.price).toBeDefined();
});
});

2. Cart Management Operations#

Test the core "Add to Cart" flow without needing to click through the UI.

test("Add item to cart and verify total", async ({ request }) => {
// Create a new cart session
const cartRes = await request.post("/api/v1/cart");
const cartId = (await cartRes.json()).id;
// Add item
const addRes = await request.post(`/api/v1/cart/${cartId}/items`, {
data: { productId: "prod_123", quantity: 2 },
});
expect(addRes.ok()).toBeTruthy();
// Verify cart total calculation
const cartState = await addRes.json();
expect(cartState.total_items).toBe(2);
expect(cartState.subtotal).toBe(cartState.items[0].price * 2);
});

3. Secure Checkout Flow#

Simulate the checkout process, including address validation and mock payment processing.

test("Complete checkout flow with mock payment", async ({ request }) => {
// Setup: Create cart with items (helper function)
const { cartId } = await setupCartWithItems(request);
// Submit Shipping Info
await request.post(`/api/v1/checkout/${cartId}/shipping`, {
data: {
address: "123 Test St",
city: "Testvill",
country: "US",
},
});
// Process Payment (Mocked)
const paymentRes = await request.post(`/api/v1/checkout/${cartId}/pay`, {
data: {
token: "tok_visa", // Stripe mock token
amount: 4999,
},
});
expect(paymentRes.status()).toBe(200);
const order = await paymentRes.json();
expect(order.status).toBe("paid");
expect(order.id).toBeDefined();
});

Best Practices for E-commerce Testing#

  • Data Isolation: Create unique test user accounts and carts for every test run to avoid state leakage.
  • Mock external payments: Never use real credit cards in automated tests. Use provider-specific test tokens (e.g., Stripe tok_visa).
  • Rate Limiting: Be aware of API rate limits. Mock responses for heavily hit endpoints like "Recommended Products".

Conclusion#

By leveraging Playwright for API testing, you can ensure your e-commerce platform's backend is robust, scalable, and ready for high traffic events like Black Friday—all without the overhead of browser rendering.

Related comparisons

Weighing the tools you just read about? Here's how they stack up side by side.

See alternatives to your stack

Looking to swap one of these out? Here's where to start.

Ready for AI-Powered Testing?

ObserveOne monitors your selectors 24/7 and automatically heals them when websites change. Never deal with broken tests again.

Start Free Trial