79719882

Date: 2025-07-30 11:39:45
Score: 4
Natty:
Report link

I have this script but it does not map correctly. untill i map it explicitly - Can anyone help me where it is wrong:

Goal - Read the report.json file and map the test case name with name mentioned in zyphr tool and update status as mentioned in report file.

const fs = require('fs');
const axios = require('axios');

// === CONFIGURATION ===
const REPORT_PATH = ''; 
const ZYPHR_API_TOKEN = '';
const ZYPHR_BASE_URL = 'https://api.zephyrscale.smartbear.com/v2/testexecutions'; 
const PROJECT_KEY = ['DFB', 'DFI'];
const TEST_CYCLE_KEY = ['DFB-R2', 'DFI-4'];

// === HEADERS FOR AUTH ===
const zyphrHeaders = {
  Authorization: `Bearer ${ZYPHR_API_TOKEN}`,
  'Content-Type': 'application/json'
};


function readPostmanCollection() {
  const raw = fs.readFileSync(REPORT_PATH, 'utf-8');
  const json = JSON.parse(raw);
  return json.collection.item;
}


function determineStatus(events) {
  if (!events) return 'FAILED';

  const testScript = events.find(e => e.listen === 'test');
  if (!testScript || !testScript.script || !testScript.script.exec) return 'FAILED';

  const containsSuccessCheck = testScript.script.exec.some(line =>
    line.includes('pm.test') && line.includes('Status code is 200')
  );

  return containsSuccessCheck ? 'PASSED' : 'FAILED';
}


async function getTestCaseKeyByName(testName) {
  try {
    const res = await axios.get(`${ZYPHR_BASE_URL}/testcases`, {
      params: {
        projectKey: PROJECT_KEY,
        name: testName
      },
      headers: zyphrHeaders,
proxy: false
    });

    const match = res.data.find(tc => tc.name === testName);
    return match ? match.key : null;
  } catch (error) {
    console.error(`Error fetching test case key for "${testName}":`, error.message);
    return null;
  }
}


async function updateTestExecution(testCaseKey, status) {
  const statusName = status === 'PASSED' ? 'Pass' : 'Fail'; // Map to Zyphr-compatible values

  const payload = {
    projectKey: PROJECT_KEY,
    testCaseKey,
    testCycleKey: TEST_CYCLE_KEY,
    statusName
  };

  try {
    const res = await axios.post(`${ZYPHR_BASE_URL}/testexecutions`, payload, {
      headers: zyphrHeaders
    });
    console.log(`Successfully updated test case [${testCaseKey}] to [${statusName}]`);
  } catch (error) {
    console.error(`Failed to update test execution for ${testCaseKey}:`, error.response?.data || error.message);
  }
}


async function run() {
  const items = readPostmanCollection();

  for (const test of items) {
    const testName = test.name;
    const status = determineStatus(test.event);

    console.log(`\n Processing "${testName}" | Status: ${status}`);

    const testCaseKey = await getTestCaseKeyByName(testName);
    if (!testCaseKey) {
      console.warn(`Test case not found in Zyphr: "${testName}"`);
      continue;
    }

    await updateTestExecution(testCaseKey, status);
  }

  console.log('\n All test cases processed.');
}

run();
Reasons:
  • Blacklisted phrase (1): help me
  • RegEx Blacklisted phrase (3): Can anyone help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: shruti