Skip to content

Commit

Permalink
Report log live (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
danitseitlin authored Mar 15, 2021
1 parent c4f1f18 commit c9a76a5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
68 changes: 36 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,43 @@ exports['default'] = () => {
.newline();
},
async reportTestStart (name /*, meta */) {
process.logs = [];
console.log = function (d) {
process.logs.push({ type: 'info', log: d, time: new Date().valueOf() });
process.stdout.write(d + '\n');
console.log = d => {
(async() => this.captureLogs(this.client.test.id, 'info', d, new Date().valueOf()))().then(d => {
process.stdout.write(d + '\n');
})
};
console.error = function (d) {
process.logs.push({ type: 'error', log: d, time: new Date().valueOf() });
process.stdout.write(d + '\n');
(async() => this.captureLogs(this.client.test.id, 'error', d, new Date().valueOf()))().then(d => {
process.stdout.write(d + '\n');
})
};
console.warning = function (d) {
process.logs.push({ type: 'warning', log: d, time: new Date().valueOf() });
process.stdout.write(d + '\n');
(async() => this.captureLogs(this.client.test.id, 'warning', d, new Date().valueOf()))().then(d => {
process.stdout.write(d + '\n');
})
};
console.debug = function (d) {
process.logs.push({ type: 'debug', log: d, time: new Date().valueOf() });
process.stdout.write(d + '\n');
(async() => this.captureLogs(this.client.test.id, 'debug', d, new Date().valueOf()))().then(d => {
process.stdout.write(d + '\n');
})
};
process.logs.push({ type: 'debug', log: `Starting test ${name}...`, time: new Date().valueOf() });
await this.client.startTest(name);
await this.captureLogs(this.client.test.id, 'debug', `Starting test ${name}...`, new Date().valueOf())
},
async captureLogs(testId, level, message, time, attachment) {
try {
if(message !== undefined) {
const isJSON = this.client.client.isJSON(message) || Array.isArray(message);
if(isJSON && JSON.parse(message).errMsg !== undefined) message = JSON.parse(message).errMsg;
else if(isJSON) message = JSON.parse(message)
message = this.client.client.isJSON(message) ? JSON.stringify(message): message
}
await this.client.sendTestLogs(testId, level, message, time, attachment);
return message
}
catch (error) {
this.client.client.handleError(error);
}
},
async reportTestDone (name, testRunInfo) {
const errors = testRunInfo.errs;
Expand Down Expand Up @@ -92,33 +110,19 @@ exports['default'] = () => {
this.newline().write(title);

if (hasErrors)
this._renderErrors(testRunInfo.errs);
await this._renderErrors(testRunInfo.errs);

const result = testRunInfo.skipped ? 'skipped' : hasErrors ? 'failed' : 'passed';

this.afterErrorList = hasErrors;

this.newline();
if (testRunInfo.screenshots) {
testRunInfo.screenshots.forEach((screenshot, idx) => {
process.logs.push({ type: 'debug', log: `Taking screenshot (${name}-${idx}.png)`, file: { name: `${name}-${idx}.png`, path: screenshot.screenshotPath }, time: new Date().valueOf() });
testRunInfo.screenshots.forEach(async (screenshot, idx) => {
await this.captureLogs(this.client.test.id, 'debug', `Taking screenshot (${name}-${idx}.png)`, new Date().valueOf(), { name: `${name}-${idx}.png`, path: screenshot.screenshotPath })
});
}
process.logs.push({ type: 'debug', log: `Test ${name} has ended...`, time: new Date().valueOf() });
process.logs.forEach(async (item) => {
try {
if(item.log !== undefined) {
const isJSON = this.client.client.isJSON(item.log) || Array.isArray(item.log);
if(isJSON && JSON.parse(item.log).errMsg !== undefined) item.log = JSON.parse(item.log).errMsg;
else if(isJSON) item.log = JSON.parse(item.log)
item.log = this.client.client.isJSON(item.log) ? JSON.stringify(item.log): item.log
}
await this.client.sendTestLogs(this.client.test.id, item.type, item.log, item.time, item.file);
}
catch (error) {
this.client.client.handleError(error);
}
});
await this.captureLogs(this.client.test.id, 'debug', `Test ${name} has ended...`, new Date().valueOf())
await this.client.finishTest(this.client.test.id, result);
},

Expand Down Expand Up @@ -146,12 +150,12 @@ exports['default'] = () => {
this._renderWarnings(warnings);
await this.client.finishLaunch();
},
_renderErrors (errs) {
async _renderErrors (errs) {
this.setIndent(3)
.newline();

errs.forEach((err, idx) => {
process.logs.push({ type: 'error', log: JSON.stringify(err), time: new Date().valueOf() });
await errs.forEach(async (err, idx) => {
await this.captureLogs(this.client.test.id, 'error', JSON.stringify(err), new Date().valueOf())
var prefix = this.chalk.red(`${idx + 1}) `);

this.newline()
Expand Down
8 changes: 6 additions & 2 deletions tests/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export const mock: Route[] = [{
},{
path: '/api/v1/tmp/log',
method: 'post',
response: {
id: 134
response: (req: Request) => {
process.stdout.write(`[Server]${JSON.stringify(req.body)} \n`)
process.stdout.write(`[Server][${(req.body as any).level}] log sent: ${(req.body as any).message} \n`)
return {
id: 134
}
}
}]
1 change: 1 addition & 0 deletions tests/test.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Performing E2E testing', async function() {
const failedCount = await runner
.src(['tests/test.testcafe.ts'])
.browsers(['firefox:headless'])
.reporter('reportportal-plugin')
.run();

console.log('Tests failed: ' + failedCount);
Expand Down
21 changes: 11 additions & 10 deletions tests/test.testcafe.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { t } from 'testcafe';

fixture `sss`
fixture `First fixture`
.page('https://google.com')

test('fff', async () => {
console.log('xxx');
test('Taking screenshot', async () => {
console.log('About to take a screenshot');
await t.takeScreenshot()
console.log('The screenshot was succesfully taken!');
})
test('fff2', async () => {
console.log('xxx');
test('Negative testing, verifying Error display', async () => {
console.log('About to fail..');
await t.expect('X').eql('Y', 'OMG')
})

fixture `sss2`
fixture `Second fixture`

test.skip('fff5', async () => {
console.log('xxx');
test.skip('Skipping the test', async () => {
console.log('The test is skipped. This log shoud not be appearing.');
})
test('fff32', async () => {
console.log('xxx');
test('Basic print', async () => {
console.log('Printing the test contents');
})

0 comments on commit c9a76a5

Please sign in to comment.