Artillery.io Login and capture response and cookies before each VU runs

2.3k Views Asked by At

Using Artillery.io load tester I am trying to login each VU Before running the Scenarios flow (I do not want the login requests to be part of the scenario results) I tried moving the login flow to the beforeScenario but that doesn't seem to run

config:
  target: '{{ $processEnvironment.URL }}'
scenarios:
  - name: 'Warm Up'
    beforeScenario:
      flow:
        - post:
            url: '/login'
            json:
              username: 'admin'
              password: 'password'
            capture:
              - json: '$.user._id'
                as: 'userId'
    flow:
      - post:
          url: '/api/graphql'
          json:
            - operationName: 'apiRequest'
              query: 'query aQuery($userId: ID!) { aQuery: testQuery(userId: $userId) { id name } }'
              variables:
                userId: '{{ userId }}'

Is there any way to achieve this?

2

There are 2 best solutions below

0
On

Not sure if you have figured this out but i used the "before" section that comes before the scenario and calls a function to helper function that login and saves the access_token

before:
  flow:
    - function: "login"
scenarios:
  - name: "Create"
  - flow:
    - post:
        url: ""
        headers:
          Authorization: "Bearer {{access_token}}"
0
On

Before your "scenarios" section, you must use something like this (just an example):

before:
  flow:
    - log: "Running the Authorization before virtual users run"
    - post:
        url: "/users/login"
        json:
          username: "{{user}}"
          password: "{{password}}"

        capture:
          json: "$.data.token"
          as: "accessToken"
        strict: false

        expect:
          - statusCode: 200