Skip to content

Commit 1744fef

Browse files
authored
changes to fix server issues (inluxc#15)
1 parent 7264daf commit 1744fef

9 files changed

+171
-114
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ dist
105105

106106
test-results/
107107
lib/
108+
109+
# IDE
110+
.vscode/

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+45-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { XrayTestResult, XrayTestSteps, XrayTestEvidence, XrayTest } from '../types/cloud.types';
2+
import { XrayCloudStatus } from '../types/cloud.types';
3+
import { XrayServerStatus } from '../types/server.types';
24
import type { XrayOptions } from '../types/xray.types';
35
import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
46
import * as fs from 'fs';
@@ -22,8 +24,8 @@ class XrayReporter implements Reporter {
2224
const testResults: XrayTestResult = {
2325
info: {
2426
summary: this.defaultRunName,
25-
startDate: new Date().toISOString(),
26-
finishDate: new Date().toISOString(),
27+
startDate: this.getFormatData(new Date()),
28+
finishDate: this.getFormatData(new Date()),
2729
testPlanKey: this.options.testPlan,
2830
revision: '2536',
2931
},
@@ -43,9 +45,9 @@ class XrayReporter implements Reporter {
4345

4446
let xrayTestData: XrayTest = {
4547
testKey: testCode,
46-
status: result.status.toUpperCase(),
47-
start: result.startTime.toISOString(),
48-
finish: finishTime.toISOString(),
48+
status: this.convertPwStatusToXray(result.status),
49+
start: this.getFormatData(result.startTime),
50+
finish: this.getFormatData(finishTime),
4951
steps: [] as XrayTestSteps[],
5052
};
5153

@@ -63,7 +65,7 @@ class XrayReporter implements Reporter {
6365
}
6466

6567
const xrayTestStep: XrayTestSteps = {
66-
status: typeof step.error == 'object' ? 'FAILED' : 'PASSED',
68+
status: typeof step.error == 'object' ? this.convertPwStatusToXray('failed') : this.convertPwStatusToXray('passed'),
6769
comment: typeof step.error == 'object' ? errorMessage : '',
6870
actualResult: dataReceived,
6971
};
@@ -95,13 +97,49 @@ class XrayReporter implements Reporter {
9597
async onEnd() {
9698
// Update test Duration
9799
this.testResults?.info?.finishDate !=
98-
new Date(new Date(this.testResults?.info?.startDate!).getTime() + this.totalDuration).toISOString();
100+
this.getFormatData(new Date(new Date(this.testResults?.info?.startDate!).getTime() + this.totalDuration));
99101
if (typeof this.testResults != 'undefined' && typeof this.testResults.tests != 'undefined' && this.testResults.tests.length > 0) {
100102
await this.xrayService.createRun(this.testResults);
101103
} else {
102104
console.log(`There are no tests with such ${this.testCaseKeyPattern} key pattern`);
103105
}
104106
}
107+
108+
convertPwStatusToXray(status: string): string {
109+
switch (this.options.jira.type) {
110+
case 'cloud':
111+
return XrayCloudStatus[status];
112+
case 'server':
113+
return XrayServerStatus[status];
114+
default:
115+
return '';
116+
}
117+
}
118+
119+
getFormatData(date: Date) {
120+
if (this.options.jira.type === 'cloud') {
121+
return date.toISOString();
122+
} else {
123+
let timezone = date.getTimezoneOffset().toString();
124+
return (
125+
date.getFullYear() +
126+
'-' +
127+
date.getMonth() +
128+
'-' +
129+
date.getDay() +
130+
'T' +
131+
date.getHours() +
132+
':' +
133+
date.getMinutes() +
134+
':' +
135+
date.getSeconds() +
136+
'+' +
137+
(timezone.length == 1 ? '0'+timezone : timezone) +
138+
':00'
139+
);
140+
}
141+
142+
}
105143
}
106144

107145
export default XrayReporter;

src/xray.service.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ export class XrayService {
1414
private readonly jira: string;
1515
private readonly username: string;
1616
private readonly password: string;
17+
private readonly token: string;
1718
private readonly type: string;
1819
private readonly requestUrl: string;
19-
private readonly debug: boolean;
20+
private readonly options: XrayOptions;
2021
private axios: Axios;
2122

2223
constructor(options: XrayOptions) {
2324
// Init vars
2425
this.xray = '';
2526
this.username = '';
2627
this.password = '';
28+
this.token = '';
2729
this.requestUrl = '';
28-
this.debug = options.debug;
30+
this.options = options;
2931

3032
// Set Jira URL
3133
if (!options.jira.url) throw new Error('"jira.url" option is missed. Please, provide it in the config');
@@ -75,26 +77,23 @@ export class XrayService {
7577

7678
case 'server':
7779
// Set Xray Server URL
78-
if (!options.server?.url) throw new Error('"host" option is missed. Please, provide it in the config');
79-
this.xray = options.server?.url;
80+
if (!options.jira?.url) throw new Error('"host" option is missed. Please, provide it in the config');
81+
this.xray = options.jira?.url;
8082

8183
// Set Xray Credencials
82-
if (!options.server?.username || !options.server?.password)
83-
throw new Error('"server.username" and/or "server.password" options are missed. Please provide them in the config');
84-
this.username = options.server?.username;
85-
this.password = options.server?.password;
84+
if (!options.server?.token)
85+
throw new Error('"server.token" option is missing. Please provide them in the config');
86+
this.token = options.server?.token;
8687

8788
// Set Request URL
88-
this.requestUrl = this.xray + 'rest/raven/2.0/api';
89+
this.requestUrl = this.xray + 'rest/raven/1.0';
8990

9091
//Create Axios Instance with Auth
91-
const token = `${this.username}:${this.password}`;
92-
const encodedToken = Buffer.from(token).toString('base64');
9392
this.axios = axios.create({
9493
baseURL: this.xray,
9594
headers: {
9695
'Content-Type': 'application/json',
97-
Authorization: `Basic ${encodedToken}`,
96+
Authorization: `Bearer ${this.token}`,
9897
},
9998
});
10099

@@ -116,10 +115,10 @@ export class XrayService {
116115

117116
results.tests!.forEach((test: { status: any }) => {
118117
switch (test.status) {
119-
case 'PASSED':
118+
case 'PASSED', 'PASS':
120119
passed = passed + 1;
121120
break;
122-
case 'FAILED':
121+
case 'FAILED', 'FAIL':
123122
failed = failed + 1;
124123
break;
125124
}
@@ -128,14 +127,15 @@ export class XrayService {
128127
try {
129128
const response = await this.axios.post(URL, JSON.stringify(results));
130129
if (response.status !== 200) throw new Error(`${response.status} - Failed to create test cycle`);
131-
const {
132-
data: { key },
133-
} = response;
130+
let key = response.data.key;
131+
if (this.options.jira.type === 'server') {
132+
key = response.data.testExecIssue.key;
133+
}
134134

135135
// Results
136136
console.log(`${bold(blue(`-------------------------------------`))}`);
137137
console.log(`${bold(blue(` `))}`);
138-
console.log(`${bold(blue(`✅ Test status: ${key}`))}`);
138+
console.log(`${bold(blue(`✅ Test plan: ${this.options.testPlan}`))}`);
139139
console.log(`${bold(blue(`✅ Tests ran: ${total}`))}`);
140140
console.log(`${bold(green(`✅ Tests passed: ${passed}`))}`);
141141
console.log(`${bold(red(`✅ Tests failed: ${failed}`))}`);
@@ -144,11 +144,11 @@ export class XrayService {
144144
console.log(`${bold(blue(` `))}`);
145145
console.log(`${bold(blue(`✅ Test cycle ${key} has been created`))}`);
146146
console.log(`${bold(blue('👇 Check out the test result'))}`);
147-
console.log(`${bold(blue(`🔗 ${this.jira}/browse/${key}`))}`);
147+
console.log(`${bold(blue(`🔗 ${this.jira}browse/${key}`))}`);
148148
console.log(`${bold(blue(` `))}`);
149149
console.log(`${bold(blue(`-------------------------------------`))}`);
150150

151-
if (this.debug) {
151+
if (this.options.debug) {
152152
fs.writeFile('xray-payload.json', JSON.stringify(results), (err) => {
153153
if (err) throw err;
154154
});

tests/another-simple.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect } from '@playwright/test';
22

3-
test('[DS-1790] another test', async ({ page, browserName }) => {
3+
test('[XRAYISSUE-1] another test', async ({ page, browserName }) => {
44
test.skip(browserName === 'webkit');
55

66
await page.goto('https://playwright.dev/');
@@ -12,7 +12,7 @@ test('[DS-1790] another test', async ({ page, browserName }) => {
1212
await expect(title).toHaveText('Playwright');
1313
});
1414

15-
test('[DS-1837] another test', async ({ page, browserName }) => {
15+
test('[XRAYISSUE-2] another test', async ({ page, browserName }) => {
1616
test.skip(browserName === 'webkit');
1717

1818
await page.goto('https://playwright.dev/');

tests/simple.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect } from '@playwright/test';
22

3-
test('[C70] basic test', async ({ page }) => {
3+
test('[XRAYISSUE-1] basic test', async ({ page }) => {
44
await page.goto('https://playwright.dev/');
55
const title = page.locator('.navbar__inner .navbar__title');
66
await expect(title).toHaveText('Playwright');

types/cloud.types.ts

+38-30
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
1-
export type XrayStatus = 'passed' | 'failed' | 'timedOut' | 'skipped' | 'interrupted';
1+
type XrayCloudStatusType = { [key: string]: string };
2+
3+
export const XrayCloudStatus: XrayCloudStatusType = {
4+
passed: 'PASSED',
5+
failed: 'FAILED',
6+
skipped: 'SKIPPED',
7+
timedOut: 'TIMEDOUT',
8+
interrupted: 'INTERRUPTED',
9+
};
210

311
export interface XrayTestResult {
412
testExecutionKey?: string;
513
info: XrayInfo;
614
tests?: XrayTest[];
7-
};
15+
}
816

917
export interface XrayInfo {
10-
summary: string,
11-
description?: string,
12-
version?: string,
13-
user?: string,
14-
revision?: string,
15-
startDate: string,
16-
finishDate: string,
17-
testPlanKey: string,
18-
testEnvironments?: object,
19-
};
18+
summary: string;
19+
description?: string;
20+
version?: string;
21+
user?: string;
22+
revision?: string;
23+
startDate: string;
24+
finishDate: string;
25+
testPlanKey: string;
26+
testEnvironments?: object;
27+
}
2028

2129
export interface XrayTest {
22-
testKey: string,
23-
start: string,
24-
finish: string,
25-
actualResult?: string,
26-
status: string,
27-
evidence?: XrayTestEvidence[],
28-
steps?: XrayTestSteps[],
29-
defects?: object,
30-
};
30+
testKey: string;
31+
start: string;
32+
finish: string;
33+
actualResult?: string;
34+
status: string;
35+
evidence?: XrayTestEvidence[];
36+
steps?: XrayTestSteps[];
37+
defects?: object;
38+
}
3139

3240
export interface XrayTestSteps {
33-
status: string,
34-
comment?: string,
35-
actualResult?: string,
36-
evidences?: XrayTestEvidence[]
37-
};
41+
status: string;
42+
comment?: string;
43+
actualResult?: string;
44+
evidences?: XrayTestEvidence[];
45+
}
3846

3947
export interface XrayTestEvidence {
40-
data: string,
41-
filename: string,
42-
contentType: string
43-
};
48+
data: string;
49+
filename: string;
50+
contentType: string;
51+
}

0 commit comments

Comments
 (0)