↓ Skip to main content

Puppetchef – YAML Recipes for Puppeteer Web Automation

Table of Contents

Puppetchef is a web automation tool that uses Puppeteer to execute web automation recipes defined in YAML format. You can find and fork the source code on GitHub for personal or collaborative use.

npm version
License: MIT

Installation
#

You can find and install the package directly from the npm registry:

npm install puppetchef

When installed locally in a project, run it with npx puppetchef. To have the puppetchef command available everywhere, install it globally with npm install -g puppetchef instead.

Usage
#

puppetchef <recipe> [options]

Command-Line Options
#

  • <recipe>: Recipe file in YAML format (required).
  • -c, --conf <file>: Configuration file (default: puppetchefrc).
  • --syntax-check: Validate recipe only, without executing (default: false).

Environment Variables
#

  • PUPPETCHEF_LOGLEVEL: Enables debugging at the specified level (info, debug, error, warn, verbose).
  • PUPPETCHEF_LOGFILE: Path to the log file (default: stdout) when a debug level is set.

Configuration File
#

The configuration file, puppetchefrc by default, is a JSON object of Puppeteer launch options:

{
    "executablePath": "/usr/local/bin/chromium",
    "headless": false,
    "defaultViewport": {
        "width": 1920,
        "height": 1080
    },
    "downloadBehavior": {
        "policy": "allow",
        "downloadPath": "/tmp"
    }
}

Examples
#

Validate Recipes
#

To validate a recipe without executing it, run:

puppetchef recipe.yaml --syntax-check

Execute Recipes
#

To execute a recipe, run:

puppetchef recipe.yaml

For additional information or file output, set the logging environment variables on the same command line:

PUPPETCHEF_LOGLEVEL=debug PUPPETCHEF_LOGFILE=/tmp/puppetchef.log puppetchef recipe.yaml

Create Recipes
#

The template engine is based on Handlebars.js and supports env and json as helper functions.

url: "https://example.com"
name: "Login Test"
tasks:
  - name: "Login"
    steps:
      - puppetchef.builtin.common:
          command: "fill_out"
          selector: "#username"
          data: "testuser"
      - puppetchef.builtin.common:
          command: "fill_out"
          selector: "#password"
          data: "{{{ env 'PASS' }}}"
      - puppetchef.builtin.common:
          command: "click"
          selector: "#submit"

Create Your Own Plugin
#

A plugin is an ordinary npm package that exports one or more async functions (commands). Each command receives the Puppeteer page and the step’s data.

How Puppetchef Finds Plugins
#

Every step key has the form puppetchef.<package>.<file>. Puppetchef turns it into require("<package>/<file>"):

Step keyModule loaded
puppetchef.builtin.commonbuiltin/common.js, bundled with Puppetchef
puppetchef.my-puppetchef-plugin.actionsmy-puppetchef-plugin/actions.js, from your project’s node_modules

The command field of the step then selects which exported function to call. Because the key is split on dots, the package name must not contain a ..

Write the Plugin Package
#

Create a folder for your plugin, for example my-puppetchef-plugin/, with a package.json whose name is my-puppetchef-plugin and an actions.js file:

// my-puppetchef-plugin/actions.js

module.exports = {
  customAction: async (page, data = {}) => {
    // Custom automation logic
    await page.locator(data.selector);

    // If you need to return values for later use
    return { x: 20, y: "msg" };
  }
};

Install the Plugin in Your Project
#

Install the plugin in the same project as Puppetchef, so Node.js can resolve it from Puppetchef’s location. A global Puppetchef install will not find plugins installed in a project.

npm install puppetchef ./my-puppetchef-plugin

Use the Plugin in Your Recipes
#

name: Example Recipe
url: https://example.com/demo-url
tasks:
  - name: "Custom Action"
    steps:
      - puppetchef.my-puppetchef-plugin.actions:
          command: "customAction"
          selector: "#username"
        # Make the return value of customAction available to the following steps
        register: ret
      - puppetchef.builtin.common:
          command: debug
          format: "Debugging: {{ ret.y }}"
        # Conditionals for when to execute this step
        when: "{{ ret.x }} > 0"

Run it with npx puppetchef recipe.yaml from the project folder.