DEV Community

Mammoth WP
Mammoth WP

Posted on

Test only the pages your WordPress plugin owns, with a JSON list

Crawling every wp-admin menu page is a good smoke test. On a WooCommerce site it also means your plugin's test can fail on screens it doesn't own.

wp-self-test has a second browser mode for that: mmwp-e2e pages. You describe the pages you care about in browser-pages.json, admin and frontend, and it checks only those.

{
  "version": 1,
  "pages": [
    { "area": "admin", "name": "Settings", "path": "/wp-admin/admin.php?page=my-plugin", "as": "admin", "expect": "#my-plugin-form" },
    { "area": "admin", "name": "Product", "path": "/wp-admin/post.php?post=${PRODUCT_ID}&action=edit", "as": "admin", "expect_url": "action=edit" },
    { "area": "frontend", "name": "Shop", "path": "/shop/", "as": "guest", "expect": ".products" },
    { "area": "frontend", "name": "Old URL", "path": "/old/", "as": "guest", "status": 200, "expect_url": "/new/" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

What the keys do:

  • path and as are required. as: "admin" reuses the logged-in session; as: "guest" has no cookies.
  • ${PRODUCT_ID} comes from a --vars JSON file. An unknown variable fails that page with a clear message, instead of loading the wrong URL.
  • expect is a CSS selector that must exist, expect_url a substring the final URL must contain after redirects, expect_text text that must be visible on the page.
  • status is the expected HTTP status, 200 by default. A page you removed on purpose can expect 410 and still pass.

Every page gets a fresh browser context, so a cart or session from one page never leaks into the next. Each page also gets the same checks as the full crawl: PHP fatals, warnings and notices in the markup, critical-error pages, console errors, responses of 500 or more, and REST or admin-ajax responses of 400 or more.

Run it:

MMWP_E2E_ALLOW_PASSWORD_LOGIN=1 \
npx mmwp-e2e pages --url="$WP_SITE_URL" --file=browser-pages.json --vars=vars.json \
    --user=admin --password="$WP_ADMIN_PASSWORD" --json=mmwp-e2e-results.json --junit=junit-browser.xml
Enter fullscreen mode Exit fullscreen mode

It exits 1 when any page fails, and the JUnit file drops straight into a CI test report. A missing or invalid path or as stops the run before a browser starts.

Guide: https://mammothwp.com/guides/wordpress-plugin-self-test-wp-self-test/

Top comments (0)