QA AND TEST AUTOMATION · PART 11 · AI TOOLS FOR TESTERS · MCP · 101 OF 112

Playwright MCP: Browser Automation With AI Agents

Playwright MCP lets an AI agent drive a real browser through tools, reading the page as an accessibility snapshot with refs. A real session on ShopRung with Playwright 1.63: 24 tools costing about 5,000 tokens to describe, snapshots of 1,800 to 6,300 characters, refs that change, a renamed argument, a password that leaked into the snapshot until --secrets hid it, and turning the session into a test that passes.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • npx playwright mcp
  • 24 tools ≈ 5,000 tokens
  • snapshots with refs
  • refs change
  • --secrets
  • review into a test

Notes

Set it up

Since Playwright one point six two, the MCP server ships with Playwright itself: the command is npx playwright mcp. You add it to your agent's MCP configuration, like any other server. Isolated keeps the browser profile in memory, so nothing leaks between sessions. Secrets we come back to. Version one point six three offers twenty four tools: navigate, snapshot, click, fill form, find, network requests and more.

MCP client config
{"mcpServers": {
  "playwright": {
    "command": "npx",
    "args": ["playwright", "mcp",
      "--isolated",
      "--secrets", ".env.test"]
}}}

Playwright 1.63: 24 tools
pin the Playwright version

The cost of tools

Tools are not free. The twenty four tool definitions, with their descriptions and schemas, came to about twenty thousand characters, roughly five thousand tokens, and the model receives them with every request. A whole session of eleven calls on ShopRung, logging in and adding a mug to the cart, returned about nine thousand three hundred characters. Turn off servers and tools you do not need.

Before the first click
24 tool definitions
  about 20,000 characters
  about 5,000 tokens
sent with every model request

one 11-call session on ShopRung:
  responses 9,285 characters,
  about 2,300 tokens

Snapshots

The agent sees the page as an accessibility snapshot: roles, accessible names and values, each with a ref such as e one seventy one, which it passes back to click. It is cheaper and more exact than a screenshot. Sizes on ShopRung: the login page about one thousand eight hundred characters, the product list with twelve products about six thousand three hundred. Browser find searches the snapshot for text, which costs less than reading it all.

browser_snapshot, cart page
- heading "Your cart" [ref=e133]
- row [ref=e145]:
  - cell "Green Build Mug"
  - spinbutton "Quantity of
    Green Build Mug": "1"
- generic [ref=e169]: Total
- generic [ref=e170]: $19.99
- button "Place order"
    [ref=e171]

Real breakage

Two real breakages. Refs are not stable: after adding a mug, the cart link went from e ten to e one thirty two, and an old ref gives an error asking for a new snapshot. And tools change between versions. Our first script sent the argument ref, as older examples do; version one point six three calls it target, and every click failed validation. Pin the Playwright version, like any dependency.

What went wrong
Cart link: ref e10 → e132 after
  adding a mug
old ref: "Ref e999 not found in
  the current page snapshot. Try
  capturing new snapshot."
our first script sent "ref";
  1.63 wants "target":
  "Invalid input: expected string,
   received undefined → at target"

Secrets

A real finding. Without the secrets option, the password Alice one two three was in the generated code and in the snapshot, even though it is a password field. Everything in a snapshot goes to the model provider and into logs. With secrets pointing at a dot env file, the agent fills the field by the secret's name, and both the code and the snapshot show only the name. Use test accounts only, and never real credentials.

Filling the password field
without --secrets:
  code: .fill('Alice123!')
  snapshot: textbox "Password"
    [ref=e20]: Alice123!
with --secrets .env.test:
  code: .fill(process.env
    ['ALICE_PASSWORD'])
  snapshot: <secret>ALICE_
    PASSWORD</secret>

Safety

Limit what the agent can reach. Use an isolated profile, test accounts and a test environment, never production with real customers. Two tools run arbitrary code; the server itself calls one of them unsafe and equivalent to remote code execution, so approve those calls. The allowed origins option is documented as not a security boundary. And remember Part ten: any text on a page can try to inject instructions into the agent.

Guard rails
  • --isolated: a fresh profile
  • test accounts, test data, a test environment only --secrets for every password
  • browser_run_code_unsafe and browser_evaluate run any code: approve each call
  • --allowed-origins is not a security boundary (its docs)
  • page text can inject the agent

Into a test

Every action returns the Playwright code it ran, with role based locators. Collect them, use relative URLs, and add the assertions the session never made: that Alice is logged in, and that the total is nineteen ninety nine. My own first assertion matched three table cells and failed in strict mode, so review and run everything an exploration produces. After the fix, the test passed in under a second.

From session to spec
session code, then reviewed:
goto('/#/login')
getByRole('textbox', {name:
  'Email'}).fill(...)
+ expect(...'Hi, Alice Shah')
+ expect(...'$19.99')
my first assertion: strict mode
  violation, 3 cells matched
fixed (exact: true): 1 passed

Common mistakes

Mistakes. Turning on every MCP server and tool, which fills the context before any work starts. Reusing old refs instead of taking a new snapshot. Not pinning versions. Typing real passwords without secrets, so they land in snapshots and logs. Pointing the agent at production or real accounts. Auto approving the unsafe tools. And treating an agent session as the test, instead of writing a real test from it.

Avoid these
  • every MCP server switched on
  • reusing old refs
  • unpinned tool versions
  • real passwords, no --secrets
  • production or real accounts
  • unsafe tools auto-approved
  • keeping the session, not writing the test

Say it like this

In the interview: I use it for exploring and drafting. The agent reads accessibility snapshots with refs and every action returns the Playwright code it ran. I pin the version, isolate the browser profile, keep passwords in a secrets file, use only test accounts, and approve the unsafe tools by hand. Then I turn the session into a reviewed test with real assertions, and run it in CI without any agent.

In the interview
  • "For exploring and drafting. The agent reads accessibility
  • snapshots with refs and each action returns Playwright code.
  • I pin the version, isolate the profile, use --secrets and test
  • accounts, approve unsafe tools, and turn the session into a reviewed test with real
  • assertions."

Interview questions

Q1What is Playwright MCP?

An MCP server, bundled with Playwright since 1.62 as `npx playwright mcp`, that gives an AI agent browser tools such as navigate, snapshot, click and fill form.

Q2What does the agent see of the page?

An accessibility snapshot: roles, accessible names and values, each element with a ref it can pass back to an action tool.

Q3What do the tools cost?

On Playwright 1.63, 24 tool definitions of about 20,000 characters, roughly 5,000 tokens, sent with every model request.

Q4Why did a click fail with "Ref not found"?

Refs belong to one snapshot and change as the page changes; take a new snapshot before acting.

Q5How do you keep passwords out of the model's context?

Start the server with --secrets and a dotenv file; the agent fills by the secret's name and snapshots show <secret>NAME</secret>. Without it, ShopRung's password appeared in the snapshot.

Q6Which tools need extra care?

browser_run_code_unsafe and browser_evaluate run arbitrary code; approve each call. --allowed-origins is documented as not a security boundary.

Q7How do you turn a session into a test?

Collect the Playwright code each action returns, make URLs relative, add assertions, review the locators and run it like any test.

Q8Why pin the Playwright version for MCP?

Tool schemas change: 1.63 expects a `target` argument where older examples used `ref`, so unpinned clients break.