Skip to content

Commit

Permalink
feat: support pagination on testrail API (when there are more than 25…
Browse files Browse the repository at this point in the history
…0 results) (#41)

* chore: testrail-js-api up to 0.3.2

* feat: support navigation on next set of results

* refactor: helper functions to navigate on testrailAPI (getAllTests & getAllCases)

* style: resolved warnings (npm run lint)

Co-authored-by: Jérémy Torralba <[email protected]>
  • Loading branch information
trooperjey01 and jeremyagatha authored Oct 5, 2022
1 parent acb89a2 commit 07f257d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
49 changes: 38 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"homepage": "https://github.com/DamianOsipiuk/testcafe-reporter-testrail#readme",
"dependencies": {
"moment": "^2.29.4",
"testrail-js-api": "^0.3.0"
"testrail-js-api": "^0.3.2"
},
"devDependencies": {
"@types/jest": "^29.1.0",
Expand Down
23 changes: 8 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { prepareConfig, verifyConfig } from "./config";
import { uploadScreenshots } from "./utils/upload-screenshots";
import { uploadVideos } from "./utils/upload-videos";
import { closeOldRuns } from "./utils/close-runs";
import { getAllCases, getAllTests } from "./utils/testrail-getResults";

import {
prepareReference,
prepareReportName,
Expand Down Expand Up @@ -69,11 +71,8 @@ const prepareRun = async (
);
}
} else if (existingRun) {
const { value: testsResult } = await throwOnApiError(
testrailAPI.getTests(existingRun.id)
);
const currentCaseIds =
testsResult?.tests?.map((test) => test.case_id) || [];
const tests = await getAllTests(testrailAPI, config);
const currentCaseIds = tests?.map((test) => test.case_id) || [];
const additionalDescription = "\n" + runDescription;
const newDescription = existingRun.description
? existingRun.description.replace(additionalDescription, "") +
Expand Down Expand Up @@ -219,7 +218,7 @@ class TestcafeTestrailReporter {
_warnings: string[],
_result: TaskResult
) => {
const { host, user, apiKey, projectId, suiteId } = this.config;
const { host, user, apiKey } = this.config;

if (verifyConfig(this.config)) {
try {
Expand All @@ -234,11 +233,8 @@ class TestcafeTestrailReporter {
const caseIdList = this.results.map((result) => result.case_id);

const testrailAPI = new TestRail(host, user, apiKey);
const { value: caseListResult } = await throwOnApiError(
testrailAPI.getCases(projectId, { suite_id: suiteId })
);
const existingCaseIds = caseListResult?.cases.map((item) => item.id);

const cases = await getAllCases(testrailAPI, this.config);
const existingCaseIds = cases.map((item) => item.id);
caseIdList.forEach((id) => {
if (!existingCaseIds.includes(id)) {
console.error(
Expand Down Expand Up @@ -275,10 +271,7 @@ class TestcafeTestrailReporter {
const { value: results } = await throwOnApiError(
testrailAPI.addResultsForCases(runId, resultsToPush)
);
const { value: testsResult } = await throwOnApiError(
testrailAPI.getTests(runId)
);
const tests = testsResult.tests || [];
const tests = await getAllTests(testrailAPI, this.config);

if (this.config.uploadScreenshots) {
await uploadScreenshots({
Expand Down
38 changes: 38 additions & 0 deletions src/utils/testrail-getResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { TestRail } from "testrail-js-api";
import type { Config } from "../types";
import { throwOnApiError } from "./misc";

export const getAllCases = async (testrailAPI: TestRail, config: Config) => {
let { value: caseListResult } = await throwOnApiError(
testrailAPI.getCases(config.projectId, { suite_id: config.suiteId })
);
let cases = caseListResult?.cases || [];
let offsetVal = 0;
while (caseListResult._links.next !== null) {
offsetVal += 250;
({ value: caseListResult } = await throwOnApiError(
testrailAPI.getCases(config.projectId, {
suite_id: config.suiteId,
offset: offsetVal,
})
));
cases = cases.concat(caseListResult?.cases);
}
return cases;
};

export const getAllTests = async (testrailAPI: TestRail, config: Config) => {
let { value: testsResult } = await throwOnApiError(
testrailAPI.getTests(config.runId)
);
let tests = testsResult.tests || [];
let offsetVal = 0;
while (testsResult._links.next !== null) {
offsetVal += 250;
({ value: testsResult } = await throwOnApiError(
testrailAPI.getTests(config.runId, { offset: offsetVal })
));
tests = tests.concat(testsResult.tests);
}
return tests;
};

0 comments on commit 07f257d

Please sign in to comment.