How to Create Lifecycle Hooks

Background

Lifecycle Hooks

Writing a Lifecycle Hooks Script

We currently only support lifecycle hook scripts written in Python.

  1. Write a Python script (e.g. lifecycle_hooks.py)

  2. In this script, define the lifecycle events to hook into

  3. Define what kind of behavior to execute for each of those events

Below is an example of a simple script that prints some strings during two events. In the script there are two functions with names that correspond to its lifecycle event:

  • handle_before_replay is for before a Request or Scenario gets replayed

  • handle_after_replay is for after a Request or Scenario gets replayed

Before a request or scenario gets replayed, the string "Before replay!" will be printed and after the replay, "After replay!" will be printed.

from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_before_replay(context: ReplayContext):
    print('Before replay!')

def handle_after_replay(context: ReplayContext):
    print('After replay!')

Each function takes a single context object. Manipulating this object will affect the behaviour of the request interception.

Enabling Lifecycle Hook Scripts Use

Once your script is written, it can now be used by the Agent.

Lifecycle hooks are currently supported for these commands:

  • stoobly-agent run

  • stoobly-agent request replay

  • stoobly-agent scenario replay

  • stoobly-agent endpoint import

For all of the above commands, the flag to specify the path to the lifecycle hooks file is --lifecycle-hooks-path <PATH>

For example, if you want to replay a request the command would look something like this:

$ stoobly-agent request replay <REQUEST_KEY> --lifecycle-hooks-path ~/path-to-file/lifecycle_hooks.py

Last updated