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

HP ALM API密钥配置方法咨询及测试用例提取Java程序问题

HP ALM API密钥配置步骤及Java代码实现

一、HP ALM API密钥配置步骤

操作需拥有站点管理员或项目管理员权限:

  • 登录ALM Web控制台:访问你的ALM服务器地址(格式如http://your_alm_server:port/qcbin),用有权限的账号登录
  • 打开API密钥管理:点击右上角用户名,在下拉菜单中选择「API Keys」(部分版本可能显示为「API密钥管理」)
  • 生成新密钥:
    • 点击「Create API Key」按钮
    • 填写密钥描述(用于标识用途,比如「测试用例提取程序专用」),可选择设置过期时间
    • 点击「Generate」生成密钥,立即复制并保存完整密钥字符串(关闭弹窗后无法再次查看完整内容)
  • 配置权限:确保该密钥关联的用户账号拥有目标项目的测试用例读取权限,以及「访问REST API」的权限(可在ALM的「权限管理」模块中配置)

二、用于提取测试用例的Java代码

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ALMTestCaseExtractor {

    public static void main(String[] args) {
        String almURL = "http://your_alm_server:port/qcbin";
        String almDomain = "your_domain";
        String almProject = "your_project";
        String almAPIKey = "your_api_key"; // 替换为你生成的API密钥
        String almFolderPath = "Subject\\Folder1\\Folder2"; // 替换为实际测试用例文件夹路径
        String excelFilePath = "path/to/excel/file.xlsx";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 设置API密钥授权头
            String authorizationHeader = "Bearer " + almAPIKey;

            // 构造测试用例请求URL
            String testCasesUrl = almURL + "/rest/domains/" + almDomain + "/projects/" + almProject
                    + "/test-instances?query=" + almFolderPath;
            HttpGet httpGet = new HttpGet(testCasesUrl);
            httpGet.setHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);
            httpGet.setHeader(HttpHeaders.ACCEPT, "application/json");

            String responseString = EntityUtils.toString(httpClient.execute(httpGet).getEntity());
            List<Map<String, Object>> testCases = extractTestCasesFromResponse(responseString);

            // 创建Excel并写入数据
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Test Cases");

            // 写入表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("Test Case Name");
            headerRow.createCell(1).setCellValue("Description");
            headerRow.createCell(2).setCellValue("Steps");
            headerRow.createCell(3).setCellValue("Expected Results");

            // 写入测试用例数据
            int rowIndex = 1;
            if (testCases != null) {
                for (Map<String, Object> testCase : testCases) {
                    Row row = sheet.createRow(rowIndex++);
                    row.createCell(0).setCellValue((String) testCase.get("name"));
                    row.createCell(1).setCellValue((String) testCase.get("description"));
                    row.createCell(2).setCellValue((String) testCase.get("steps"));
                    row.createCell(3).setCellValue((String) testCase.get("expected-results"));
                }
            }

            // 保存Excel文件
            try (OutputStream outputStream = new FileOutputStream(excelFilePath)) {
                workbook.write(outputStream);
                System.out.println("Test cases exported to Excel successfully.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<Map<String, Object>> extractTestCasesFromResponse(String responseString) {
        // 使用Jackson解析JSON响应,需添加Jackson依赖到项目
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 根据ALM REST API的实际响应结构调整解析逻辑,此处示例假设响应是包含测试用例的数组
            return objectMapper.readValue(responseString, List.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

代码注意事项

  • 需添加对应依赖:Apache HttpClient、Apache POI、Jackson(或其他JSON解析库)到你的项目依赖中
  • 替换代码中的占位符(如服务器地址、API密钥、项目信息等)为实际值
  • 根据ALM REST API返回的JSON结构,调整extractTestCasesFromResponse方法的解析逻辑,确保能正确提取测试用例数据

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:55:29