Skip to content

Commit

Permalink
feat: collect har file on test fail
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 committed Sep 29, 2023
1 parent bd36cef commit 530113a
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 0 deletions.
201 changes: 201 additions & 0 deletions tests/e2e/CheReporter.js

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

1 change: 1 addition & 0 deletions tests/e2e/driver/ChromeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ChromeDriver implements IDriver {

constructor() {
const options: Options = this.getDriverOptions();
options.setLoggingPrefs({performance: 'ALL'})
if (CHROME_DRIVER_CONSTANTS.TS_USE_WEB_DRIVER_FOR_TEST) {
this.driver = this.getDriverBuilder(options).build();
}
Expand Down
77 changes: 77 additions & 0 deletions tests/e2e/package-lock.json

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

1 change: 1 addition & 0 deletions tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@typescript-eslint/parser": "^6.1.0",
"axios": "^0.25.0",
"chai": "^4.3.4",
"chrome-har": "^0.13.2",
"chromedriver": "^114.0.2",
"clone-deep": "^4.0.1",
"eslint": "^8.45.0",
Expand Down

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions tests/e2e/utils/CheReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { PLUGIN_TEST_CONSTANTS } from '../constants/PLUGIN_TEST_CONSTANTS';
import { injectable } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import { e2eContainer } from '../configs/inversify.config';
// @ts-ignore: No module declaration file
import * as chromeHar from 'chrome-har';

const { lazyInject } = getDecorators(e2eContainer);

Expand Down Expand Up @@ -135,6 +137,7 @@ class CheReporter extends mocha.reporters.Spec {
const screenshotFileName: string = `${testReportDirPath}/screenshot-${testTitle}.png`;
const pageSourceFileName: string = `${testReportDirPath}/pagesource-${testTitle}.html`;
const browserLogsFileName: string = `${testReportDirPath}/browserlogs-${testTitle}.txt`;
const harFileName: string = `${testReportDirPath}/har-${testTitle}.har`;

// create reporter dir if not exist
const reportDirExists: boolean = fs.existsSync(REPORTER_CONSTANTS.TS_SELENIUM_REPORT_FOLDER);
Expand Down Expand Up @@ -172,6 +175,15 @@ class CheReporter extends mocha.reporters.Spec {
browserLogsStream.write(Buffer.from(browserLogs), (): void => {
browserLogsStream.end();
});

// take networking logs and write to file
const networkLogsEntries: logging.Entry[] = await this.driverHelper.getDriver().manage().logs().get('performance');
const events = networkLogsEntries.map(entry => JSON.parse(entry.message).message)
const har = chromeHar.harFromMessages(events, {includeTextFromResponseBody: true});
const networkLogsStream: WriteStream = fs.createWriteStream(harFileName);
networkLogsStream.write(Buffer.from(JSON.stringify(har)), (): void => {
networkLogsStream.end();
});
});
}
}
Expand Down

0 comments on commit 530113a

Please sign in to comment.