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中文网其他相关文章!