diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/reporter/mptReporter.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/reporter/mptReporter.ts index 2ca7bc3cb8b3..18f5c33bb019 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/reporter/mptReporter.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/reporter/mptReporter.ts @@ -271,11 +271,16 @@ class MPTReporter implements Reporter { this.testResultBatch.add(testResultObject); // Store test attachments in array const testAttachments: string[] = []; + const otherAttachments: any[] = []; for (const attachment of result.attachments) { if (attachment.path !== undefined && attachment.path !== "") { testAttachments.push(attachment.path); this.uploadMetadata.numTotalAttachments++; this.uploadMetadata.sizeTotalAttachments += ReporterUtils.getFileSize(attachment.path); + } else if (attachment.body instanceof Buffer) { + otherAttachments.push(attachment); + this.uploadMetadata.numTotalAttachments++; + this.uploadMetadata.sizeTotalAttachments += ReporterUtils.getBufferSize(attachment.body); } } @@ -283,7 +288,11 @@ class MPTReporter implements Reporter { const rawTestResult: RawTestResult = this.reporterUtils.getRawTestResultObject(result); this.testRawResults.set(testResultObject.testExecutionId, JSON.stringify(rawTestResult)); this._testEndPromises.push( - this._uploadTestResultAttachments(testResultObject.testExecutionId, testAttachments), + this._uploadTestResultAttachments( + testResultObject.testExecutionId, + testAttachments, + otherAttachments, + ), ); } catch (err: any) { this._addError(`Name: ${err.name}, Message: ${err.message}, Stack: ${err.stack}`); @@ -309,10 +318,21 @@ class MPTReporter implements Reporter { reporterLogger.error(`\nError in uploading test run information: ${err.message}`); } } - + private renewSasUriIfNeeded = async (): Promise => { + if ( + this.sasUri === undefined || + !ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri) + ) { + this.sasUri = await this.serviceClient.createStorageUri(); + reporterLogger.info( + `\nFetched SAS URI with validity: ${this.sasUri.expiresAt} and access: ${this.sasUri.accessLevel}.`, + ); + } + }; private async _uploadTestResultAttachments( testExecutionId: string, testAttachments: string[], + otherAttachments: any[], ): Promise { try { this.isTestRunStartSuccess = await this.promiseOnBegin; @@ -320,30 +340,26 @@ class MPTReporter implements Reporter { this._addError(`\nUnable to initialize test run report.`); return; } + for (const attachmentPath of testAttachments) { - const fileRelativePath = `${testExecutionId}/${ReporterUtils.getFileRelativePath( - attachmentPath, - )}`; - if ( - this.sasUri === undefined || - !ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri) - ) { - // Renew the sas uri - this.sasUri = await this.serviceClient.createStorageUri(); - reporterLogger.info( - `\nFetched SAS URI with validity: ${this.sasUri.expiresAt} and access: ${this.sasUri.accessLevel}.`, - ); - } + const fileRelativePath = `${testExecutionId}/${ReporterUtils.getFileRelativePath(attachmentPath)}`; + await this.renewSasUriIfNeeded(); await this.storageClient.uploadFile(this.sasUri.uri, attachmentPath, fileRelativePath); } - const rawTestResult = this.testRawResults.get(testExecutionId); - if ( - this.sasUri === undefined || - !ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri) - ) { - // Renew the sas uri - this.sasUri = await this.serviceClient.createStorageUri(); + + for (const otherAttachment of otherAttachments) { + await this.renewSasUriIfNeeded(); + const match = otherAttachment?.contentType?.match(/charset=(.*)/); + const charset = match && match.length > 1 ? match[1] : "utf-8"; + await this.storageClient.uploadBuffer( + this.sasUri.uri, + otherAttachment.body.toString((charset as any) || "utf-8"), + `${testExecutionId}/${otherAttachment.name}`, + ); } + + const rawTestResult = this.testRawResults.get(testExecutionId); + await this.renewSasUriIfNeeded(); await this.storageClient.uploadBuffer( this.sasUri.uri, rawTestResult[0]!, diff --git a/sdk/playwrighttesting/microsoft-playwright-testing/src/utils/reporterUtils.ts b/sdk/playwrighttesting/microsoft-playwright-testing/src/utils/reporterUtils.ts index a4930ddbd4af..c3f1883fee54 100644 --- a/sdk/playwrighttesting/microsoft-playwright-testing/src/utils/reporterUtils.ts +++ b/sdk/playwrighttesting/microsoft-playwright-testing/src/utils/reporterUtils.ts @@ -349,6 +349,16 @@ class ReporterUtils { return 0; } } + + public static getBufferSize(attachmentBody: Buffer): number { + try { + const fileSizeInBytes = attachmentBody.length; + return fileSizeInBytes; + } catch (error) { + return 0; + } + } + public redactAccessToken(info: string | undefined): string { if (!info || ReporterUtils.isNullOrEmpty(this.envVariables.accessToken)) { return ""; @@ -468,6 +478,11 @@ class ReporterUtils { attachmentStatus += ","; } attachmentStatus += "trace"; + } else if (attachment.contentType === "text/plain") { + if (attachmentStatus !== "") { + attachmentStatus += ","; + } + attachmentStatus += "txt"; } } return attachmentStatus;