Playwright自动化测试
一、安装 Playwright
首先,你需要安装 Playwright 及其依赖的浏览器:
bash 运行
npm init playwright@latest
安装过程中,Playwright 会自动下载并安装所需的浏览器驱动。
二、常见的元素定位方法和示例
1. 使用选择器定位元素
Playwright 支持多种选择器语法,包括 CSS、XPath、文本内容等:
CSS 选择器(最常用)
javascript 运行
// 通过ID定位
await page.locator('#username').fill('testuser');
// 通过类名定位
await page.locator('.btn-primary').click();
// 通过属性定位
await page.locator('input\[type="password"\]').fill('password');
// 组合选择器
await page.locator('div.card > button.submit').click();
XPath 选择器
javascript 运行
// 使用XPath定位
await page.locator('//div\[@\]//a\[text()="Sign Up"\]').click();
文本选择器
javascript 运作
// 通过文本内容定位按钮
await page.locator('text=Submit Form').click();
// 部分文本匹配(包含"Save"的元素)
await page.locator('text=Save').click();
// 区分大小写的文本匹配
await page.locator('text="Login"').click();
2. 基于角色定位(推荐)
Playwright 提供了基于语义角色的定位方式,更符合用户交互习惯:
javascript 运行
// 定位按钮(role="button")
await page.getByRole('button', { name: 'Submit' }).click();
// 定位链接
await page.getByRole('link', { name: 'Learn More' }).click();
// 定位输入框
await page.getByRole('textbox', { name: 'Email' }).fill('test@example.com');
// 定位复选框
await page.getByRole('checkbox', { name: 'Remember me' }).check();
3. 基于标签和文本组合定位
javascript 运行
// 定位包含特定文本的div
await page.locator('div:has-text("Welcome")').click();
// 定位包含子元素的父元素
await page.locator('article:has(img.thumbnail)').click();
4. 链式定位
通过链式调用定位更精确的元素:
javascript 运行
// 先定位表单,再定位其中的按钮
const form = page.locator('form.login-form');
await form.locator('button\[type="submit"\]').click();
// 定位下一个兄弟元素
await page.locator('input#username').locator('xpath=following-sibling::button').click();
5. 使用页面元素引用
先获取元素引用,再执行操作:
javascript 运行
const element = await page.locator('button.submit');
await element.click();
await expect(element).toHaveText('Submitted');
6. 处理动态元素
使用nth()选择多个匹配元素中的第 N 个(索引从 0 开始):
javascript 运行
// 选择第三个列表项
await page.locator('ul.items > li').nth(2).click();
// 选择最后一个元素
const items = await page.locator('ul.items > li');
await items.nth(await items.count() - 1).click();
7. 等待元素状态
Playwright 会自动等待元素可交互,但也可以显式等待特定状态:
javascript 运行
// 等待元素可见后点击
await page.locator('div.loading').waitFor({ state: 'hidden' });
await page.locator('button.submit').click();
// 等待元素启用
await page.locator('button').waitFor({ state: 'enabled' });
8. 调试定位问题
使用page.pause()暂停测试,或使用console.log()输出元素信息:
javascript 运行
// 打印匹配的元素数量S
const count = await page.locator('div.item').count();
console.log(\`找到 ${count} 个元素\`);
// 调试模式(需安装playwright CLI)
await page.pause(); // 会打开浏览器调试工具
三、编写第一个测试用例
下面是一个使用 Playwright 测试 GitHub 登录页面的示例:
javascript 运行
// tests/github-login.spec.jsconst { test, expect } = require('@playwright/test');
test('GitHub登录测试', async ({ page }) => {
// 访问GitHub登录页面
await page.goto('https://github.com/login');
// 填写用户名和密码
await page.fill('input\[name="login"\]', 'your\_username');
await page.fill('input\[name="password"\]', 'your\_password');
// 点击登录按钮
await page.click('input\[type="submit"\]');
// 断言登录成功后页面会跳转到仪表盘
await expect(page).toHaveURL('https://github.com/');
// 断言页面包含用户头像元素
await expect(page.locator('.avatar-user')).toBeVisible();});
四、运行测试
使用以下命令运行测试:
bash 运行
npx playwright test
五、测试报告
运行测试后,可以生成 HTML 报告查看详细结果:
bash 运行
npx playwright show-report
六、更多高级功能
1. 浏览器上下文和多页面测试
javascript 运行
test('多页面交互测试', async ({ browser }) => {
const context = await browser.newContext();
const page1 = await context.newPage();
const page2 = await context.newPage();
// 在不同页面执行操作
await page1.goto('https://example.com');
await page2.goto('https://another-example.com');
// 断言两个页面的交互结果
});
2. 自动等待和断言
Playwright 会自动等待元素可点击、可见等状态,减少了手动添加等待的代码:
javascript 运行
// 无需手动添加等待,Playwright会自动等待按钮可点击
await page.click('button:has-text("Submit")');
3. 调试工具
使用—debug参数可以进入调试模式:
bash 运行
npx playwright test --debug
4. 截屏和视频录制
javascript 运行
test('截屏测试', async ({ page }) => {
await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });
});