Skip to content

Latest commit

 

History

History
92 lines (76 loc) · 2.44 KB

attachments.md

File metadata and controls

92 lines (76 loc) · 2.44 KB

Attachments

Text, images and files can be added to the output of the JSON formatter with attachments. The world constructor is passed an attach function, which the default world constructor assigns to this.attach.

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
  After(function () {
    this.attach('Some text');
  });
});

By default, text is saved with a MIME type of text/plain. You can also specify a different MIME type:

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
  After(function () {
    this.attach('{"name": "some JSON"}', 'application/json');
  });
});

Images and other binary data can be attached using a stream.Readable. The data will be base64 encoded in the output. You should wait for the stream to be read before continuing by providing a callback or waiting for the returned promise to resolve.

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
  // Passing a callback
  After(function (scenarioResult, callback) {
    if (scenarioResult.isFailed()) {
      var stream = getScreenshotOfError();
      this.attach(stream, 'image/png', callback);
    }
    else {
      callback();
    }
  });

  // Returning the promise
  After(function (scenarioResult) {
    if (scenarioResult.isFailed()) {
      var stream = getScreenshotOfError();
      return this.attach(stream, 'image/png');
    }
  });
});

Images and binary data can also be attached using a Buffer. The data will be base64 encoded in the output.

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
  After(function (scenarioResult) {
    if (scenarioResult.isFailed()) {
      var buffer = getScreenshotOfError();
      this.attach(buffer, 'image/png');
    }
  });
});

Here is an example of saving a screenshot using Selenium WebDriver when a scenario fails:

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({After}) {
    After(function (scenarioResult) {
    var world = this;
    if (scenarioResult.isFailed()) {
      return webDriver.takeScreenshot().then(function(screenShot) {
        // screenShot is a base-64 encoded PNG
        world.attach(screenShot, 'image/png');
      });
    }
  });
});