Skip to main content

externalnotes

Callable


  • Alias for Notepad.notes.

    Pro tip: notes<T>().get(subject) returns a NotepadAdapter to make accessing the APIs of the underlying type easier. Check NotepadAdapter for more examples.

    Working with untyped notes

    You can use notes<T>() without parameterising it with an interface describing the structure of your notes.

    Note: this approach is not type-safe and the type-safe alternative presented below should be used in most cases.

    import { actorCalled, Log, notes, TakeNotes } from '@serenity-js/core';

    await actorCalled('Leonard')
    .whoCan(TakeNotes.usingAnEmptyNotepad())
    .attemptsTo(
    notes().set('my_note', 'some value'),

    Log.the(notes().get('my_note')),
    // emits "some value"

    Log.the(notes().get('my_note').toLocaleUpperCase()),
    // emits "SOME VALUE"
    );

    Working with typed notes

    The recommended way to use notes<T>() is to parameterise it with an interface describing the structure of your notes.

    Note: this approach is type-safe.

    import { actorCalled, Log, notes, TakeNotes } from '@serenity-js/core';

    interface MyNotes {
    username: string;
    token?: string; // optional value
    }

    await actorCalled('Leonard')
    .whoCan(TakeNotes.using(Notepad.with<MyNotes>({
    username: 'Leonard.McLaud@example.org',
    }))
    .attemptsTo(
    notes<MyNotes>().set('token', 'my-auth-token')

    Log.the(notes<MyNotes>().get('token').length),
    // emits 13

    Log.the(notes<MyNotes>().get('username').toLocaleLowerCase()),
    // emits 'leonard.mclaud@example.org'
    )

    Checking if a note is present

    import { actorCalled, Check, notes, TakeNotes } from '@serenity-js/core'
    import { isPresent } from '@serenity-js/assertions'

    await actorCalled('Leonard')
    .whoCan(TakeNotes.using(Notepad.empty()))
    .attemptsTo(
    Check.whether(notes().get('token'), isPresent())
    .andIfSo(
    Authenticate.using(notes().get('token')),
    )
    .otherwise(
    SignIn.using('username', 'password'),
    )
    )

    Working with complex data structures

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

    interface MyNotes {
    recordedItems: string[];
    }

    await actorCalled('Leonard')
    .whoCan(TakeNotes.using(Notepad.with<MyNotes>({
    recordedItems: [],
    }))
    .attemptsTo(
    // push 3 items
    notes().get('recordedItems').push('apples'),
    notes().get('recordedItems').push('cucumbers'),
    notes().get('recordedItems').push('bananas'),

    // use QuestionAdapter to access Array.sort()
    notes().get('recordedItems').sort(),

    Log.the(notes().get('recordedItems')),
    // emits 'apples', 'bananas', 'cucumbers'
    )

    Learn more


    Type parameters

    • N: Record<any, any>

    Returns NotepadAdapter<N>