externalabstractQuestion <T>
Hierarchy
- Describable
- Question
Index
Methods
staticexternalabout
Factory method that simplifies the process of defining custom questions.
Defining a custom question
import { Question } from '@serenity-js/core'
const EnvVariable = (name: string) =>
Question.about(`the ${ name } env variable`, actor => process.env[name])Type parameters
- Answer_Type
- Supported_Context_Type
Parameters
externaldescription: Answerable<string>
externalbody: (actor: AnswersQuestions & UsesAbilities) => Answer_Type | Promise<Answer_Type>
externaloptionalmetaQuestionBody: (answerable: Answerable<Supported_Context_Type>) => Question<Promise<Answer_Type>> | Question<Answer_Type>
Returns MetaQuestionAdapter<Supported_Context_Type, Awaited<Answer_Type>>
staticexternalfromObject
Generates a
QuestionAdapter
that recursively resolves anyAnswerable
fields of the provided object, includingAnswerable
fields of nested objects.Optionally, the method accepts
overrides
to be shallow-merged with the fields of the originalsource
, producing a new merged object.Overrides are applied from left to right, with subsequent objects overwriting property assignments of the previous ones.
Resolving an object recursively using
Question.fromObject
import { actorCalled, Question } from '@serenity-js/core'
import { Send, PostRequest } from '@serenity-js/rest'
import { By, Text, PageElement } from '@serenity-js/web'
await actorCalled('Daisy')
.whoCan(CallAnApi.at('https://api.example.org'))
.attemptsTo(
Send.a(
PostRequest.to('/products/2')
.with(
Question.fromObject({
name: Text.of(PageElement.located(By.css('.name'))),
})
)
)
);Merging objects using
Question.fromObject
import { actorCalled, Question } from '@serenity-js/core'
import { Send, PostRequest } from '@serenity-js/rest'
import { By, Text, PageElement } from '@serenity-js/web'
await actorCalled('Daisy')
.whoCan(CallAnApi.at('https://api.example.org'))
.attemptsTo(
Send.a(
PostRequest.to('/products/2')
.with(
Question.fromObject({
name: Text.of(PageElement.located(By.css('.name'))),
quantity: undefined,
}, {
quantity: 2,
})
)
)
);Learn more
Type parameters
- Source_Type: object
Parameters
externalsource: Answerable<WithAnswerableProperties<Source_Type>>
externalrest...overrides: Answerable<Partial<WithAnswerableProperties<Source_Type>>>[]
Returns QuestionAdapter<RecursivelyAnswered<Source_Type>>
staticexternalfromArray
Generates a
QuestionAdapter
that resolves anyAnswerable
elements of the provided array.Type parameters
- Source_Type
Parameters
externalsource: Answerable<Source_Type>[]
externaloptionaloptions: DescriptionFormattingOptions
Returns QuestionAdapter<Source_Type[]>
staticexternalisAQuestion
staticexternalisAMetaQuestion
Checks if the value is a
MetaQuestion
.Type parameters
- CT
- RQT: Question<unknown>
Parameters
externalmaybeMetaQuestion: unknown
The value to check
Returns maybeMetaQuestion is MetaQuestion<CT, RQT>
staticexternalformattedValue
Creates a
MetaQuestion
that can be composed with anyAnswerable
to produce a single-line description of its value.import { actorCalled, Question } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'
const accountDetails = () =>
Question.about('account details', actor => ({ name: 'Alice', age: 28 }))
await actorCalled('Alice').attemptsTo(
Ensure.that(
Question.formattedValue().of(accountDetails()),
equals('{ name: "Alice", age: 28 }'),
),
)Parameters
externaloptionaloptions: DescriptionFormattingOptions
Returns MetaQuestion<any, Question<Promise<string>>>
staticexternalvalue
Creates a
MetaQuestion
that can be composed with anyAnswerable
to return its value when the answerable is aQuestion
, or the answerable itself otherwise.The description of the resulting question is produced by calling
Question.describedBy
on the provided answerable.import { actorCalled, Question } from '@serenity-js/core'
import { Ensure, equals } from '@serenity-js/assertions'
const accountDetails = () =>
Question.about('account details', actor => ({ name: 'Alice', age: 28 }))
await actorCalled('Alice').attemptsTo(
Ensure.that(
Question.description().of(accountDetails()),
equals('account details'),
),
Ensure.that(
Question.value().of(accountDetails()),
equals({ name: 'Alice', age: 28 }),
),
)Type parameters
- Answer_Type
Returns MetaQuestion<Answer_Type, Question<Promise<Answer_Type>>>
externalabstractansweredBy
Parameters
externalactor: AnswersQuestions & UsesAbilities
Returns T
externaldescribedAs
Changes the description of this object, as returned by
Describable.describedBy
andDescribable.toString
.Parameters
externaldescription: Answerable<string> | MetaQuestion<Awaited<T>, Question<Promise<string>>>
Replaces the current description according to the following rules:
- If
description
is anAnswerable
, it replaces the current description - If
description
is aMetaQuestion
, the current description is passed ascontext
todescription.of(context)
, and the result replaces the current description
- If
Returns this
publicexternalas
Maps this question to one of a different type.
Question.about('number returned as string', actor => '42') // returns: QuestionAdapter<string>
.as(Number) // returns: QuestionAdapter<number>Type parameters
- O
Parameters
externalmapping: (answer: Awaited<T>) => O | Promise<O>
Returns QuestionAdapter<O>
externaldescribedBy
Resolves the description of this object in the context of the provided
actor
.Parameters
externalactor: AnswersQuestions & UsesAbilities & { name: string }
Returns Promise<string>
externaltoString
Returns a human-readable description of this object.
Returns string
Questions describe how actors should query the system under test or the test environment to retrieve some information.
Questions are the core building block of the Screenplay Pattern, along with actors, abilities, interactions, and tasks.
Learn more about:
Actor
Ability
Interaction
QuestionAdapter
Implementing a basic custom Question
Implementing a Question that uses an Ability
Just like the interactions, a
Question
also can use actor's abilities.Here, we use the ability to
CallAnApi
to retrieve a property of an HTTP response.Learn more
CallAnApi
LastResponse
Mapping answers to other questions
Apart from retrieving information, questions can be used to transform information retrieved by other questions.
Here, we use the factory method
Question.about
to produce a question that makes the received actor answerLastResponse.status
and then compare it against some expected value.Note that the above example is for demonstration purposes only, Serenity/JS provides an easier way to verify the response status of the
LastResponse
: