Skip to content

Commit

Permalink
new logic for collecting failed tests from TestRail (#4151)
Browse files Browse the repository at this point in the history
* new logic for collecting failed tests from TestRail

* old version of glob
  • Loading branch information
ostapwd authored Sep 3, 2024
1 parent fbdd166 commit 175b9ec
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
122 changes: 122 additions & 0 deletions collect-failed-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable no-console */
const { glob } = require('glob');
const fs = require('fs');
const { getTestNames } = require('find-test-names');
const axios = require('axios');

const status = {
Passed: 1,
Blocked: 2,
Untested: 3,
Retest: 4,
Failed: 5,
};

const team = {
Firebird: 3,
Folijet: 4,
Spitfire: 6,
Thunderjet: 8,
Vega: 9,
Volaris: 13,
Corsair: 19,
};

const selectedStatus = [status.Failed];
const selectedTeams = [team.Thunderjet, team.Folijet];

const testUsername = '';
const testPassword = '';
const runId = 2581;

const ids = [];
const arrayOfFiles = [];
const filteredFiles = [];

function getTest(offsetToPass) {
return axios({
method: 'get',
url: `https://foliotest.testrail.io/index.php?/api/v2/get_tests/${runId}`,
headers: { 'Content-Type': 'application/json' },
params: { offset: offsetToPass },
auth: {
username: testUsername,
password: testPassword,
},
}).then((response) => {
console.log(`GET /tests (offset: ${offsetToPass}, length: ${response.data.tests.length})`);
return response.data.tests;
});
}

async function getTests() {
const tests = [];
let offset = 0;
let testsFromResponse = 0;
do {
testsFromResponse = await getTest(offset);
tests.push(...testsFromResponse);
offset += 250;
} while (testsFromResponse.length === 250);
return tests;
}

function removeRootPath(path) {
return path.substring(path.indexOf('cypress\\e2e\\'));
}

function titleContainsId(title, testCaseIds) {
if (title === undefined) {
return false;
}
for (let i = 0; i < testCaseIds.length; i++) {
if (title.includes(testCaseIds[i])) {
return true;
}
}
return false;
}

function parseCommand() {
getTests()
.then((tests) => {
console.log(`Number of all tests in the #${runId} run: ${tests.length}\n`);
tests.forEach((test) => {
if (
selectedStatus.includes(test.status_id) &&
selectedTeams.includes(test.custom_dev_team)
) {
ids.push('C' + test.case_id);
}
});
})
.then(() => {
glob('cypress/e2e/**/*')
.then((res) => {
res.forEach((file) => {
if (file.includes('.cy.js')) {
arrayOfFiles.push(removeRootPath(file).replace(/\\/g, '/'));
}
});
})
.then(() => {
arrayOfFiles.forEach((file) => {
const text = fs.readFileSync(file, { encoding: 'utf8' });
const names = getTestNames(text);
names.tests.forEach((test) => {
if (test.type === 'test' && titleContainsId(test.name, ids)) {
filteredFiles.push(file);
}
});
});
filteredFiles.sort();
})
.then(() => {
console.log(`Number of filtered tests: ${filteredFiles.length}\n`);
const parsedCommand = `--spec "${filteredFiles.join(',')}"`;
console.log(parsedCommand);
});
});
}

parseCommand();
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@
"@interactors/html": "^1.0.0-rc1.4",
"@interactors/keyboard": "^1.0.0-rc1.5",
"axe-core": "4.3.3",
"axios": "^1.7.7",
"cypress": "12.0.0",
"cypress-cloud": "^1.9.6",
"cypress-testrail-simple": "^3.1.0",
"date-fns": "^2.16.1",
"debug": "^4.0.1",
"element-is-visible": "^1.0.0",
"find-test-names": "^1.28.28",
"glob": "^10.4.5",
"moment": "^2.29.3"
},
"devDependencies": {
Expand Down

0 comments on commit 175b9ec

Please sign in to comment.