You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Playwright测试集成至本地部署的Zephyr Scale并同步执行状态?

连接Playwright测试到本地部署的Zephyr Scale并同步测试状态

一、准备Zephyr Scale基础信息与凭证

  1. 获取Zephyr Scale内网API地址:本地部署的Zephyr API基础路径通常为 http://你的Zephyr内网域名/rest/atm/1.0/(需根据实际部署版本调整API版本号)
  2. 生成API Token:在Zephyr Scale的个人设置中创建具备测试结果写入权限的API Token
  3. 确定目标测试周期ID:在Zephyr Scale的测试周期页面,从URL或页面信息中获取周期标识(如CYC-123)

二、在Playwright项目中集成Zephyr同步逻辑

1. 安装依赖

在项目根目录执行:

npm install axios dotenv

2. 编写Zephyr工具函数

创建zephyr-utils.js文件,封装测试状态更新的API调用:

const axios = require('axios');
require('dotenv').config();

const zephyrConfig = {
  apiUrl: process.env.ZEPHYR_API_URL,
  apiToken: process.env.ZEPHYR_API_TOKEN,
  testCycleId: process.env.ZEPHYR_TEST_CYCLE_ID
};

async function updateZephyrTestResult(testCaseKey, status) {
  if (!testCaseKey) return;
  
  try {
    await axios.post(
      `${zephyrConfig.apiUrl}/testresult`,
      {
        testCaseKey: testCaseKey,
        testCycleKey: zephyrConfig.testCycleId,
        status: status,
        comment: `Playwright执行时间: ${new Date().toLocaleString()}`
      },
      {
        headers: {
          'Authorization': `Bearer ${zephyrConfig.apiToken}`,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log(`✅ 测试用例 ${testCaseKey} 状态已更新为 ${status}`);
  } catch (err) {
    console.error(`❌ 更新 ${testCaseKey} 状态失败: ${err.response?.data?.message || err.message}`);
  }
}

module.exports = { updateZephyrTestResult };

3. 在测试用例中关联Zephyr并同步状态

修改Playwright测试文件,通过钩子函数自动同步结果:

const { test, expect } = require('@playwright/test');
const { updateZephyrTestResult } = require('../zephyr-utils');

// 全局后置钩子:每个测试用例执行完成后同步状态
test.afterEach(async ({ testInfo }) => {
  // 从测试标题中提取Zephyr用例ID(格式:@ZephyrTestCase=TC-XXX)
  const caseIdMatch = testInfo.title.match(/@ZephyrTestCase=([A-Z0-9-]+)/);
  if (!caseIdMatch) return;

  const testCaseKey = caseIdMatch[1];
  const statusMap = {
    passed: 'Pass',
    failed: 'Fail',
    skipped: 'Skip',
    timedOut: 'Blocked'
  };
  await updateZephyrTestResult(testCaseKey, statusMap[testInfo.status] || 'Blocked');
});

// 示例测试用例:标题中携带Zephyr用例ID
test('用户登录成功 @ZephyrTestCase=TC-456', async ({ page }) => {
  await page.goto('/login');
  await page.fill('#username', 'test_user');
  await page.fill('#password', 'test_pass');
  await page.click('#submit-btn');
  await expect(page.locator('#welcome-text')).toHaveText('欢迎回来');
});

test('错误密码登录失败 @ZephyrTestCase=TC-457', async ({ page }) => {
  await page.goto('/login');
  await page.fill('#username', 'test_user');
  await page.fill('#password', 'wrong_pass');
  await page.click('#submit-btn');
  await expect(page.locator('#error-alert')).toHaveText('密码错误');
});

三、本地执行配置

  1. 在项目根目录创建.env文件,填入Zephyr配置:
ZEPHYR_API_URL=http://你的Zephyr内网域名/rest/atm/1.0/
ZEPHYR_API_TOKEN=你的API_TOKEN
ZEPHYR_TEST_CYCLE_ID=CYC-123
  1. 执行测试:
npx playwright test

四、Jenkins执行配置

  1. 确保Jenkins节点能访问Zephyr内网地址(无防火墙/网络策略限制)
  2. 在Jenkins任务中配置环境变量:
    • 自由风格任务:在「构建环境」中添加环境变量,或使用Jenkins凭证存储API Token
    • 流水线任务:通过environment块注入配置(推荐用凭证存储敏感信息)
      示例流水线:
    pipeline {
      agent any
      environment {
        ZEPHYR_API_URL = 'http://你的Zephyr内网域名/rest/atm/1.0/'
        ZEPHYR_API_TOKEN = credentials('zephyr-api-token') // 关联Jenkins凭证
        ZEPHYR_TEST_CYCLE_ID = 'CYC-123'
      }
      stages {
        stage('安装依赖') {
          steps {
            sh 'npm install'
          }
        }
        stage('执行Playwright测试') {
          steps {
            sh 'npx playwright test'
          }
        }
      }
    }
    

注意事项

  • 确认Zephyr测试用例已提前关联到目标测试周期
  • API版本需与本地部署的Zephyr Scale版本匹配(可参考官方文档调整API路径)
  • 若测试用例未关联Zephyr ID,钩子函数会自动跳过同步逻辑

内容的提问来源于stack exchange,提问作者rahul patidar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:01:25