初始化playwright
npm init playwright@latest
初始化后产生目录
playwright.config.js
package.json
package-lock.json
tests/
example.spec.js
tests-examples/
demo-todo-app.spec.js
测试用例example.spec.js文件代码
import { test, expect } from '@playwright/test';
test('test', async ({ page, context }) => {
const obj = {
screenshots: true,
snapshots: true,
sources: true
}
await context.tracing.start(obj)
await page.goto('http://good1230.com/');
const count = await page.locator('ul.width-135 > a.none-line').count();
const rows = await page.locator('ul.width-135 > a.none-line');
for (let i = 0; i < count; ++i) {
await rows.nth(i).click()
await expect(page).toHaveURL(page.url());
console.log(page.url())
}
const count2 = await page.locator('div.width-300 ul > li > a.none-line').count();
const rows2 = await page.locator('div.width-300 ul > li > a.none-line');
for (let i = 0; i < count2; ++i) {
await rows2.nth(i).click()
await expect(page).toHaveURL(page.url());
console.log(page.url())
}
await context.tracing.stop({ path: "trace.zip" })
});
运行测试用例
npx playwright test
查看测试用例报告
npx playwright show-report
查看记录跟踪文件
npx playwright show-trace trace.zip
import { test, expect } from '@playwright/test';
test('test', async ({ page, context }) => {
const obj = {
screenshots: true,
snapshots: true,
sources: true
}
await context.tracing.start(obj)
await page.goto('http://localhost:8080/#/login');
await page.fill('#userName', 'admin@good.com');
await page.fill('#password', 'long10000');
await page.locator('#login2').click();
const [popup] = await Promise.all([
page.waitForResponse((resp) => {
return resp
})
]);
console.log(popup.status())
console.log(page.url())
await expect(page).toHaveURL('http://localhost:8080/#/pannellum');
const rows = await page.locator('li.menu > span');
await rows.nth(0).click()
await expect(page).toHaveURL('http://localhost:8080/#/pannellum', { timeout: 5000 });
console.log(page.url())
await context.tracing.stop({ path: "trace.zip" })
});