externalCallAnApi
Hierarchy
- Ability
- CallAnApi
Index
Constructors
externalconstructor
Learn more
Parameters
externalaxiosInstance: AxiosInstance
A pre-configured instance of the Axios HTTP client
Returns CallAnApi
Methods
staticexternalat
staticexternalusing
Produces an ability to call an HTTP API using the given Axios instance, or an Axios request configuration object.
When you provide an Axios configuration object, it gets shallow-merged with the following defaults:
- request timeout of 10 seconds
- automatic proxy support based on your environment variables
When you provide an Axios instance, it's enhanced with proxy support and no other modifications are made.
If you don't want Serenity/JS to augment or modify your Axios instance in any way, please use the
CallAnApi.constructor
directly.Parameters
externalaxiosInstanceOrConfig: AxiosInstance | AxiosRequestConfigDefaults
Returns CallAnApi
externalabilityType
Returns the most abstract type of this Ability instance, specifically the first class in the inheritance hierarchy that directly extends the
Ability
class.import { Ability } from '@serenity-js/core';
class MyAbility extends Ability {}
class MySpecialisedAbility extends MyAbility {}
new MyAbility().abilityType(); // returns MyAbility
new MySpecialisedAbility().abilityType(); // returns MyAbilityReturns AbilityType<Ability>
externalmodifyConfig
externalrequest
Sends an HTTP request to a specified url. Response will be cached and available via
CallAnApi.mapLastResponse
.Learn more
Parameters
externalconfig: AxiosRequestConfig<any>
Returns Promise<AxiosResponse<any, any>>
externalresolveUrl
Resolves the final URL, based on the
AxiosRequestConfig
provided and any defaults that theAxiosInstance
has been configured with.Note that unlike Axios, this method uses the Node.js WHATWG URL API to ensure URLs are correctly resolved.
Parameters
externalconfig: AxiosRequestConfig<any>
Returns string
externalmapLastResponse
Maps the last cached response to another type. Useful when you need to extract a portion of the
AxiosResponse
object.Learn more
Type parameters
- T
Parameters
externalmappingFunction: (response: AxiosResponse<any, any>) => T
Returns T
externaltoJSON
Returns SerialisedAbility
An ability that wraps axios client and enables the actor to send HTTP requests to HTTP APIs.
CallAnApi
usesproxy-from-env
and an approach described in "Node.js Axios behind corporate proxies" to automatically detect proxy server configuration based on your environment variables. You can override this configuration if needed.Configuring the ability to call an API
The easiest way to configure the ability to
CallAnApi
is to provide thebaseURL
of your HTTP API, and rely on Serenity/JS to offer other sensible defaults:Resolving relative URLs
Serenity/JS resolves request URLs using Node.js WHATWG URL API. This means that the request URL is determined using the resource path resolved in the context of base URL, i.e.
new URL(resourcePath, [baseURL])
.Consider the following example:
In the above example:
resourcePath
is defined as a full URL, it overrides the base URLresourcePath
starts with a forward slash/
, it replaces any path defined in the base URLresourcePath
is not a full URL and doesn't start with a forward slash, it gets appended to the base URLhttps://api.example.org/
/v1/users/2
https://api.example.org/v1/users/2
https://example.org/api/v1/
users/2
https://example.org/api/v1/users/2
https://example.org/api/v1/
/secure/oauth
https://example.org/secure/oauth
https://v1.example.org/api/
https://v2.example.org/
https://v2.example.org/
Using Axios configuration object
When you need more control over how your Axios instance is configured, provide an Axios configuration object. For example:
Working with proxy servers
CallAnApi
usesproxy-from-env
to automatically detect proxy server configuration based on your environment variables.This default behaviour can be overridden by providing explicit proxy configuration:
Please note that Serenity/JS uses proxy-agents and the approach described in "Node.js Axios behind corporate proxies" to work around limited proxy support capabilities in Axios itself.
Using Axios instance with proxy support
To have full control over the Axios instance used by the ability to
CallAnApi
, you can create it yourself and inject it into the ability. This approach allows you to still benefit from automated proxy detection in configuration, while taking advantage of the many Axios plugins.Using raw Axios instance
If you don't want Serenity/JS to enhance your Axios instance with proxy support, instantiate the ability to
CallAnApi
using its constructor directly. Note, however, that by using this approach you're taking the responsibility for all the aspects of configuring Axios.Serenity/JS defaults
When using
CallAnApi.at
orCallAnApi.using
with a configuration object, Serenity/JS merges your Axios request configuration with the following defaults:timeout
: 10 secondsYou can override them by specifying the given property in your configuration object, for example:
Interacting with multiple APIs
Some test scenarios might require you to interact with multiple HTTP APIs. With Serenity/JS you can do this using either API-specific actors, or by specifying full URLs when performing the requests.
The following examples will assume that the test scenarios needs to interact with the following APIs:
https://testdata.example.org/api/v1/
https://shop.example.org/api/v1/
Let's also assume that the
testdata
API allows the automation to manage the test data used by theshop
API.Using API-specific actors
To create API-specific actors, configure your test runner with a cast that gives your actors appropriate abilities based, for example, on their name:
Next, retrieve the appropriate actor in your test scenario using
actorCalled
, for example:Using full URLs
If you prefer to have a single actor interacting with multiple APIs, you can specify the full URL for every request:
Learn more