Attach a plain text/log file to my cucumber test cases #2186
-
I run cucumber tests and my tests generate a plain text/log file for every test case. I would like to attach that file to the test case and see it in the serenity html report. I could not find the code snippet for it. Any help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @gitToSantosh, As of Serenity/JS 3.15, the process to add custom attachments is not yet as obvious as I would have liked, so I'm planning to improve it in future releases. Right now, you can use the Since you're interested in attaching text data, you'll need to use the import { Name, TextData } from '@serenity-js/core/lib/model'
actor.collect(
TextData.fromJSON({ contentType: 'text/plain', data: 'my text data' }),
new Name('optional name of the attachment'),
); Typically, you'd do this in a custom interaction. For example, you might want to have an interaction to import * as fs from 'node:fs'
import { Anserable, Interaction } from '@serenity-js/core'
import { Name, TextData } from '@serenity-js/core/lib/model'
import { Path } from '@serenity-js/core/lib/io'
export class Attach {
static contentsOf = (pathToFile: Path): Interaction =>
Interaction.where(`#actor attaches contents of ${ pathToFile.basename() }`, async actor => {
const data = fs.readFileSync(pathToFile.value).toString('utf-8');
actor.collect(
TextData.fromJSON({ contentType: 'text/plain', data }),
new Name(pathToFile.basename()),
);
});
} With a custom interaction like this in place, you can now make your actor attach the contents of a log file in a test scenario: import { Path } from '@serenity-js/core/lib/io'
actor.attemptsTo(
Attach.contentsOf(Path.from(__dirname, '/out/server.log')),
) |
Beta Was this translation helpful? Give feedback.
Hey @gitToSantosh,
As of Serenity/JS 3.15, the process to add custom attachments is not yet as obvious as I would have liked, so I'm planning to improve it in future releases.
Right now, you can use the
actor.collect(artifact, name)
API, and pass one of theArtifact
implementations as the argument. Note that while theactor.collect
is stable and won't change without a major release, artifact implementations are likely to get simplified over the coming months, so please watch this space.Since you're interested in attaching text data, you'll need to use the
actor.collect
mehod as follows: