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 <[email protected]>
  • Loading branch information
dkwon17 committed Sep 18, 2023
1 parent 8a0bacc commit d54abb7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
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
81 changes: 79 additions & 2 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 @@ -31,6 +31,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
10 changes: 10 additions & 0 deletions tests/e2e/utils/CheReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { OAUTH_CONSTANTS } from '../constants/OAUTH_CONSTANTS';
import { REPORTER_CONSTANTS } from '../constants/REPORTER_CONSTANTS';
import { PLUGIN_TEST_CONSTANTS } from '../constants/PLUGIN_TEST_CONSTANTS';
import { inject, injectable } from 'inversify';
const chromeHar = require('chrome-har')

@injectable()
class CheReporter extends mocha.reporters.Spec {
Expand Down Expand Up @@ -140,6 +141,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 @@ -178,6 +180,14 @@ class CheReporter extends mocha.reporters.Spec {
const browserLogsStream: WriteStream = fs.createWriteStream(browserLogsFileName);
browserLogsStream.write(Buffer.from(browserLogs));
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)));
networkLogsStream.end();
});
}
}
Expand Down

0 comments on commit d54abb7

Please sign in to comment.