Skip to main content

externalCollectsArtifacts

Describes an Actor who can collect artifacts, such as photos or JSON data., while the scenario is being executed

Learn more

Implemented by

Index

Methods

Methods

externalcollect

  • collect(artifact: Artifact, name?: Name): void
  • Makes the Actor collect an artifact so that it can be included in the test report.

    Implementing a custom interaction to attach artifacts

    import * as fs from 'node:fs'
    import { Answerable, Interaction, the } from '@serenity-js/core'
    import { Path } from '@serenity-js/core/lib/io'
    import { Name, TextData } from '@serenity-js/core/lib/model'

    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()),
    )
    })

    static textData = (contents: Answerable<string>, name?: string): Interaction =>
    Interaction.where(the`#actor attaches text data`, async actor => {
    const data = await actor.answer(contents);

    actor.collect(
    TextData.fromJSON({ contentType: 'text/plain', data }),
    name && new Name(name),
    )
    })
    }

    Attaching plain text

    import { actorCalled } from '@serenity-js/core'
    import { Path } from '@serenity-js/core/lib/io'

    actorCalled('Alice').attemptsTo(
    Attach.textData('some text', 'some name'),
    )

    Attaching contents of a text file

    import { actorCalled } from '@serenity-js/core'
    import { Log } from '@serenity-js/core'

    actorCalled('Alice').attemptsTo(
    Attach.contentsOf(Path.from(__dirname, 'output/server.log')),
    )

    Parameters

    • externalartifact: Artifact

      The artifact to be collected, such as JSON data.

    • externaloptionalname: Name

      The name of the artifact to make it easy to recognise in the test report

    Returns void