Documentation

AnyRequest extends RequestBase
in package

Anonymous Request type. Can be used for not-yet-supported or custom engines. Crude implementation.

Tags
example
$emitAndValidateResponse = Artillery::scenario('Emit and validate response')
    ->setEngine('socketio')
    ->addRequest(
        Artillery::anyRequest('emit')
            ->set('channel', 'echo')
            ->set('data', 'Hello from Artillery')
            ->set('response', ['channel' => 'echoResponse', 'data' => 'Hello from Artillery']));
link
https://www.artillery.io/docs/guides/guides/ws-reference

Table of Contents

addCapture()  : $this
Adds a capture to the request to parse responses and re-use those values in subsequent requests as '{{ alias }}'.
addCaptures()  : $this
Adds an array of capture objects to the request.
addExpect()  : $this
Adds an expectation assertion to the request.
addExpects()  : $this
Adds an array of expectation assertions to the request.
addMatch()  : $this
Adds a matching statement to the request, similar to a capture but without an 'as' alias.
addMatches()  : $this
Adds an array of matching statement to the request, similar to a capture but without an 'as' alias.
set()  : $this
Set arbitrary data in the request, such as those from third party extension.
setMethod()  : $this
Set the method of this request, eg 'put', 'think', 'emit', etc.
setRequest()  : $this
Completely override the request data for this request.

Methods

addCapture()

Adds a capture to the request to parse responses and re-use those values in subsequent requests as '{{ alias }}'.

public addCapture(string $as, string $type, string $expression[, bool $strict = true ][, string|null $attr = null ][, int|string $index = null ]) : $this
Parameters
$as : string

Captured data alias.

$type : string
$expression : string

Capture expression.

$strict : bool = true

Whether to throw an error if the capture expression does not match anything.

$attr : string|null = null

If $type is 'selector': The name of the attribute whose value we want.

$index : int|string = null
Tags
description

The capture option must always have an as attribute, which names the value for use in subsequent requests. It also requires one of the following attributes:

  • json - Allows you to define a JSONPath expression.
  • xpath - Allows you to define an XPath expression.
  • regexp - Allows you to define a regular expression that gets passed to a RegExp constructor. A specific capturing group to return may be set with the group attribute (set to an integer index of the group in the regular expression). Flags for the regular expression may be set with the flags attribute.
  • header - Allows you to set the name of the response header whose value you want to capture.
  • selector - Allows you to define a Cheerio element selector. The attr attribute will contain the name of the attribute whose value we want. An optional index attribute may be set to a number to grab an element matching a selector at the specified index, "random" to grab an element at random, or "last" to grab the last matching element. If the index attribute is not specified, the first matching element will get captured.
example
// Cheerio element selector: $request->addCapture(as: "productUrl", type: "selector", expression: "a[class^=productLink]", index: "random", attr: "href")
// Xpath: $request->addCapture(as: "JourneyId", type: "xpath", expression: "(//Journey)[1]/JourneyId/text()")
// Header: $request->addCapture(as: "headerValue", type: "header", expression: "x-my-custom-header")
// Json:
$getIdRequest = Artillery::request('get', '/users')
   ->addCapture('first_id', 'json', '$[0].id');
$postMsgRequest = Artillery::request('post', '/inbox')
  ->setPayload(['id' => '{{ first_id }}', 'msg' => 'Hello world!']);
link
https://www.artillery.io/docs/guides/guides/http-reference#extracting-and-re-using-parts-of-a-response-request-chaining
Return values
$this

The current Request instance.

addCaptures()

Adds an array of capture objects to the request.

public addCaptures(array<string|int, mixed> $captures) : $this
Parameters
$captures : array<string|int, mixed>
Tags
description

The capture option must always have an as attribute, which names the value for use in subsequent requests. It also requires one of the following attributes:

  • json - Allows you to define a JSONPath expression.
  • xpath - Allows you to define an XPath expression.
  • regexp - Allows you to define a regular expression that gets passed to a RegExp constructor. A specific capturing group to return may be set with the group attribute (set to an integer index of the group in the regular expression). Flags for the regular expression may be set with the flags attribute.
  • header - Allows you to set the name of the response header whose value you want to capture.
  • selector - Allows you to define a Cheerio element selector. The attr attribute will contain the name of the attribute whose value we want. An optional index attribute may be set to a number to grab an element matching a selector at the specified index, "random" to grab an element at random, or "last" to grab the last matching element. If the index attribute is not specified, the first matching element will get captured.
example
// Cheerio element selector: $request->addCapture(as: "productUrl", type: "selector", expression: "a[class^=productLink]", index: "random", attr: "href")
// Xpath: $request->addCapture(as: "JourneyId", type: "xpath", expression: "(//Journey)[1]/JourneyId/text()")
// Header: $request->addCapture(as: "headerValue", type: "header", expression: "x-my-custom-header")
// Json:
$getIdRequest = Artillery::request('get', '/users')
    ->addCaptures([
        ['as' => 'id', 'json' => '$[0].id'],
	       ['as' => 'name', 'json' => '$[0].name'],
    ]);

$postMsgRequest = Artillery::request('post', '/inbox')
  ->setPayload(['id' => '{{ id }}', 'msg' => 'Hello {{ name }}!']);
link
https://www.artillery.io/docs/guides/guides/http-reference#extracting-and-re-using-parts-of-a-response-request-chaining
Return values
$this

The current Request instance.

addExpect()

Adds an expectation assertion to the request.

public addExpect(string $type, mixed $value[, mixed $equals = null ]) : $this
Parameters
$type : string
$value : mixed

The value(s) to expect.

$equals : mixed = null

Equals can be added after hasHeader and hasProperty to do an equality check.

Tags
description

Expectations are assertions that are checked after the request is made. If the assertion fails, the request is considered to have failed.
The built-in 'expect' plugin needs to be enabled with Artillery::setPlugin('expect') for this feature. Here's a list of the available expectations:
contentType, statusCode, notStatusCode: expectNotStatusCode, hasHeader, headerEquals, hasProperty, equals, matchesRegexp, notHasProperty, cdnHit.

example
npm i artillery-plugin-expect
$artillery->setPlugin('expect');
$ensureRequest = Artillery::request('get', '/users/1')
   ->addExpect('statusCode', [200, 201]);
link
https://www.artillery.io/docs/guides/plugins/plugin-expectations-assertions#expectations
link
https://www.artillery.io/docs/guides/plugins/plugin-expectations-assertions
Return values
$this

The current Request instance.

addExpects()

Adds an array of expectation assertions to the request.

public addExpects(array<string|int, mixed> $expects) : $this
Parameters
$expects : array<string|int, mixed>
Tags
description

Expectations are assertions that are checked after the request is made. If the assertion fails, the request is considered to have failed.
The built-in 'expect' plugin needs to be enabled with Artillery::setPlugin('expect') for this feature.

example
npm i artillery-plugin-expect
$artillery->setPlugin('expect');
$expectJson200 = [
    ['statusCode' => 200],
    ['contentType' => 'application/json'],
];
$ensureRequest = Artillery::request('get', '/users/1')
   ->addExpects($expectJson200);
link
https://www.artillery.io/docs/guides/plugins/plugin-expectations-assertions#expectations
link
https://www.artillery.io/docs/guides/plugins/plugin-expectations-assertions
Return values
$this

The current Request instance.

addMatch()

Adds a matching statement to the request, similar to a capture but without an 'as' alias.

public addMatch(string $type, string $expression, mixed $value[, bool $strict = true ][, string $attr = null ][, int|string $index = null ]) : $this
Parameters
$type : string
$expression : string

The capture expression.

$value : mixed

The value to match against the capture.

$strict : bool = true
$attr : string = null
$index : int|string = null
Tags
example
$request = Artillery::request('get', '/users')
    ->addMatch('json', '$[0].name', 'John');
Return values
$this

The current Request instance.

addMatches()

Adds an array of matching statement to the request, similar to a capture but without an 'as' alias.

public addMatches(array<string|int, mixed> $matches) : $this
Parameters
$matches : array<string|int, mixed>
Tags
example
$request = Artillery::request('get', '/users')
    ->addMatches([
        ['json' => '$[0].name', 'value' => 'John'],
        ['json' => '$[1].name', 'value' => 'Jane']
    ]);
Return values
$this

The current Request instance.

set()

Set arbitrary data in the request, such as those from third party extension.

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

Arbitrary data to add to the request.

Tags
example
$artillery = Artillery::new()->setPlugin('something', ['some' => 'option']);
$request = Artillery::request('get', '/users/1')
    ->set('something', ['pluginAction' => 'values']);
Return values
$this

The current Request instance.

setMethod()

Set the method of this request, eg 'put', 'think', 'emit', etc.

public setMethod(string $method) : $this
Parameters
$method : string

The HTTP method for this request.

Return values
$this

The current Request instance.

setRequest()

Completely override the request data for this request.

public setRequest(mixed $request) : $this
Parameters
$request : mixed

The request data.

Return values
$this

The current Request instance.

Search results