Skip to content

Writing Tests

The important part of writing a test is to use the Test class shipped with this library (that extends Playwright's normal Test class):

import { test, expect } from '@packages/playwright-drupal';

This will trigger the setup and teardown of the separate Drupal site.

If you have a test that you don't want to run this way, import test and expect from @playwright/test as normal.

Recording Tests in VS Code

The VS Code "Record new" command generates absolute URLs and default imports. To keep URLs relative and use @lullabot/playwright-drupal helpers, record at cursor into a test file.

  1. Create a test from the following template:

    // test/playwright/tests/test1.spec.ts
    import { test, expect, execDrushInTestSite } from '@packages/playwright-drupal';
    
    // 1. Update the test name.
    test('new test', async ({ page }) => {
      // 2. Keep paths relative; "Record new" would supply the absolute URL.
      await page.goto('/');
      // 3. Place cursor here, then use "Record at cursor"
    });
    

  2. In the VS Code Testing sidebar, under Playwright:

    • Scroll to and toggle the correct configs (gear icon) pointing to your project's test/playwright/playwright.config.ts.
    • Use Record at cursor to append steps into the template. (If the recorder adds an extra absolute page.goto('http://…'), change it to keep them relative /.)

This keeps tests portable across DDEV and CI, leverages use.baseURL, and ensures Lullabot helpers are available.

Running Drush in Tests

There's many good reasons to want to run Drush in a test. For example, to scaffold out test data, or to turn on testing-related modules.

To run Drush during a test, use execDrushInTestSite as shown in the example test. This ensures that Drush bootstraps against the test site, and not the default site.

There may be times you want to run Drush once, globally before all tests. In that case, add a playwright:install:hook task to your Taskfile, and from there you can call Drush or anything else you may need to do during setup.