UNPKG

fintech-automation-test

Version:
128 lines (112 loc) 3.06 kB
// In steps_file.js const { I } = inject(); const axios = require('axios'); const fs = require('fs'); const config = require('./config'); const FormData = require('form-data'); module.exports = function () { return actor({ updateTestCaseStatus( testCycleKey, testCaseId, statusName, actualResult, screenshotPath ) { const JIRA_API_URL = config.zapiBaseUrl; const JIRA_API_KEY = config.zapiBearerToken; const currentDate = new Date().toISOString(); const testCase = this.getTestCases(testCaseId); const testSteps = this.getTestSteps(testCase.testScript.self); const testScriptResults = Array(testSteps.length).fill({ statusName, actualResult, }); const data = { projectKey: config.projectKey, testCycleKey, testCaseKey: testCaseId, statusName, actualEndDate: currentDate, testScriptResults, }; try { const response = axios.post( `${JIRA_API_URL}/testexecutions`, data, { headers: this.getJiraHeaders() } ); console.log(`Test case status updated: ${response.data.self}`); if (screenshotPath) { this.uploadScreenshot(response.data.id, screenshotPath); } } catch (error) { this.handleError('Error updating test case status', error); } }, async getTestCases(testCaseKey) { const JIRA_API_URL = config.zapiBaseUrl; try { const response = await axios.get( `${JIRA_API_URL}/testcases/${testCaseKey}`, { headers: this.getJiraHeaders() } ); return response.data; } catch (error) { this.handleError( `Error fetching test case ${testCaseKey}`, error ); } }, async getTestSteps(testScriptUrl) { try { const response = await axios.get(testScriptUrl, { headers: this.getJiraHeaders(), params: { maxResults: 50, startAt: 0, }, }); return response.data.values; } catch (error) { this.handleError('Error fetching test steps', error); } }, async uploadScreenshot(issueIdOrKey, screenshotPath) { const JIRA_API_URL = config.zapiBaseUrl; const formData = new FormData(); formData.append('file', fs.createReadStream(screenshotPath)); try { const response = await axios.post( `${JIRA_API_URL}/issue/${issueIdOrKey}/attachments`, formData, { headers: { ...this.getJiraHeaders(), 'X-Atlassian-Token': 'no-check', ...formData.getHeaders(), }, } ); console.log('Screenshot uploaded:', response.data); return response.data; } catch (error) { this.handleError('Error uploading screenshot', error); } }, getJiraHeaders() { return { Authorization: `Bearer ${config.zapiBearerToken}`, 'Content-Type': 'application/json', }; }, handleError(message, error) { console.error( `${message}:`, error.response?.data?.message || error.message ); throw error; }, }); };