PlayWright是微軟開發的Web UI自動化測試框架。
它旨在提供一個跨平台、跨語言、跨瀏覽器的自動化測試框架,同時也支援行動瀏覽器。
如其官方首頁所述:
當今最好的 Web UI 自動化測試框架是什麼?傑出的選項包括已有十年歷史的 Selenium、最近流行的 Cypress 以及我們在這裡介紹的 PlayWright。它們有何不同?以下是總結對比供大家參考
Feature | PlayWright | Selenium | Cypress |
---|---|---|---|
Supported Languages | JavaScript, Java, C#, Python | JavaScript, Java, C#, Python, Ruby | JavaScript/TypeScript |
Supported Browsers | Chrome, Edge, Firefox, Safari | Chrome, Edge, Firefox, Safari, IE | Chrome, Edge, Firefox, Safari |
Testing Framework | Frameworks for supported languages | Frameworks for supported languages | Frameworks for supported languages |
Usability | Easy to use and configure | Complex setup with a learning curve | Easy to use and configure |
Code Complexity | Simple | Moderate | Simple |
DOM Manipulation | Simple | Moderate | Simple |
Community Maturity | Improving gradually | Highly mature | Fairly mature |
Headless Mode Support | Yes | Yes | Yes |
Concurrency Support | Supported | Supported | Depends on CI/CD tools |
iframe Support | Supported | Supported | Supported via plugins |
Driver | Not required | Requires a browser-specific driver | Not required |
Multi-Tab Operations | Supported | Not supported | Supported |
Drag and Drop | Supported | Supported | Supported |
Built-in Reporting | Yes | No | Yes |
Cross-Origin Support | Supported | Supported | Supported |
Built-in Debugging | Yes | No | Yes |
Automatic Wait | Yes | No | Yes |
Built-in Screenshot/Video | Yes | No video recording | Yes |
雖然 PlayWright 支援多種語言,但它嚴重依賴 Node.js。無論您使用Python還是Java版本,PlayWright在初始化時都需要Node.js環境,下載Node.js驅動程式。因此,本指南我們將重點放在 JavaScript/TypeScript。
# Using npm npm init playwright@latest # Using yarn yarn create playwright
如果您選擇下載瀏覽器,PlayWright 將下載 Chromium、Firefox 和 WebKit,這可能需要一些時間。此過程僅在第一次設定期間發生,除非 PlayWright 版本已更新。
初始化後,您將得到一個項目範本:
playwright.config.ts # PlayWright configuration file package.json # Node.js configuration file package-lock.json # Node.js dependency lock file tests/ # Your test directory example.spec.ts # Template test case tests-examples/ # Example tests directory demo-todo-app.spec.ts # Example test case
運行範例測試案例:
npx playwright test
測試靜默執行(在無頭模式下),結果顯示為:
Running 6 tests using 6 workers 6 passed (10s) To open the last HTML report run: npx playwright show-report
這是 example.spec.ts 測試用例:
import { test, expect } from '@playwright/test'; test('has title', async ({ page }) => { await page.goto('https://playwright.dev/'); await expect(page).toHaveTitle(/Playwright/); }); test('get started link', async ({ page }) => { await page.goto('https://playwright.dev/'); await page.getByRole('link', { name: 'Get started' }).click(); await expect(page).toHaveURL(/.*intro/); });
每種測試方法都有:
主要方法包括:
以下是常用指令:
# Using npm npm init playwright@latest # Using yarn yarn create playwright
playwright.config.ts # PlayWright configuration file package.json # Node.js configuration file package-lock.json # Node.js dependency lock file tests/ # Your test directory example.spec.ts # Template test case tests-examples/ # Example tests directory demo-todo-app.spec.ts # Example test case
npx playwright test
使用codegen功能記錄互動:
Running 6 tests using 6 workers 6 passed (10s) To open the last HTML report run: npx playwright show-report
錄製的程式碼可以複製到您的檔案中。注意:記錄器可能無法處理懸停等複雜操作。
本節介紹一些典型的 Playwright 操作與頁面元素互動。請注意,前面介紹的定位器物件在建立過程中實際上並沒有在頁面上定位該元素。即使頁面上不存在該元素,使用元素定位器方法取得定位器物件也不會引發任何異常。實際的元素查找僅在互動期間發生。這與 Selenium 的 findElement 方法不同,後者直接在頁面上搜尋元素,如果找不到元素則拋出異常。
使用 fill 方法進行文字輸入,主要針對 、
import { test, expect } from '@playwright/test'; test('has title', async ({ page }) => { await page.goto('https://playwright.dev/'); await expect(page).toHaveTitle(/Playwright/); }); test('get started link', async ({ page }) => { await page.goto('https://playwright.dev/'); await page.getByRole('link', { name: 'Get started' }).click(); await expect(page).toHaveURL(/.*intro/); });
使用 locator.setChecked() 或 locator.check() 與 input[type=checkbox]、input[type=radio] 或具有 [role=checkbox] 屬性的元素進行交互:
npx playwright test
使用 locator.selectOption() 與
npx playwright test landing-page.spec.ts
基本操作:
npx playwright test --debug
對於被其他人覆蓋的元素,請用力點擊:
npx playwright codegen https://leapcell.io/
或以程式方式觸發點擊事件:
// Text input await page.getByRole('textbox').fill('Peter');
locator.type() 方法模擬逐個字元輸入,觸發 keydown、keyup 和 keypress 事件:
await page.getByLabel('I agree to the terms above').check(); expect(await page.getByLabel('Subscribe to newsletter').isChecked()).toBeTruthy(); // Uncheck await page.getByLabel('XL').setChecked(false);
使用 locator.press() 作為特殊鍵:
// Select by value await page.getByLabel('Choose a color').selectOption('blue'); // Select by label await page.getByLabel('Choose a color').selectOption({ label: 'Blue' }); // Multi-select await page.getByLabel('Choose multiple colors').selectOption(['red', 'green', 'blue']);
支援的按鍵包括反引號、減號、等於、反斜線、退格、製表符、刪除、轉義、ArrowDown、End、Enter、Home、Insert、PageDown、PageUp、ArrowRight、ArrowUp、F1 -F12、Digit0-Digit9 和KeyA -KeyZ。
使用locator.setInputFiles()指定要上傳的檔案。支援多個文件:
// Left click await page.getByRole('button').click(); // Double click await page.getByText('Item').dblclick(); // Right click await page.getByText('Item').click({ button: 'right' }); // Shift+click await page.getByText('Item').click({ modifiers: ['Shift'] }); // Hover await page.getByText('Item').hover(); // Click at specific position await page.getByText('Item').click({ position: { x: 0, y: 0 } });
使用 locator.focus() 聚焦在某個元素:
# Using npm npm init playwright@latest # Using yarn yarn create playwright
拖放過程涉及四個步驟:
您可以使用locator.dragTo()方法:
playwright.config.ts # PlayWright configuration file package.json # Node.js configuration file package-lock.json # Node.js dependency lock file tests/ # Your test directory example.spec.ts # Template test case tests-examples/ # Example tests directory demo-todo-app.spec.ts # Example test case
或者,手動實作該流程:
npx playwright test
預設情況下,Playwright 會自動取消警告、確認和提示等對話方塊。您可以預先註冊一個對話框處理程序來接受對話框:
Running 6 tests using 6 workers 6 passed (10s) To open the last HTML report run: npx playwright show-report
當新頁面彈出時,可以使用popup事件來處理:
import { test, expect } from '@playwright/test'; test('has title', async ({ page }) => { await page.goto('https://playwright.dev/'); await expect(page).toHaveTitle(/Playwright/); }); test('get started link', async ({ page }) => { await page.goto('https://playwright.dev/'); await page.getByRole('link', { name: 'Get started' }).click(); await expect(page).toHaveURL(/.*intro/); });
Leapcell 是一個專為分散式應用程式設計的現代雲端運算平台。它採用按量付費模式,無閒置成本,確保您只需為使用的資源付費。
成本效率
開發者體驗
可擴充性和效能
更多部署範例,請參閱文件。
以上是Playwright:Web UI 自動化測試框架全面概述的詳細內容。更多資訊請關注PHP中文網其他相關文章!