import {Question} from '@serenity-js/core/lib/screenplay'
public class | source

Question

Enables the Actor to query the system under test.

Direct Subclasses:

src/screenplay/Question.ts~AnonymousQuestion, Note

Examples:

A basic Question
 import { Actor, AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
 import { Ensure, equals } from '@serenity-js/assertions'

 const LastItemOf = <T>(list: T[]): Question<T> =>
     Question.about('last item from the list', (actor: AnswersQuestions & UsesAbilities) => {
         return list[list.length - 1];
     });

 Actor.named('Quentin').attemptsTo(
     Ensure.that(LastItemFrom([1,2,3]), equals(3)),
 );
A question using the Actor's Ability to do something
 import { AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
 import { CallAnApi } from '@serenity-js/rest'

 const TextOfLastResponseStatus = () =>
     Question.about<number>(`the text of the last response status`, actor => {
         return CallAnApi.as(actor).mapLastResponse(response => response.statusText);
     });
Mapping answers to other questions
 import { Actor, AnswersQuestions, UsesAbilities, Question } from '@serenity-js/core'
 import { CallAnApi, LastResponse } from '@serenity-js/rest'
 import { Ensure, equals } from '@serenity-js/assertions';

 const RequestWasSuccessful = () =>
     Question.about<number>(`the text of the last response status`, actor => {
         return LastResponse.status().answeredBy(actor) === 200;
     });

 const actor = Actor.named('Quentin').whoCan(CallAnApi.at('https://myapp.com/api'));

 actor.attemptsTo(
     Send.a(GetRequest.to('/books/0-688-00230-7')),
     Ensure.that(RequestWasSuccessful(), isTrue()),
 );

Tests:

Static Method Summary

Static Public Methods
public static

about(description: string, body: function(actor: AnswersQuestions & UsesAbilities): R): Question<R>

Factory method that simplifies the process of defining custom questions.

public static

isAQuestion(maybeQuestion: any): boolean

Checks if the value is a Question.

Constructor Summary

Protected Constructor
protected

constructor(subject: string)

Member Summary

Public Members
public

subject: *

Method Summary

Public Methods
public

describedAs(subject: string): Question<T>

Changes the description of this question's subject.

public

map(mapping: function(value: A, index?: number): Promise<O> | O): Question<Promise<Mapped>>

Creates a new Question, which value is a result of applying the mapping function to the value of this Question.

public

toString(): string

Describes the subject of this Question.

Static Public Methods

public static about(description: string, body: function(actor: AnswersQuestions & UsesAbilities): R): Question<R> source

Factory method that simplifies the process of defining custom questions.

Params:

NameTypeAttributeDescription
description string
body function(actor: AnswersQuestions & UsesAbilities): R

Returns:

Question<R>

Examples:

 const EnvVariable = (name: string) =>
     Question.about(`the ${ name } env variable`, actor => process.env[name])

public static isAQuestion(maybeQuestion: any): boolean source

Checks if the value is a Question.

Params:

NameTypeAttributeDescription
maybeQuestion any

The value to check

Returns:

boolean

Protected Constructors

protected constructor(subject: string) source

Params:

NameTypeAttributeDescription
subject string

The subject of this question

Public Members

public subject: * source

Public Methods

public describedAs(subject: string): Question<T> source

Changes the description of this question's subject.

Params:

NameTypeAttributeDescription
subject string

Returns:

Question<T>

public map(mapping: function(value: A, index?: number): Promise<O> | O): Question<Promise<Mapped>> source

Creates a new Question, which value is a result of applying the mapping function to the value of this Question.

Params:

NameTypeAttributeDescription
mapping function(value: A, index?: number): Promise<O> | O

A mapping function that receives a value of type <A>, which is either:

  • an answer to the original question, if the question is defined as Question<Promise<A>> or Question<A>
  • or, if the question is defined as Question<Promise<Mappable<A>>, Question<Mappable<A>> - each item of the Mappable collection,

Returns:

Question<Promise<Mapped>>

A new Question which value is a result of applying the mapping function to the value of the current question, so that:

  • if the answer to the current question is a Mappable<A>, the result becomes Question<Promise<O[]>>
  • if the answer is a value <A> or Promise<A>, the result becomes Question<Promise<O>>

Examples:

Mapping a Question<Promise<string>> to Question<Promise<number>>
 import { Question, replace, toNumber } from '@serenity-js/core';

 Question.about('the price of some item', actor => '$3.99')
     .map(replace('$', ''))
     .map(toNumber)

 // => Question<Promise<number>>
 //      3.99
Mapping all items of Question<string[]> to Question<Promise<number>>
 import { Question, trim } from '@serenity-js/core';

 Question.about('things to do', actor => [ ' walk the dog  ', '  read a book  ' ])
     .map(trim())

 // => Question<Promise<string[]>>
 //      [ 'walk the dog', 'read a book' ]
Using a custom mapping function
 import { Question } from '@serenity-js/core';

 Question.about('normalised percentages', actor => [ 0.1, 0.3, 0.6 ])
     .map((value: number) => value * 100)

 // => Question<Promise<number[]>>
 //      [ 10, 30, 60 ]

See:

public toString(): string source

Describes the subject of this Question.

Returns:

string