Test WordPress and WooCommerce functionality with GitHub actions

When I found the Reflect.run test automation SaaS tool years ago, they had a free tier that allowed you to visually construct website functionality tests and run them on a regular basis.

I used them to test our newspaper website’s basic functionality that went beyond a “does the page load” check: are articles paywalled appropriately, can you load and submit our contact form, if you have a subscription and log in can you see articles and our print archives, etc. It was pretty slick:

Then the service removed their free tier and my budget didn’t support their asking price, so we didn’t have those tests for a while. A recent bug caused by plugin update reminded me of their usefulness and importance, so I revisited the issue.

I’ve known about Playwright for a while now but have never worked with it directly. Claude helped me think through implementation details and we arrived at a plan for a smoke suite run on GitHub actions. It works pretty well!

Running 5 tests using 1 worker
  ✓  1 [chromium] › tests/wp-contact-form.spec.ts:8:9 › WWN WP: contact form › WWN WP: contact page loads and empty submit shows validation errors (24.5s)
  ✘  2 [chromium] › tests/wp-gift-subscription-cart.spec.ts:12:9 › WWN WP: gift subscription › WWN WP: gift subscription can be added to cart and reaches checkout (24.8s)
  ✓  3 [chromium] › tests/wp-gift-subscription-cart.spec.ts:12:9 › WWN WP: gift subscription › WWN WP: gift subscription can be added to cart and reaches checkout (retry #1) (25.1s)
  ✘  4 [chromium] › tests/wp-metered-content-login.spec.ts:7:9 › WWN WP: metered content › WWN WP: metered paywall gate appears, then login unlocks article (1.2m)
  ✓  5 [chromium] › tests/wp-metered-content-login.spec.ts:7:9 › WWN WP: metered content › WWN WP: metered paywall gate appears, then login unlocks article (retry #1) (58.9s)
  ✓  6 [chromium] › tests/wp-online-access-account.spec.ts:11:9 › WWN WP: online access and account › WWN WP: print edition requires login, subscriber can open an issue and view account (58.0s)
  ✓  7 [chromium] › tests/wp-subscribe-checkout.spec.ts:8:9 › WWN WP: subscribe to checkout › WWN WP: subscription can be added to cart and reaches checkout (17.0s)

First, I set up Playwright (Typescript) test definitions for each thing I wanted to check. For example, here’s the one that tests our contact form:

import { test, expect } from './support/fixtures';
import { NAV, humanPause, assertNotChallenged } from './support/wp';

test.describe('WWN WP: contact form', () => {
    test('WWN WP: contact page loads and empty submit shows validation errors', async ({ page }) => {
        await test.step('Home page -> Contact Us', async () => {
            const response = await page.goto('/', NAV);
            await assertNotChallenged(page);
            expect(response?.ok(), 'WWN WordPress: home page did not load').toBeTruthy();
            await humanPause(page);

            // Contact Us appears in the header, the secondary nav and the footer, plus a hidden mobile copy.
            // Scope to the desktop secondary nav and take the visible one.
            await page
                .locator('#secondary-nav-contain')
                .getByRole('link', { name: /^contact us$/i })
                .click();
            await expect(page).toHaveURL(/\/contact\/?$/);
            await assertNotChallenged(page);
        });

        await test.step('Form is displayed', async () => {
            await expect(page.getByText('Your email address (required)')).toBeVisible();
        });

        await test.step('Empty submit shows validation errors', async () => {
            await humanPause(page);
            await page.locator('#csbutton').click();

            // Inline per-field tips are what users see. Scope to them to avoid the hidden SR summary list.
            const tips = page.locator('.wpcf7-not-valid-tip');
            await expect(tips.first()).toBeVisible({ timeout: 20_000 });
            await expect(tips.filter({ hasText: 'The field is required.' }).first()).toBeVisible();

            // Three required fields on this form: name, email, message.
            await expect(tips).toHaveCount(3);

            // The form-level message is aria-hidden, so assert text rather than visibility.
            await expect(page.locator('.wpcf7-response-output')).toContainText(
                'One or more fields have an error. Please check and try again.',
            );
        });
    });
});

You’ll notice some logic around human-like pauses and detecting whether Cloudflare challenges appear. The Cloudflare problem (love you, Cloudflare, kisses) evolved into the need to try to bypass it in a more comprehensive way for these tests. So, at the Cloudflare domain level, there’s a WAF rule that bypasses all bot detection when a given header and secret value is present in the request.

import { test as base, expect } from '@playwright/test';

const SITE_HOST = new URL(process.env.WWN_WP_BASE_URL ?? 'https://oursite.com').hostname;

export const test = base.extend({
    context: async ({ context }, use) => {
        const secret = process.env.OUR_SECRET;
        if (secret) {
            await context.route(
                (url) => url.hostname === SITE_HOST,
                (route) =>
                    route.continue({
                        headers: { ...route.request().headers(), 'X-Our-Special-Header': secret },
                    }),
            );
        }
        await use(context);
    },
});

export { expect };

Once the tests were written, the GitHub action workflow file pulls it all together:

name: WWN WordPress Monitor

permissions:
  contents: read

on:
  schedule:
    - cron: '30 9 * * *'
  workflow_dispatch:

jobs:
  wordpress-tests:
    name: WWN WordPress site tests
    runs-on: ubuntu-latest
    timeout-minutes: 15
    env:
      OUR_USER: ${{ secrets.OUR_USER }}
      OUR_PASS: ${{ secrets.OUR_PASS }}
      OUR_SECRET: ${{ secrets.OUR_SECRET }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      - run: npm ci

      - run: npx playwright install --with-deps chromium

      - name: Run Playwright tests
        run: npx playwright test

      - name: Annotate failure for Nagios
        if: failure()
        run: |
          titles=$(jq -r '[.. | objects | select(has("specs")) | .specs[] | select(.ok == false) | .title] | unique | join(", ")' test-results/results.json 2>/dev/null || true)
          msg="WWN WordPress: failed - ${titles:-see run logs}"
          echo "::error title=WWN WordPress::${msg:0:200}"
      - name: Upload reports
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: wwn-wordpress-monitor-reports
          path: |
            playwright-report
            test-results
          retention-days: 14

As it says, the workflow annotates what happens in each run to be useful for monitoring by Nagios, which we use to know when things are broken in our small business infrastructure.

So, then a “simple” Nagios custom check script can ask the GitHub API how things went in the latest run:

#!/usr/bin/env bash
# Nagios plugin: WWN WordPress Monitor (GitHub Actions workflow status, pull model).
# Deps: bash, curl, jq. Exit: 0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN.

PREFIX="WWN WordPress:"
API="https://api.github.com"
REPO=""; WORKFLOW="wordpress-monitor.yml"; TOKEN=""
MAX_AGE=93600; BRANCH="main"; EXPIRE_DAYS=14

usage() {
  cat <<USAGE
Usage: $0 -r owner/repo -t token [-w workflow_file] [-a max_age_seconds] [-b branch] [-e days] [-h]
  -r  owner/repo (required)
  -t  GitHub fine-grained PAT (required, probably from a Nagios $USER var)
  -w  workflow file (default: $WORKFLOW)
  -a  max age in seconds of last completed run (default: $MAX_AGE)
  -b  branch (default: $BRANCH)
  -e  warn if token expires within this many days (default: $EXPIRE_DAYS)
  -h  this help
USAGE
}

unknown() { echo "$PREFIX UNKNOWN - $1"; exit 3; }

while getopts "r:w:t:a:b:e:h" opt; do
  case $opt in
    r) REPO=$OPTARG ;; w) WORKFLOW=$OPTARG ;; t) TOKEN=$OPTARG ;;
    a) MAX_AGE=$OPTARG ;; b) BRANCH=$OPTARG ;; e) EXPIRE_DAYS=$OPTARG ;;
    h) usage; exit 3 ;;
    *) usage >&2; exit 3 ;;
  esac
done

[ -n "$REPO" ] || unknown "missing required option -r owner/repo"
[ -n "$TOKEN" ] || unknown "missing required option -t token"
[[ "$MAX_AGE" =~ ^[0-9]+$ && "$EXPIRE_DAYS" =~ ^[0-9]+$ ]] || unknown "-a and -e must be integers"
command -v curl >/dev/null 2>&1 || unknown "curl not installed"
command -v jq >/dev/null 2>&1 || unknown "jq not installed"

HDRS=$(mktemp) ; BODY=$(mktemp)
trap 'rm -f "$HDRS" "$BODY"' EXIT

# gh_get <path-and-query>; sets HTTP_CODE; body in $BODY, headers in $HDRS.
# Auth header goes through a curl config on stdin so it is not in the process list.
gh_get() {
  HTTP_CODE=$(printf 'header = "Authorization: Bearer %s"\n' "$TOKEN" | curl -sS -K - \
    --max-time 20 -o "$BODY" -D "$HDRS" -w '%{http_code}' \
    -H "Accept: application/vnd.github+json" \
    -H "X-GitHub-Api-Version: 2022-11-28" \
    -H "User-Agent: check_wwn_wordpress_monitor" \
    "$API$1" 2>/dev/null) || { HTTP_CODE=000; return 1; }
}

human() { # seconds -> 5h12m / 2h / 14m / 30s
  local s=$1 d h m
  d=$((s/86400)); h=$(((s%86400)/3600)); m=$(((s%3600)/60))
  if [ "$d" -gt 0 ]; then echo "${d}d${h}h"
  elif [ "$h" -gt 0 ]; then if [ "$m" -gt 0 ]; then echo "${h}h${m}m"; else echo "${h}h"; fi
  elif [ "$m" -gt 0 ]; then echo "${m}m"
  else echo "${s}s"; fi
}

gh_get "/repos/$REPO/actions/workflows/$WORKFLOW/runs?branch=$BRANCH&status=completed&per_page=1&exclude_pull_requests=true" \
  || unknown "network error or timeout contacting api.github.com"

case $HTTP_CODE in
  200) ;;
  401) unknown "HTTP 401: token expired or revoked, or lacks access to the repo" ;;
  403|429)
    if grep -qi '^x-ratelimit-remaining: 0' "$HDRS" || jq -e '.message|test("rate limit";"i")' "$BODY" >/dev/null 2>&1; then
      unknown "GitHub API rate limit hit"
    fi
    unknown "HTTP $HTTP_CODE: token lacks permission (needs Actions: read) or org approval pending" ;;
  404) unknown "HTTP 404: repo or workflow not found, or token lacks access (check -r, -w, token scope)" ;;
  *) unknown "unexpected HTTP status $HTTP_CODE from GitHub" ;;
esac

jq -e '.workflow_runs|type=="array"' "$BODY" >/dev/null 2>&1 || unknown "malformed JSON from GitHub"
[ "$(jq '.workflow_runs|length' "$BODY")" -gt 0 ] || unknown "no completed runs found for $WORKFLOW on branch $BRANCH"

RUN=$(jq -c '.workflow_runs[0]' "$BODY")
IFS=$'\t' read -r RUN_ID RUN_NUM CONCLUSION URL AGE < <(jq -r --argjson now "$(date +%s)" \
  '[.id,.run_number,(.conclusion//"unknown"),.html_url,($now - (.updated_at|fromdateiso8601))]|@tsv' <<<"$RUN" 2>/dev/null) \
  || unknown "malformed run data from GitHub"
[[ "$AGE" =~ ^-?[0-9]+$ && -n "$RUN_ID" ]] || unknown "malformed run data from GitHub"
[ "$AGE" -lt 0 ] && AGE=0

# Token expiry (header present only for fine-grained PATs with expiry)
EXP_NOTE=""; EXP_WARN=0
EXP_HDR=$(grep -i '^github-authentication-token-expiration:' "$HDRS" | head -1 | cut -d: -f2- | tr -d '\r' | sed 's/^ *//')
if [ -n "$EXP_HDR" ]; then
  # Format: "2026-11-01 12:00:00 UTC"
  EXP_ISO=$(sed -E 's/^([0-9-]+) ([0-9:]+).*/\1T\2Z/' <<<"$EXP_HDR")
  EXP_EPOCH=$(jq -rn --arg t "$EXP_ISO" '$t|fromdateiso8601' 2>/dev/null)
  if [[ "$EXP_EPOCH" =~ ^[0-9]+$ ]]; then
    LEFT=$((EXP_EPOCH - $(date +%s)))
    if [ "$LEFT" -lt $((EXPIRE_DAYS*86400)) ]; then
      EXP_WARN=1; EXP_NOTE="token expires ${EXP_HDR%% *}"
    fi
  fi
fi

AGE_H=$(human "$AGE")
PERF="| age=${AGE}s;${MAX_AGE};;"
finish() { # state code, message
  local msg=$2
  [ -n "$EXP_NOTE" ] && msg="$msg (WARNING: $EXP_NOTE)"
  echo "$PREFIX $1 - $msg $PERF"; exit "$3"
}

if [ "$AGE" -gt "$MAX_AGE" ]; then
  finish CRITICAL "stale: last completed run was $AGE_H ago" 2
fi

case $CONCLUSION in
  success)
    if [ "$EXP_WARN" -eq 1 ]; then
      finish WARNING "last run passed $AGE_H ago (run #$RUN_NUM) but $EXP_NOTE" 1
    fi
    finish OK "last run passed $AGE_H ago (run #$RUN_NUM)" 0 ;;
  failure|timed_out|startup_failure)
    DETAIL=""
    if gh_get "/repos/$REPO/actions/runs/$RUN_ID/jobs?per_page=100" && [ "$HTTP_CODE" = 200 ]; then
      JOBINFO=$(jq -r '[.jobs[]?|select(.conclusion=="failure" or .conclusion=="timed_out")][0]
        | if .==null then empty else [.id,.name,([.steps[]?|select(.conclusion=="failure" or .conclusion=="timed_out")][0].name//"")]|@tsv end' "$BODY" 2>/dev/null)
      if [ -n "$JOBINFO" ]; then
        IFS=$'\t' read -r JOB_ID JOB_NAME STEP_NAME <<<"$JOBINFO"
        if [ -n "$STEP_NAME" ]; then DETAIL=" at step '$STEP_NAME'"; else DETAIL=" in job '$JOB_NAME'"; fi
        if gh_get "/repos/$REPO/check-runs/$JOB_ID/annotations" && [ "$HTTP_CODE" = 200 ]; then
          ANN=$(jq -r '[.[]?|select((.message//"")|startswith("WWN WordPress:"))][0].message//empty' "$BODY" 2>/dev/null | head -c 200)
          [ -n "$ANN" ] && DETAIL="$DETAIL: $ANN"
        fi
      fi
    fi
    finish CRITICAL "last run ${CONCLUSION/failure/failed} $AGE_H ago$DETAIL $URL" 2 ;;
  cancelled|skipped|neutral|action_required|stale)
    finish WARNING "last run $CONCLUSION $AGE_H ago (run #$RUN_NUM) $URL" 1 ;;
  *)
    unknown "unrecognized run conclusion '$CONCLUSION'" ;;
esac

Then Nagios can alert me if things are falling apart, or if things have recovered after problems, e.g. via Pushover:

And if there is a problem that needs investigating, the “artifact” available in the GitHub action run details is a zipfile of the Playwright run report, a self-contained HTML and image bundle with all the details and screenshots. Amazing.

The Cloudflare bypass is not perfect, it seems some of GitHub’s action infrastructure IPs trigger “are you human” checks with Cloudflare no matter what. And the run timing seems to be a bit inconsistent (based on whether GitHub actions are having issues, I think) so the monitoring we’re doing has had some false alarms for delayed runs. But otherwise, I’m pretty happy with it.

Using Nagios for small business infrastructure monitoring

There are a million tools out there for monitoring services and measuring application performance, and I’m sure many of them are awesome.

When I went looking for a good service and infrastructure monitoring solution for our community newspaper’s one-person IT department (me), I came back to an open source tool that I’d probably first started using over 20 years ago: Nagios.

It works great for us and only costs $4/month to self-host. I thought I’d share about how we’re using it in case others might find it helpful.

My needs were pretty straightforward.

I wanted to monitor 10 or so servers and network devices and various services hosted on them, the health of 20 or so web applications, and the status pages of about 20 external services/platforms we depend on.

When something gets slow or stops responding, I wanted to know about it in a reliable way.

We’d used Nagios extensively at the website development and hosting business I ran a long time ago, and it was great for this kind of thing. Highly configurable and flexible, low maintenance, and very reliable.

When looking at options more recently, Nagios didn’t seem to have changed much in all that time, and with its outdated web interface and arcane config file syntax, I convinced myself that someone had surely created a better tool by now. So, I spent a bunch of time researching alternatives, including Zabbix, Prometheus, Grafana, Datadog, New Relic, Pingdom, UptimeRobot, and a bunch of others. And we did start out using UptimeRobot for a brief while.

But when I looked at the amount of time I was going to spend learning and managing the complexity of those stacks, or the amount of money we would spend on SaaS tools along with some of the constraints and “per seat” or “per monitor” limits they imposed, the more I was ready to go with a tried and true, purpose-built option that could scale up without additional cost or complexity.

Nagios is not fun to set up initially — see my earlier comment about arcane config syntax. It probably took me the equivalent of a full day to get everything just so. But once I did, it’s been humming along great, and brings significant peace of mind.

We’re currently monitoring 54 hosts and 104 services. This is all on a $4/month droplet at Digital Ocean, 1 vCPU, 512 MB RAM, 10 GB Disk. We pay another $0.80/month for weekly droplet snapshots. We set our monitoring droplet up in a different physical region from the rest of our servers/droplets, on the other side of the continent.

Continue reading Using Nagios for small business infrastructure monitoring

Quick hack to monitor the health of a collection of AWS services

My SaaS app WP Lookout uses Amazon Web Services for hosting. I’ve had plenty of monitoring set up using both internal and external tools notifications, and these mostly output to a combination of a dedicated Slack channel and/or a Pushover notification that goes to my mobile device.

The one thing I didn’t have in place until recently was monitoring for the health of the specific group of AWS services the app depends on. With AWS offering many services spread across many parts of the world (as illustrated in the Very Long Service Health Dashboard), I wasn’t about to start monitoring all of the AWS infrastructure just to see if my particular app was affected.

They do offer a “Personal Health Dashboard” that’s supposedly more tailored to incidents affecting my AWS account, but I find it unintuitive and confusing to use, especially when it comes to setting up notifications to external services like Slack. In my online searching I found various services and tools that supposedly integrated the PHD into more standard monitoring and alerting options, but they were also either too complicated or seemed to require additional levels of paid AWS support plans that I didn’t want to take on.

So I decided to keep things simple and use the tried and true technology for polling information about external resources: RSS!

Continue reading Quick hack to monitor the health of a collection of AWS services

Getting alerts about outdated Time Machine backups

Our household uses Apple’s Time Machine backup system as one of our backup methods. Various Macs are set to run their backups at night (we use TimeMachineEditor to schedule backups so they don’t take up local bandwidth or computing resources during the day) and everything goes to a Synology NAS (basically following the steps in this great guide from 9to5Mac).

But, Time Machine doesn’t necessarily complete a successful backup for every machine every night. Maybe the Mac is turned off or traveling away from home. Maybe the backup was interrupted for some reason. Maybe the backup destination isn’t on or working correctly. Most of these conditions can self-correct within a day or two.

But, I wanted to have a way to be notified if any given Mac had not successfully completed a backup after a few days so that I could take a look. If, say, three days go by without a successful backup, I start to get nervous. I also didn’t want to rely on any given Mac’s human owner/user having to notice this issue and remember to tell me about it.

I decided to handle this by having a local script on each machine send a daily backup status to a centralized database, accomplished through a new endpoint on my custom webhook server. The webhook call stores the status update locally in a database table. Another script runs daily to make sure every machine is current, and sends a warning if anyone is running behind.

Here are the details in case it helps anyone else.

Continue reading Getting alerts about outdated Time Machine backups

Monitoring WordPress events and status with a custom API endpoint

Let’s say your WordPress site has some set of custom functionality that is important to the overall operation of the site, and you want to know right away if it’s not working as expected, even if the site is otherwise “up” and working fine. There could be anywhere from 0 to many things needing attention at any given time, and you don’t want to receive a flood of emails or Slack pings that you have to sort through, you just want a single alert that things are off track, and another notice when they’re back to being in good shape.

I recently handled this case using transients, a custom REST API endpoint, and the service UptimeRobot. The context for me was a set of functions that regularly retrieve information from a variety of third-party sources; most of the time it goes fine, but between network issues, changes in third-party API endpoints or HTML source code and other possible errors, occasionally these functions would break and need updating.

First, I established an error function that was called any time some aspect of my site’s custom functionality encountered a problem that might need my attention.

public static function record_event_fetch_failure( $source = null, $message = null ) {

	if ( empty( $source ) ) {
		return false;
	}

	$source         = esc_attr( $source );
	$transient_name = 'event_fetch_failure_' . $source;

	$failure_data = array(
		'count'                => 1,
		'last_error_timestamp' => gmdate( 'Y-m-d H:i:sP', time() ),
		'last_error_message'   => esc_html( $message ),
	);

	// If the transient is already there, update it.
	$event_failure_counter = get_transient( $transient_name );
	if ( false !== $event_failure_counter ) {
		$failure_data['count'] = $event_failure_counter['count'] + 1;
	}
	set_transient( $transient_name, $failure_data, 24 * HOUR_IN_SECONDS );

}

When called, this function increases the counter of the number of errors interacting with a passed third party data source, storing that counter in a transient. As my custom functions run, any failures will be recorded for up to 24 hours. If there are no additional failures to increase the counter and extend the transient expiration time, then the failure data will go away with the assumption that things are back to normal now. (You may need to adjust these assumptions for your use case.)

Then, I create a REST API endpoint on the site that allows me to monitor that failure data externally.

add_action( 'rest_api_init', function() {
			register_rest_route(
				'mysite/v1',
				'/event_fetch_status',
				array(
					'methods'  => 'GET',
					'callback' => array( $this, 'mysite_event_fetch_status' ),
				)
			);
		} );

And then a callback function to determine the content of that API endpoint:

public static function mysite_event_fetch_status() {

	$error_count = 0;

	foreach ( array( 'facebook', 'eventbrite', 'googlecal' ) as $event_source ) {
		$fail_data = get_transient( 'event_fetch_failure_' . $event_source );
		if ( false !== $fail_data ) {
			$error_count += $fail_data['count'];
		}
	}

	if ( 0 < $error_count ) {
		echo sprintf( 'There have been %d recent event fetch errors.', (int) $error_count );
	} else {
		echo 'OK';
	}
}

Now, I have a REST API endpoint available at https://example.com/wp-json/mysite/v1/event_fetch_status that will either return OK if there have been no recent problems, or an error message with a count of recent issues. I could expand that output to include more detail about which third-party services are having issues and what those issues are, but for the purposes of a red versus green monitoring setup, the basics are fine and I can look into the details when I investigate.

Finally, I set up a monitor in UptimeRobot to check that endpoint on a regular basis and notify me if there’s a problem:

UptimeRobot monitor screenshot
UptimeRobot monitor screenshot

Just for good measure, I also create an admin notice in the WordPress admin area with a little more detail about what is failing:

public static function mysite_event_fetch_admin_notice() {

	$error_count    = 0;
	$error_messages = array();

	foreach ( array( 'facebook', 'eventbrite', 'googlecal' ) as $event_source ) {
		$fail_data = get_transient( 'event_fetch_failure_' . $event_source );
		if ( false !== $fail_data ) {
			$error_count     += $fail_data['count'];
			$error_messages[] = $fail_data['last_error_timestamp'] . ': ' . $fail_data['last_error_message'];
		}
	}

	if ( 0 < $error_count ) {
		echo '<div class="notice notice-warning">';
		echo sprintf( '<p>There have been %d recent event fetch errors.</p>', (int) $error_count );
		echo '<ul>';
		foreach ( $error_messages as $message ) {
			echo sprintf( '<li>%s</li>', esc_html( $message ) );
		}
		echo '</ul>';
		echo '</div>';
	}
}

add_action( 'admin_notices', array( $this, 'mysite_event_fetch_admin_notice' ) );

All put together, I will now receive alerts as configured in UptimeRobot when my custom functions have issues.

You could go the typical route of generating an email or Slack message about each problem, but in my experience this can quickly create a lot of one-off monitoring and alerting configurations in your life, and that can lead to you missing important information or being desensitized to the notices. Instead, I find it’s worth trying to manage all of my time-sensitive notifications across all of my various projects and services in one place where possible, and UptimeRobot or similar services offer a lot of flexibility for that.