From 436cccfa4c982adb70e90f589ec25c8a79e5475a Mon Sep 17 00:00:00 2001 From: davidgoss Date: Mon, 25 Jan 2021 14:33:42 +0000 Subject: [PATCH 1/8] add new scenario --- features/attachments.feature | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/features/attachments.feature b/features/attachments.feature index 3d721f7de..f05dadc69 100644 --- a/features/attachments.feature +++ b/features/attachments.feature @@ -28,6 +28,20 @@ Feature: Attachments | DATA | MEDIA TYPE | MEDIA ENCODING | | iVBORw== | image/png | BASE64 | + Scenario: Attach a string that is already base64 encoded + Given a file named "features/support/hooks.js" with: + """ + const {Before} = require('@cucumber/cucumber') + + Before(function() { + this.attach(Buffer.from([137, 80, 78, 71]).toString('base64'), 'image/png') + }) + """ + When I run cucumber-js + Then scenario "some scenario" "Before" hook has the attachments: + | DATA | MEDIA TYPE | MEDIA ENCODING | + | iVBORw== | image/png | BASE64 | + Scenario: Attach a stream (callback) Given a file named "features/support/hooks.js" with: """ From f54f8997eabe61ca54c05b489cf1fe5fcab6e42c Mon Sep 17 00:00:00 2001 From: davidgoss Date: Mon, 25 Jan 2021 14:38:07 +0000 Subject: [PATCH 2/8] new notation --- features/attachments.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/attachments.feature b/features/attachments.feature index f05dadc69..86bf33ae5 100644 --- a/features/attachments.feature +++ b/features/attachments.feature @@ -34,7 +34,7 @@ Feature: Attachments const {Before} = require('@cucumber/cucumber') Before(function() { - this.attach(Buffer.from([137, 80, 78, 71]).toString('base64'), 'image/png') + this.attach(Buffer.from([137, 80, 78, 71]).toString('base64'), 'base64:image/png') }) """ When I run cucumber-js From 1b897747feb70cef493fe4d17a5d6b9878d41def Mon Sep 17 00:00:00 2001 From: davidgoss Date: Mon, 25 Jan 2021 14:51:24 +0000 Subject: [PATCH 3/8] implement --- src/runtime/attachment_manager/index.ts | 15 ++++++++--- src/runtime/attachment_manager/index_spec.ts | 28 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/runtime/attachment_manager/index.ts b/src/runtime/attachment_manager/index.ts index 4257569b1..8c4a4967c 100644 --- a/src/runtime/attachment_manager/index.ts +++ b/src/runtime/attachment_manager/index.ts @@ -51,10 +51,17 @@ export default class AttachmentManager { if (doesNotHaveValue(mediaType)) { mediaType = 'text/plain' } - this.createStringAttachment(data, { - encoding: messages.Attachment.ContentEncoding.IDENTITY, - contentType: mediaType, - }) + if (mediaType.startsWith('base64:')) { + this.createStringAttachment(data, { + encoding: messages.Attachment.ContentEncoding.BASE64, + contentType: mediaType.replace('base64:', ''), + }) + } else { + this.createStringAttachment(data, { + encoding: messages.Attachment.ContentEncoding.IDENTITY, + contentType: mediaType, + }) + } } else { throw Error( 'Invalid attachment data: must be a buffer, readable stream, or string' diff --git a/src/runtime/attachment_manager/index_spec.ts b/src/runtime/attachment_manager/index_spec.ts index d72c97583..482cd8f84 100644 --- a/src/runtime/attachment_manager/index_spec.ts +++ b/src/runtime/attachment_manager/index_spec.ts @@ -204,6 +204,34 @@ describe('AttachmentManager', () => { }) }) + describe('with media type, already base64 encoded', () => { + it('adds the data and media', function () { + // Arrange + const attachments: IAttachment[] = [] + const attachmentManager = new AttachmentManager((x) => + attachments.push(x) + ) + + // Act + const result = attachmentManager.create( + Buffer.from('my string', 'utf8').toString('base64'), + 'base64:text/special' + ) + + // Assert + expect(result).to.eql(undefined) + expect(attachments).to.eql([ + { + data: 'bXkgc3RyaW5n', + media: { + contentType: 'text/special', + encoding: messages.Attachment.ContentEncoding.BASE64, + }, + }, + ]) + }) + }) + describe('without mime type', () => { it('adds the data with the default mime type', function () { // Arrange From e7f681050621716f8c21f5158fe173198a410a77 Mon Sep 17 00:00:00 2001 From: davidgoss Date: Mon, 25 Jan 2021 14:52:55 +0000 Subject: [PATCH 4/8] documentation --- docs/support_files/attachments.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/support_files/attachments.md b/docs/support_files/attachments.md index b428d3435..5db571ffe 100644 --- a/docs/support_files/attachments.md +++ b/docs/support_files/attachments.md @@ -65,6 +65,19 @@ After(function (testCase) { }); ``` +If you've already got a base64-encoded string, you can prefix your mime type with `base64:` to indicate this: + +```javascript +var {After, Status} = require('@cucumber/cucumber'); + +After(function (testCase) { + if (testCase.result.status === Status.FAILED) { + var base64String = getScreenshotOfError(); + this.attach(base64String, 'base64:image/png'); + } +}); +``` + Here is an example of saving a screenshot using [Selenium WebDriver](https://www.npmjs.com/package/selenium-webdriver) when a scenario fails: @@ -76,7 +89,7 @@ After(function (testCase) { if (testCase.result.status === Status.FAILED) { return webDriver.takeScreenshot().then(function(screenShot) { // screenShot is a base-64 encoded PNG - world.attach(screenShot, 'image/png'); + world.attach(screenShot, 'base64:image/png'); }); } }); From 283c3c14283b97bb252124a55249dcba0aba1d65 Mon Sep 17 00:00:00 2001 From: davidgoss Date: Mon, 25 Jan 2021 14:54:05 +0000 Subject: [PATCH 5/8] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2595fc66c..e6c0db1b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO ### Added +- Support attachments that are already base64-encoded + ### Changed ### Deprecated From 37b04047e93f94b9f10579b1ba8ad074953c2102 Mon Sep 17 00:00:00 2001 From: David Goss Date: Tue, 2 Feb 2021 08:10:29 +0000 Subject: [PATCH 6/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c0db1b4..60bfac63e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO ### Added -- Support attachments that are already base64-encoded +- Support attachments that are already base64-encoded via a prefix on the MIME type e.g. `this.attach(base64String, 'base64:image/png')` ([https://github.com/cucumber/cucumber-js/pull/1552](#1552)) ### Changed From 0bd106345ed85b8cbf20100691d0d6ada8a38511 Mon Sep 17 00:00:00 2001 From: David Goss Date: Tue, 2 Feb 2021 08:11:54 +0000 Subject: [PATCH 7/8] remove unnecessary comment --- docs/support_files/attachments.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/support_files/attachments.md b/docs/support_files/attachments.md index 5db571ffe..b4d1afa93 100644 --- a/docs/support_files/attachments.md +++ b/docs/support_files/attachments.md @@ -88,7 +88,6 @@ After(function (testCase) { var world = this; if (testCase.result.status === Status.FAILED) { return webDriver.takeScreenshot().then(function(screenShot) { - // screenShot is a base-64 encoded PNG world.attach(screenShot, 'base64:image/png'); }); } From 0ca6aa8a0eed0f449b2a75f7f7218ed2cdf51821 Mon Sep 17 00:00:00 2001 From: David Goss Date: Tue, 2 Feb 2021 08:13:27 +0000 Subject: [PATCH 8/8] i am bad at markdown --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60bfac63e..c440389a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO ### Added -- Support attachments that are already base64-encoded via a prefix on the MIME type e.g. `this.attach(base64String, 'base64:image/png')` ([https://github.com/cucumber/cucumber-js/pull/1552](#1552)) +- Support attachments that are already base64-encoded via a prefix on the MIME type e.g. `this.attach(base64String, 'base64:image/png')` ([#1552](https://github.com/cucumber/cucumber-js/pull/1552)) ### Changed