playwright-bdd
Version:
BDD Testing with Playwright runner
99 lines • 4.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestStepAttachments = void 0;
const messages = __importStar(require("@cucumber/messages"));
const timing_1 = require("./timing");
class TestStepAttachments {
constructor(testCaseRun, testStep, pwStep) {
this.testCaseRun = testCaseRun;
this.testStep = testStep;
this.pwStep = pwStep;
}
buildMessages() {
if (!this.pwStep)
return [];
return this.testCaseRun.attachmentMapper
.getStepAttachments(this.pwStep)
.map((pwAttachment) => this.buildAttachmentMessage(pwAttachment));
}
buildAttachmentMessage(pwAttachment) {
const attachment = {
testCaseStartedId: this.testCaseRun.id,
testStepId: this.testStep.id,
mediaType: pwAttachment.contentType,
fileName: pwAttachment.name,
body: '',
contentEncoding: messages.AttachmentContentEncoding.IDENTITY,
// Use step start time as attachment timestamp (per-attachment timing is not available in Playwright).
timestamp: (0, timing_1.toCucumberTimestamp)(this.pwStep.startTime.getTime()),
// Note: 'testRunHookStartedId' (added in @cucumber/messages v28) could be used to
// associate attachments from global hooks (BeforeAll/AfterAll) with TestRunHookStarted.
// We don't emit TestRunHookStarted/Finished messages yet, so this field is not populated.
};
if (pwAttachment.path) {
// For in-memory messages store absolute path in 'url' field.
// In reporters we will replace it with relative path
// or read attachment content and delete 'url' field.
attachment.url = pwAttachment.path;
}
else if (pwAttachment.body) {
// Use IDENTITY (raw UTF-8) for text types so the Cucumber HTML report can display
// non-ASCII characters correctly. atob() in the browser decodes base64 as Latin-1,
// so base64-encoding a UTF-8 string produces garbled output for multi-byte characters.
// See: https://github.com/vitalets/playwright-bdd/issues/379
if (isTextContentType(pwAttachment.contentType)) {
attachment.body = pwAttachment.body.toString('utf8');
attachment.contentEncoding = messages.AttachmentContentEncoding.IDENTITY;
}
else {
attachment.body = pwAttachment.body.toString('base64');
attachment.contentEncoding = messages.AttachmentContentEncoding.BASE64;
}
}
else {
throw new Error(`Playwright attachment without path and body`);
}
return { attachment };
}
}
exports.TestStepAttachments = TestStepAttachments;
// Same pattern as isTextAttachment() in external.ts.
// Text content types use IDENTITY encoding so non-ASCII characters are preserved
// (base64 + browser atob() interprets bytes as Latin-1, not UTF-8).
function isTextContentType(contentType) {
return /^(text\/|application\/json)/.test(contentType);
}
//# sourceMappingURL=TestStepAttachments.js.map