playwright-zephyr
Version:
Zephyr reporter for the Playwright
88 lines (87 loc) ⢠3.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZephyrService = void 0;
const axios_1 = __importDefault(require("axios"));
const table_1 = require("table");
const util_1 = require("util");
const fs_1 = require("fs");
const picocolors_1 = require("picocolors");
const form_data_1 = __importDefault(require("form-data"));
const validate_options_1 = require("./validate-options");
function isAxiosError(error) {
return error.isAxiosError === true;
}
class ZephyrService {
authorizationToken;
projectKey;
testCycle;
url = 'https://api.zephyrscale.smartbear.com/v2';
defaultRunName = `Automated Playwright run - [${new Date().toUTCString()}]`;
constructor(options) {
(0, validate_options_1.validateOptions)(options);
this.projectKey = options.projectKey;
this.authorizationToken = options.authorizationToken;
this.testCycle = options.testCycle;
}
async createRun(testResults) {
const url = `${this.url}/automations/executions/custom?projectKey=${this.projectKey}&autoCreateTestCases=false`;
const data = new form_data_1.default();
const testCycleDefault = {
name: this.defaultRunName,
...this.testCycle,
};
data.append('file', (0, fs_1.createReadStream)(testResults));
data.append('testCycle', JSON.stringify(testCycleDefault), { contentType: 'application/json' });
try {
const response = await (0, axios_1.default)({
url,
method: 'POST',
headers: {
Authorization: `Bearer ${this.authorizationToken}`,
...data.getHeaders(),
},
data,
});
if (response.status !== 200)
throw new Error(`${response.status} - Failed to create test cycle`);
const { data: { testCycle }, } = response;
this.printReportDetails(testCycle);
return response.data;
}
catch (error) {
this.handleAxiosError(error);
}
}
printReportDetails(testCycle) {
const tableData = [
[(0, picocolors_1.bold)((0, picocolors_1.green)(`ā
Test cycle ${testCycle.key} has been created`))],
[(0, picocolors_1.bold)((0, picocolors_1.gray)('š Check out the test result'))],
[(0, picocolors_1.bold)((0, picocolors_1.blue)(`š ${testCycle.url}`))],
];
const report = (0, table_1.table)(tableData, {
border: (0, table_1.getBorderCharacters)('norc'),
singleLine: true,
});
console.log((0, picocolors_1.bold)('\nš Zephyr Scale Report details:'));
console.log(report);
}
handleAxiosError(error) {
if (isAxiosError(error)) {
console.error(`Config: ${(0, util_1.inspect)(error.config)}`);
if (error.response) {
throw new Error(`\nStatus: ${error.response.status} \nHeaders: ${(0, util_1.inspect)(error.response.headers)} \nData: ${(0, util_1.inspect)(error.response.data)}`);
}
else if (error.request) {
throw new Error(`The request was made but no response was received. \n Error: ${(0, util_1.inspect)(error.toJSON())}`);
}
else {
throw new Error(`Something happened in setting up the request that triggered an Error\n : ${(0, util_1.inspect)(error.message)}`);
}
}
throw new Error(`\nUnknown error: ${error}`);
}
}
exports.ZephyrService = ZephyrService;