Documentation

Scenario
in package

The scenario is the core of an Artillery test script. It defines the flow of requests.

It has all methods related to a scenario and its flow.

Tags
example
$loginScenario = Artillery::scenario()
    ->addRequest(
        Artillery::request('get', 'https://example.com/login')
            ->setJsons(['username' => '{{ user }}', 'password' => '{{ pass }}'])
            ->addCapture('token', 'json', '$.token')
    )
   ->addRequest(
        Artillery::request('post', 'https://example.com/inbox')
            ->setJson('token', '{{ token }}')
    );

$artillery = Artillery::new()
    ->addScenario($loginScenario)
    ->addLoop(
    ->addScenario(Artillery::request('GET', 'https://example.com/inbox'), 'inbox')
    ->addScenario(Artillery::request('GET', 'https://example.com/news'), 'news');
link
https://www.artillery.io/docs/guides/getting-started/writing-your-first-test#scenarios
link
https://www.artillery.io/docs/guides/overview/why-artillery#scenarios

Table of Contents

addAfterResponse()  : $this
Add a function or array of functions to be executed after a response is received.
addAfterScenario()  : $this
Add a function or array of functions from the JavaScript file defined with Artillery::setProcessor to be run at the end of this scenario.
addBeforeRequest()  : $this
Add a function or array of functions to be executed after a scenario is finished.
addBeforeScenario()  : $this
Add a function or array of functions from the JavaScript file defined with Artillery::setProcessor to be run at the start of this scenario.
addFlow()  : $this
Add/merge the flow of another Scenario into this scenario's flow.
addFunction()  : $this
Adds a function or array of functions from the JavaScript file defined with Artillery::setProcessor() to be executed at this point in the Scenario's flow.
addLog()  : $this
Adds a log to the flow which will print messages to the console at this point in the flow.
addLoop()  : $this
Adds a loop to the flow, which can be another Scenario, a request or an array of either.
addRequest()  : $this
Add a request object directly to this scenario's flow.
addRequests()  : $this
Add an array of request objects directly to this scenario's flow.
addThink()  : $this
Adds a pause segment to the flow to pause the virtual user for N seconds.
set()  : $this
Set an arbitrary custom option in this scenario.
setEngine()  : $this
Set an engine to use for this scenario, if it's ot set then the HTTP engine will be used.
setName()  : $this
Set the name of this scenario for metrics and reporting.
setWeight()  : $this
Set the weight chance to be picked for this scenario when its weight is compared to that of other scenarios added to the Artillery script.

Methods

addAfterResponse()

Add a function or array of functions to be executed after a response is received.

public addAfterResponse(string|array<string|int, string> $function) : $this
Parameters
$function : string|array<string|int, string>

The function(s) to execute.

Tags
description

A JavaScript file defined with Artillery::setProcessor can export methods which can be executed after a response is received where the response can be inspected, and custom variables can be set.

link
https://www.artillery.io/docs/guides/guides/http-reference#afterresponse-hooks
Return values
$this

The current Request instance.

addAfterScenario()

Add a function or array of functions from the JavaScript file defined with Artillery::setProcessor to be run at the end of this scenario.

public addAfterScenario(string|array<string|int, string> $function) : $this
Parameters
$function : string|array<string|int, string>

The function or array of functions to add.

Tags
example
$artillery = Artillery::new()
    	->setProcessor('./helpers.js');

$scenario = Artillery::scenario()
    ->addAfterScenario('validateResponse')
    ->addRequest(Artillery::request('get', '/users/1'));
link
https://www.artillery.io/docs/guides/guides/http-reference#setting-scenario-level-hooks
link
https://www.artillery.io/docs/guides/guides/http-reference#function-actions-and-beforescenario--afterscenario-hooks
Return values
$this

The current Scenario instance.

addBeforeRequest()

Add a function or array of functions to be executed after a scenario is finished.

public addBeforeRequest(string|array<string|int, string> $function) : $this
Parameters
$function : string|array<string|int, string>

The function(s) to execute.

Tags
description

A JavaScript file defined with Artillery::setProcessor can export methods which can be executed before a request is sent, where you can set headers or body dynamically.

link
https://www.artillery.io/docs/guides/guides/http-reference#beforerequest-hooks
Return values
$this

The current Request instance.

addBeforeScenario()

Add a function or array of functions from the JavaScript file defined with Artillery::setProcessor to be run at the start of this scenario.

public addBeforeScenario(string|array<string|int, string> $function) : $this
Parameters
$function : string|array<string|int, string>

The function or array of functions to add.

Tags
example
$artillery = Artillery::new()
    	->setProcessor('./helpers.js');

$scenario = Artillery::scenario()
    ->addBeforeScenario(['setMessageVar', 'setRandomVar'])
    ->addRequest(Artillery::request('post', '/message')
        ->setJson('message', '{{ message }}')
        ->setJson('number', '{{ random }}'));
link
https://www.artillery.io/docs/guides/guides/http-reference#setting-scenario-level-hooks
link
https://www.artillery.io/docs/guides/guides/http-reference#function-actions-and-beforescenario--afterscenario-hooks
Return values
$this

The current Scenario instance.

addFlow()

Add/merge the flow of another Scenario into this scenario's flow.

public addFlow(Scenario $scenario) : $this
Parameters
$scenario : Scenario

The Scenario from which flow to add to this Scenario's flow.

Tags
example
$scenario = Artillery::scenario()
    ->addFlow(
        Artillery::Scenario()
            ->addRequest(Artillery::request('GET', 'https://example.com'))
            ->addThink(2));
link
https://www.artillery.io/docs/guides/getting-started/writing-your-first-test#adding-a-scenario-and-flow
Return values
$this

The current Scenario instance.

addFunction()

Adds a function or array of functions from the JavaScript file defined with Artillery::setProcessor() to be executed at this point in the Scenario's flow.

public addFunction(string|array<string|int, string> $function[, string|null $ifTrue = null ]) : $this
Parameters
$function : string|array<string|int, string>

The function to add.

$ifTrue : string|null = null

The condition for this function to be run

Tags
example
 // Set the javascript file:
$artillery->setProcessor('functions.js');

// Possibly set some variables in an exported function at the start of the flow:
$scenario = Artillery::scenario()
   ->addFunction('myFunction');
link
https://www.artillery.io/docs/guides/guides/http-reference#function-steps-in-a-flow
Return values
$this

The current Scenario instance.

addLog()

Adds a log to the flow which will print messages to the console at this point in the flow.

public addLog(string $message[, string|null $ifTrue = null ]) : $this
Parameters
$message : string

The message to log once this part of the flow is reached.

$ifTrue : string|null = null

The condition for this log.

Tags
description

It can also include a variable reference like '{{ variable }}'.
Log messages will get printed to the console even when running the tests in "quiet" mode (using the --quiet or -q flag with the run command).

example
$scenario = Artillery::scenario()
    ->addLog('Creating user {{ username }}.')
    ->addRequest(Artillery::request('post', '/new/{{ username }}'))
    ->addLog('Changing name to {{ name }}.')
    ->addRequest(Artillery::request('put', '/change/{{ username }}')
        ->setJson('name', '{{ name }}'));
link
https://www.artillery.io/docs/guides/guides/http-reference#logging
Return values
$this

The current Scenario instance.

addLoop()

Adds a loop to the flow, which can be another Scenario, a request or an array of either.

public addLoop(Scenario|RequestInterface|array<string|int, \ArtilleryPhp\Scenario|\ArtilleryPhp\RequestInterface> $loop[, int|null $count = null ][, string|null $over = null ][, string|null $whileTrue = null ]) : $this

To make it even more fun, loops can be nested.

Parameters
$loop : Scenario|RequestInterface|array<string|int, \ArtilleryPhp\Scenario|\ArtilleryPhp\RequestInterface>

A Scenario, Request or array containing these types.

$count : int|null = null

The number of times to loop.

$over : string|null = null

The variable reference to loop over.

$whileTrue : string|null = null

The condition to continue looping.

Tags
example
 // Loop through 100 pages:
$scenario = Artillery::scenario()
    ->addLoop(
        Artillery::request('get', '/pages/{{ $loopCount }}'),
        100);
link
https://www.artillery.io/docs/guides/guides/http-reference#loops
link
https://github.com/rjnienaber/artillery-test/blob/master/nested_loops/test.yml
Return values
$this

The current Scenario instance.

addRequest()

Add a request object directly to this scenario's flow.

public addRequest(RequestInterface $request) : $this
Parameters
$request : RequestInterface

The request to add.

Tags
example
$scenario = Artillery::scenario()
    ->addRequest(
        Artillery::request('get', '/users')
            ->addCapture('json', '$.users[0].id', 'userId'));
link
https://www.artillery.io/docs/guides/guides/http-reference#get--post--put--patch--delete-requests
link
https://www.artillery.io/docs/guides/guides/http-reference
Return values
$this

The current Scenario instance.

addRequests()

Add an array of request objects directly to this scenario's flow.

public addRequests(array<string|int, RequestInterface$requests) : $this
Parameters
$requests : array<string|int, RequestInterface>

The requests to add.

Tags
example
$scenario = Artillery::scenario()
    ->addRequests([
        Artillery::request('get', '/'),
        Artillery::request('get', '/home'),
        Artillery::request('get', '/about')
    ]);
link
https://www.artillery.io/docs/guides/guides/http-reference#get--post--put--patch--delete-requests
link
https://www.artillery.io/docs/guides/guides/http-reference
Return values
$this

The current Scenario instance.

addThink()

Adds a pause segment to the flow to pause the virtual user for N seconds.

public addThink(float $duration[, string|null $ifTrue = null ]) : $this
Parameters
$duration : float

The duration of the pause in seconds.

$ifTrue : string|null = null

The condition for this pause to be run

Tags
description

The argument to think is the number of second to pause for.
Floating numbers are supported, e.g., 0.5 pauses for half a second.

example
$scenario = Artillery::scenario()
    ->addRequest(Artillery::request('post', '/new/{{ username }}'))
    ->addThink(0.5)
    ->addRequest(Artillery::request('put', '/change/{{ username }}')
        ->setJson('name', '{{ name }}'));
link
https://www.artillery.io/docs/guides/guides/http-reference#pausing-execution-with-think
Return values
$this

The current Scenario instance.

set()

Set an arbitrary custom option in this scenario.

public set(string $key, mixed $value) : $this
Parameters
$key : string

The name of the option.

$value : mixed

The value of the option.

Tags
example
$artillery = Artillery::new('https://artillery.io')
    ->addPhase(['arrivalRate' => 1, 'duration' => 10])
    ->setEngine('playwright')
    ->setProcessor('./flows.js');

$scenario = Artillery::scenario('A flow with multiple steps')
    ->setEngine('playwright')
    ->set('flowFunction', 'multistepWithCustomMetrics');
Return values
$this

The current Scenario instance.

setEngine()

Set an engine to use for this scenario, if it's ot set then the HTTP engine will be used.

public setEngine(string $engine) : $this

If 'ws' is set, then you must only use WsRequest in this Scenario.

Parameters
$engine : string

Desired engine to use in this scenario.

Tags
description

Custom engines can be defined with Artillery::setEngine.
This library aims to fully support HTTP, partial support for WebSocket, and only raw support for others.

example
$webSocketScenario = Artillery::scenario()
    ->setEngine('ws')
    ->addRequest(
        Artillery::wsRequest('ws://localhost:8080', ['send' => 'Hello World!']));
link
https://www.artillery.io/docs/guides/guides/extension-apis#engines
link
https://www.artillery.io/docs/guides/guides/ws-reference#enabling-websocket-support
Return values
$this

The current Scenario instance.

setName()

Set the name of this scenario for metrics and reporting.

public setName(string $name) : $this
Parameters
$name : string

The name of this scenario.

Return values
$this

The current Scenario instance.

setWeight()

Set the weight chance to be picked for this scenario when its weight is compared to that of other scenarios added to the Artillery script.

public setWeight(int $weight) : $this
Parameters
$weight : int

The weight of this scenario.

Tags
description

Weights allow you to specify that some scenarios should be picked more often than others.
If it is not set, the default weight is 1.

example
// Here the $scenario1 will be picked 10% of the time:
$artillery->addScenario($scenario1);
$artillery->addScenario($scenario2->setWeight(10));
link
https://www.artillery.io/docs/guides/guides/test-script-reference#scenario-weights
Return values
$this

The current Scenario instance.

Search results