Skip to main content

ChangeApiConfig

Changes configuration of the ability to CallAnApi that the actor executing this interaction has been configured with.

Changing API URL for all subsequent requests

import { actorCalled } from '@serenity-js/core';
import { By Navigate, PageElement, Text } from '@serenity-js/web';
import { axiosCreate, CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions';

import * as axios from 'axios';

// Let's imagine that the website under test displays
// a dynamically generated API URL that we would like to use
const ApiDetailsWidget = {
url: () => PageElement.located(By.id('api-url')).describedAs('API URL'),
}

await actorCalled('Apisitt')
.whoCan(
BrowseTheWeb.using(protractor.browser),

// Note: no default base URL is given when the axios instance is created
CallAnApi.using(axiosCreate()),
)
.attemptsTo(
Navigate.to('/profile'),

// We change the API URL based on the text displayed in the widget
// (although we could change it to some arbitrary string too).
ChangeApiConfig.setUrlTo(Text.of(ApiDetailsWidget.url())),

// Any subsequent request will be sent to the newly set URL
Send.a(GetRequest.to('/projects')),
Ensure.that(LastResponse.status(), equals(200)),
)

Changing API port for all subsequent requests

import { actorCalled } from '@serenity-js/core'
import { LocalServer, ManageALocalServer, StartLocalServer } from '@serenity-js/local-server'
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'

await actorCalled('Apisitt')
.whoCan(
ManageALocalServer.runningAHttpListener(someServer),
CallAnApi.at('http://localhost'),
)
.attemptsTo(
StartALocalServer.onRandomPort(),
ChangeApiConfig.setPortTo(LocalServer.port()),
Send.a(GetRequest.to('/api')),
Ensure.that(LastResponse.status(), equals(200)),
)

Setting a header for all subsequent requests

import { actorCalled, Question } from '@serenity-js/core'
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'

// A sample Question reading a Node process environment variable
const EnvVar = (var_name: string) =>
Question.about(`${ name } environment variable`, actor => process.env[var_name]);

await actorCalled('Apisitt')
.whoCan(
CallAnApi.at('http://localhost'),
)
.attemptsTo(
ChangeApiConfig.setHeader('Authorization', EnvVar('TOKEN')),
Send.a(GetRequest.to('/api')),
Ensure.that(LastResponse.status(), equals(200)),
)

Handling sensitive information

By design, any data handled by an actor appears in Serenity/JS reports. To prevent the exposure of any sensitive information, such as passwords or tokens, you should use Masked.

import { actorCalled, Masked } from '@serenity-js/core'
import { CallAnApi, ChangeApiConfig, GetRequest, LastResponse, Send } from '@serenity-js/rest'
import { Ensure, equals } from '@serenity-js/assertions'

await actorCalled('Apisitt')
.whoCan(
CallAnApi.at('http://localhost'),
)
.attemptsTo(
ChangeApiConfig.setHeader('Authorization', Masked.valueOf('secret token')),
Send.a(GetRequest.to('/api')),
Ensure.that(LastResponse.status(), equals(200)),
)

Index

Constructors

constructor

Methods

staticsetUrlTo

  • setUrlTo(newApiUrl: Answerable<string>): Interaction
  • Instructs the actor to change the base URL of their ability to CallAnApi


    Parameters

    • newApiUrl: Answerable<string>

    Returns Interaction

staticsetPortTo

  • setPortTo(newApiPort: Answerable<number>): Interaction
  • Instructs the actor to change the port configured in the base URL of their ability to CallAnApi


    Parameters

    • newApiPort: Answerable<number>

    Returns Interaction

staticsetHeader

  • setHeader(name: Answerable<string>, value: Answerable<string>): Interaction
  • Instructs the actor to change the configuration of the AxiosInstance used by their ability to CallAnApi and set an HTTP request header for any subsequent HTTPRequests issued via Send.


    Parameters

    • name: Answerable<string>
    • value: Answerable<string>

    Returns Interaction