How to run an artillery script with input variables?

563 Views Asked by At
artillery run my-script.yaml

In order to log into the target, a token must be used.

How do I run the above script with a token? IE:

artillery run my-script.yaml token 983r2fh29hf2893yr72
1

There are 1 best solutions below

0
bernardobridge On

I'm assuming that the token must be provided to the API through, for example, a header. Artillery provides several mechanisms of doing that. If the token is static (i.e. you know the token ahead of time), then you can simply use an inline variable in your test script to do that.

For example:

my-script.yaml

config:
  target: "https://yourapi.com"
  phases:
    - duration: 1
      arrivalRate: 1
  variables:
    apiToken: 983r2fh29hf2893yr72 

scenarios:
  - flow:
      - get:
          url: "/"
          headers:
            Authorization: "Bearer {{ apiToken }}"

Then you simply run the script (i.e. artillery run my-script.yaml)


If the token is dynamic (for example you need to call an endpoint to authenticate and get a token before), then you might need to use a custom javascript function like a scenario hook: https://www.artillery.io/docs/reference/engines/http#setting-scenario-level-hooks. Or, alternatively, a before which runs once before all scenarios (https://www.artillery.io/docs/reference/test-script#before-and-after-sections).

But from your description, it sounds like the first example I gave is what you need.