externalActor
Implements
Index
Constructors
Properties
Methods
Constructors
externalconstructor
Properties
publicexternalreadonlyname
Methods
externalabilityTo
Retrieves actor's
Ability
ofabilityType
, or one that extendsabilityType
.Please note that this method performs an
instanceof
check against abilities given to this actor viaActor.whoCan
.Please also note that
Actor.whoCan
performs the same check when abilities are assigned to the actor to ensure the actor has at most one instance of a given ability type.Type parameters
- T: Ability
Parameters
externalabilityType: AbilityType<T>
Returns T
externalattemptsTo
Instructs the actor to attempt to perform a number of activities, so either tasks or interactions), one by one.
Parameters
externalrest...activities: Activity[]
Returns Promise<void>
externalwhoCan
externalanswer
Type parameters
- T
Parameters
externalanswerable: Answerable<T>
An
Answerable
to answer (resolve the value of).
Returns Promise<T>
The answer to the Answerable
externalcollect
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: string | Name
The name of the artifact to make it easy to recognise in the test report
Returns void
externalcurrentTime
Returns current time.
Returns Timestamp
externaldismiss
Instructs the actor to invoke
Discardable.discard
method on any discardable ability it's been configured with.Returns Promise<void>
externaltoString
Returns a human-readable, string representation of this actor and their abilities.
PRO TIP: To get the name of the actor, use
Actor.name
Returns string
externaltoJSON
Returns a JSON representation of the actor and its current state.
The purpose of this method is to enable reporting the state of the actor in a human-readable format, rather than to serialise and deserialise the actor itself.
Returns SerialisedActor
Actors represent people and external systems interacting with the system under test. Their role is to perform activities that demonstrate how to accomplish a given goal.
Actors are the core building block of the Screenplay Pattern, along with abilities, interactions, tasks, and questions. Actors are also the first thing you see in a typical Serenity/JS test scenario.
Learn more about:
Cast
Stage
Ability
Activity
Interaction
Task
Question
Representing people and systems as actors
To use a Serenity/JS
Actor
, all you need is to say their name:Serenity/JS actors perform within the scope of a test scenario, so the first time you invoke
actorCalled
, Serenity/JS instantiates a new actor from the default cast of actors (or any custom cast you might have configured). Any subsequent invocations of this function within the scope of the same test scenario retrieve the already instantiated actor, identified by their name.Serenity/JS scenarios can involve as many or as few actors as you need to model the given business workflow. For example, you might want to use multiple actors in test scenarios that model how different people perform different parts of a larger business process, such as reviewing and approving a loan application. It is also quite common to introduce supporting actors to perform administrative tasks, like setting up test data and environment, or audit tasks, like checking the logs or messages emitted to a message queue by the system under test.
Actor names can be much more than just simple identifiers like
Alice
orBob
. While you can give your actors any names you like, a good convention to follow is to give them names indicating the personae they represent or the role they play in the system.Just like the characters in Stan Lee graphic novels, actors in Serenity/JS test scenarios are often given alliterate names as a mnemonic device. Names like "Adam the Admin", "Edna the Editor", "Trevor the Traveller", are far more memorable than a generic "UI user" or "API user". They're also much easier for people to associate with the context, constraints, and affordances of the given actor.