|
| 1 | +--- |
| 2 | +description: "How to use Playwright MCP browser tools with Claude Code to observe real UI before writing QIT test packages. Covers the observe-first workflow: start a QIT environment, navigate the running site, capture exact selectors from browser snapshots, then write tests from observed reality instead of guesswork." |
| 3 | +--- |
| 4 | + |
| 5 | +# How to Write Tests with AI Browser Observation |
| 6 | + |
| 7 | +When using Claude Code to write QIT test packages, the **Playwright MCP** tools let Claude observe the real running site before writing any test code. This eliminates guessed selectors, incorrect assumptions about UI layout, and tests that fail because the page doesn't work the way the AI assumed. |
| 8 | + |
| 9 | +## Why This Matters |
| 10 | + |
| 11 | +Without browser observation, AI assistants guess at selectors and page structure based on source code. This leads to: |
| 12 | +- Wrong selectors (`#place_order` when the real button is `[name="checkout"]`) |
| 13 | +- Missing wait conditions (AJAX-loaded content that isn't there on page load) |
| 14 | +- Incorrect flow assumptions (collapsed checkout sections that need expanding first) |
| 15 | + |
| 16 | +With Playwright MCP, Claude sees exactly what a user sees and writes tests against the real page. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- [Claude Code](https://code.claude.com) with the QIT plugin installed |
| 21 | +- Playwright MCP available in Claude Code (included by default) |
| 22 | +- Docker (for running QIT environments) |
| 23 | + |
| 24 | +## The Observe-First Workflow |
| 25 | + |
| 26 | +### 1. Start a QIT environment |
| 27 | + |
| 28 | +```bash |
| 29 | +qit env:up --plugin=your-extension |
| 30 | +``` |
| 31 | + |
| 32 | +This gives you a running WordPress + WooCommerce site with your extension installed. |
| 33 | + |
| 34 | +### 2. Ask Claude to explore the UI |
| 35 | + |
| 36 | +Once the environment is running, ask Claude to navigate the site: |
| 37 | + |
| 38 | +> "Navigate to the admin settings page for my extension and show me what's there." |
| 39 | +
|
| 40 | +Claude will use Playwright MCP tools to: |
| 41 | +- `browser_navigate` to the site URL |
| 42 | +- `browser_snapshot` to capture the page structure (accessible names, roles, form fields) |
| 43 | +- Report back what it sees |
| 44 | + |
| 45 | +### 3. Explore the customer-facing experience |
| 46 | + |
| 47 | +> "Now go to the frontend. Add a product to the cart and go to checkout. Show me what shipping options appear." |
| 48 | +
|
| 49 | +Claude navigates the storefront, interacts with forms, and captures the exact state of the page at each step. |
| 50 | + |
| 51 | +### 4. Write tests from observed selectors |
| 52 | + |
| 53 | +After exploring, Claude knows: |
| 54 | +- The exact accessible names for buttons, fields, and links |
| 55 | +- How the page behaves (loading states, AJAX updates, collapsed sections) |
| 56 | +- What "working correctly" looks like |
| 57 | + |
| 58 | +It writes Playwright tests using selectors it actually saw, not selectors it guessed. |
| 59 | + |
| 60 | +### 5. Debug failures interactively |
| 61 | + |
| 62 | +When a test fails, instead of guessing what went wrong: |
| 63 | + |
| 64 | +> "Navigate to the checkout page and show me what's there right now. My test expected a shipping rate but failed." |
| 65 | +
|
| 66 | +Claude can see the current page state and compare it to what the test expected. |
| 67 | + |
| 68 | +## What Claude Observes |
| 69 | + |
| 70 | +The `browser_snapshot` tool returns an **accessibility tree** — a structured representation of the page that includes: |
| 71 | + |
| 72 | +- Element roles (button, link, textbox, heading) |
| 73 | +- Accessible names (the text a screen reader would announce) |
| 74 | +- Form field values and states (checked, disabled, expanded) |
| 75 | +- Page structure and hierarchy |
| 76 | + |
| 77 | +This is more reliable than CSS selectors because accessible names are stable across themes and minor UI changes. |
| 78 | + |
| 79 | +## Example Interaction |
| 80 | + |
| 81 | +``` |
| 82 | +You: I need E2E tests for my shipping extension. Start an environment and explore what it does. |
| 83 | +
|
| 84 | +Claude: [starts env:up, navigates to admin settings] |
| 85 | +I can see your extension adds a "Shipping Zones" tab with these fields: |
| 86 | +- "Enable/Disable" checkbox (currently checked) |
| 87 | +- "Method Title" text field (value: "Flat Rate Shipping") |
| 88 | +- "Cost" number field (value: "10.00") |
| 89 | +- "Tax Status" dropdown (options: Taxable, None) |
| 90 | +
|
| 91 | +Let me check the frontend... |
| 92 | +[navigates to checkout] |
| 93 | +At checkout with a product in cart, I see: |
| 94 | +- Shipping method "Flat Rate Shipping" at $10.00 is selected |
| 95 | +- No other shipping options available |
| 96 | +
|
| 97 | +Based on what I observe, here are the high-value tests: |
| 98 | +1. Verify shipping rate appears at checkout with correct amount |
| 99 | +2. Verify changing the cost in admin reflects at checkout |
| 100 | +3. Verify disabling the method hides it from checkout |
| 101 | +... |
| 102 | +``` |
| 103 | + |
| 104 | +## Tips |
| 105 | + |
| 106 | +- **Always explore before writing.** If Claude suggests writing tests without first navigating the site, ask it to observe the real UI first. |
| 107 | +- **Check both admin and frontend.** Most extensions have configuration in wp-admin and output on the storefront. Tests should cover both sides. |
| 108 | +- **Re-observe after environment changes.** If you reset the database or change settings, have Claude take a fresh snapshot. |
| 109 | +- **Use snapshots for debugging.** When a test fails, ask Claude to navigate to the failing page and snapshot it — the accessibility tree often reveals why a selector didn't match. |
0 commit comments