HLJ 发布于
2022-11-06 13:48:22
79阅读

playwright 测试用例报告记录跟踪保存文件完整实例代码

初始化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())
  }

  // 遍历tag标签
  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())
  }
  // 停止跟踪并将其导出到 zip 存档中。
  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())

  // 停止跟踪并将其导出到 zip 存档中。
  await context.tracing.stop({ path: "trace.zip" })
});
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-11-06/615.html
最后生成于 2024-06-22 12:07:30
此内容有帮助 ?
0