Cypress 入門 + vs Playwright 完整對比 — 該選哪個 + 遷移實戰
Cypress 完整入門 + 跟 Playwright 的真實對比。架構差異、Selector 哲學、Cross-origin / Multi-tab / 並行、生態系、價格、何時該選 / 何時該遷。
💡 本文原刊於 qa.9niche.com,2026-08 併入 9niche.com 懶人包,內容照原文完整搬遷。
目錄
1. 前言
「我們公司用 Cypress、面試問 Playwright 我不會、該轉嗎?」 — 看到太多 QA 卡在這。實話:Cypress 還很好用、但 Playwright 全方位贏。這篇講清楚兩者差異 + 何時該轉。
2. 一張圖看完差異
3. Cypress 30 分鐘入門
安裝
npm init -y
npm install --save-dev cypress
npx cypress open
GUI 介面選「E2E Testing」、Cypress 會自動建好 demo。
第一個 test
// cypress/e2e/login.cy.js
describe('Login', () => {
beforeEach(() => {
cy.visit('https://app.example.com/login');
});
it('登入成功跳轉 dashboard', () => {
cy.get('input[name=email]').type('[email protected]');
cy.get('input[name=password]').type('Test@123');
cy.contains('button', '登入').click();
cy.url().should('include', '/dashboard');
cy.contains('Welcome').should('be.visible');
});
it('錯密碼顯示錯誤', () => {
cy.get('input[name=email]').type('[email protected]');
cy.get('input[name=password]').type('wrong');
cy.contains('button', '登入').click();
cy.contains('帳號或密碼錯誤').should('be.visible');
});
});
跑:
npx cypress run # headless
npx cypress run --browser chrome # 指定瀏覽器
npx cypress open # GUI 互動
Cypress 最大強項 — Time Travel Debugger
這個 UX 至今沒對手。Playwright trace viewer 接近但體驗仍差一截。
4. Cypress vs Playwright 全方位對比
| 維度 | Cypress | Playwright | 贏家 |
|---|---|---|---|
| 學習曲線 | 低(chainable API 直覺) | 中(async/await 標準) | Cypress |
| Debug UX | 神級 time travel | 好的 trace viewer | Cypress |
| 多瀏覽器 | Chrome / Edge / Firefox | + WebKit (Safari) | Playwright |
| Cross-origin | 受限(需 setting) | 原生支援 | Playwright |
| Multi-tab / window | 困難 / hack | 原生 | Playwright |
| iframe | 困難 | 原生 page.frame() | Playwright |
| 平行測試 | 要付費 Cypress Cloud | 免費內建 sharding | Playwright |
| API 測試 | cy.request() | request fixture | 平手 |
| Component testing | 強 | 弱 | Cypress |
| Mobile emulation | 受限 | 強 | Playwright |
| Auto-wait | 是 | 是 | 平手 |
| TypeScript | 支援 | 一等公民 | Playwright |
| CI 整合 | 好 | 更好 | Playwright |
| 企業支援 | Cypress Cloud | Microsoft 支持 | Playwright |
| 社群活躍 | 大但減 | 暴增中 | Playwright |
| NPM downloads | 5M/week | 12M/week (2026) | Playwright |
5. Selector 哲學差異
// Cypress — chainable + 自家 API
cy.get('button')
.contains('Submit')
.click();
cy.get('[data-testid=email]')
.type('[email protected]');
// Playwright — standard async/await
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByLabel('Email').fill('[email protected]');
Playwright 用 getByRole 跟 accessibility tree 走、跨 UI 改動更穩。 Cypress 鼓勵 data-testid、要前端配合。
6. Cross-origin 痛點實戰
// Cypress — OAuth 登入會死
it('OAuth login', () => {
cy.visit('/login');
cy.contains('Sign in with Google').click();
// ❌ 跳到 accounts.google.com → cy.url() fail
// 需要 cy.origin() 或 mock 整段 flow
});
// Playwright — 沒事
test('OAuth login', async ({ page }) => {
await page.goto('/login');
await page.click('text=Sign in with Google');
await page.fill('input[type=email]', '[email protected]');
await page.click('button:has-text("Next")');
// ✓ 跨域沒問題
});
OAuth 工作流 = Playwright 絕對選。
7. Component Testing — Cypress 還領先
// Cypress + React component test
import Button from './Button';
describe('Button', () => {
it('renders text', () => {
cy.mount(<Button>Click me</Button>);
cy.contains('Click me').should('be.visible');
});
it('handles click', () => {
const onClick = cy.stub();
cy.mount(<Button onClick={onClick}>Click</Button>);
cy.contains('Click').click();
cy.wrap(onClick).should('have.been.calledOnce');
});
});
這塊 Cypress UX 還是最好。但新案推 Vitest + Testing Library(更快、更輕量)。
8. 平行測試 — Cypress 收費痛
Cypress 平行測試需要 Cypress Cloud 訂閱。150 個 case 跑 30 分鐘、付費後變 8 分鐘 — 但每月 $75 起跳。 Playwright --shard=1/4 免費就有。
9. 遷移 Cypress → Playwright 實戰
Step 1: 評估
Step 2: API 對照表
| Cypress | Playwright |
|---|---|
cy.visit(url) | await page.goto(url) |
cy.get(sel).click() | await page.click(sel) |
cy.get(sel).type(val) | await page.fill(sel, val) |
cy.contains(text) | await page.getByText(text) |
cy.url().should('include') | await expect(page).toHaveURL() |
cy.intercept('POST', url) | await page.route(url) |
cy.fixture('users.json') | import users from './users.json' |
cy.task('db:reset') | 直接呼叫 helper function |
beforeEach() | test.beforeEach() |
Step 3: codemod 工具
npx playwright-codemod-cypress ./cypress/e2e/
會幫你轉 80%、剩 20% 手工。
Step 4: 跑舊新平行 1-2 週
確認 case behavior 一致、再下架 Cypress。
10. 反模式 — Cypress 該換的訊號
任 3 個 → 啟動遷移討論。
11. 面試常考題
Q1: Cypress 跟 Playwright 差別?
好答案(30 秒版):
Cypress 是 chainable API、time travel debugger 神級、Chrome-only 體驗最好。Playwright 是 async/await 標準、原生 cross-origin / multi-tab / 多瀏覽器 / 免費平行。新案推 Playwright、舊 Cypress 案不一定要遷。
Q2: 你會選哪個?
好答案:
看情境。新案 90% 選 Playwright(多平台 + 免費平行)。Component testing 有時保留 Cypress。但團隊現在大多走 Playwright + Vitest 組合、Cypress 漸式微。
Q3: Cypress 如何處理 cross-origin?
好答案:
用
cy.origin('https://other.com', () => { ... })、但仍有 cookie 限制與環境變數設置麻煩。Playwright 原生支援、寫起來像同 domain。
12. 給仍在 Cypress 的人
不要急著遷。但:
1. 學 Playwright(找工作必備)
2. 新 case 開始用 Playwright
3. 舊 case 維持 Cypress 直到痛點累積
4. 痛了再 codemod 遷
兩個都會的 QA = 求職市場熱門。
13. 最後
Cypress 是好工具、time travel UX 至今沒對手。但整體生態 + 跨平台支援 + 免費平行 → Playwright 全方位贏。2026 年的新案沒理由選 Cypress、但 5 年的 Cypress 案也不用急遷。會兩個 = 最強。
延伸:
相關連結
Playwright 安裝、第一個測試、Selectors 選擇策略、debug 技巧、CI 整合。給有測試經驗但沒寫過自動化的 QA。
POM 完整指南。為什麼用、怎麼拆 class、Playwright 實作範例、Component Object 進階、反模式。附類別關係圖與重構流程。
從零配出 production-ready 的 Playwright CI。完整 yml、失敗截圖/trace 自動上傳、PR 自動留言報告、平行 sharding 加速。
相關懶人包
2026 QA 趨勢實戰:我看到的 5 個轉變(AI、Shift-Left、Observability)
從手動 QA 到 AI 輔助、從測試金字塔到測試獎盃。這篇分享我這 10+ 年看 QA 從「測完才知道」到「shift-left + AI」的真實觀察。
2026 QA 面試的 AI 題 — 12 題 + 答題框架(面試官想聽什麼)
2026 QA 面試新增一整類「你怎麼用 AI」的問題。這篇整理 12 個高頻 AI 面試題、每題附面試官真正想聽的點與答題框架,從「你用過哪些 AI 工具」到「AI 生的 test 怎麼信任」。
AI / LLM 功能 Spec Review — 幻覺 / 評估 / 成本 / 法遵 8 個必問
AI 功能 spec review 完整指南。LLM 不確定性處理、評估指標、Prompt versioning、成本控制、安全護欄、法遵(EU AI Act / GDPR)、Fallback、人工 review 流程。
一般聲明
本站提供之資訊僅供參考,不保證其完整性與正確性。使用者應自行判斷資訊之適用性。