Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { getPlaywrightConfig } from '@wpsyntex/e2e-test-utils';
export default getPlaywrightConfig( {
globalSetup: './global.setup.mjs',
globalTeardown: './global.teardown.mjs',
// Required with wp-env 11+ when using a separate test config file:
wpCliCommand: 'npx wp-env --config=.wp-env.test.json run cli',
} );
```

Expand All @@ -52,6 +54,7 @@ The `getPlaywrightConfig` function accepts an options object that can override a

- `use`: Override default Playwright use options
- `webServer`: Override default webServer configuration
- `wpCliCommand`: Base `wp-env run …` command for WP-CLI helpers such as `createTranslator`. Defaults to `npx wp-env run tests-cli`. Can also be set via the `WP_ENV_CLI_COMMAND` environment variable.
- Any other Playwright configuration options

#### Default Configuration
Expand Down Expand Up @@ -194,6 +197,21 @@ Reads a file and returns its contents as a string.
- `filePath`: The file path to read
- **Returns:** String content of the file

#### 🔧 WP-CLI Utilities

##### `runWpCli( command, shellPrefix = '' )`

Runs a WP-CLI command in the wp-env test environment. Uses the command configured via `getPlaywrightConfig({ wpCliCommand })` or the `WP_ENV_CLI_COMMAND` environment variable.

- **Parameters:**
- `command`: The WP-CLI command to run (e.g. `wp pll setting get`)
- `shellPrefix`: Optional shell prefix (e.g. `echo "y" |`)
- **Returns:** The command output as a string

##### `getWpCliCommand()`

Returns the base `wp-env run …` command used by `runWpCli` and `createTranslator`. Configure it via `getPlaywrightConfig({ wpCliCommand })` or the `WP_ENV_CLI_COMMAND` environment variable.

#### 🔧 Additional Exports

##### `globalSetup`
Expand Down
26 changes: 15 additions & 11 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { defineConfig, devices } from '@playwright/test';
import path from 'path';
import { setWpCliCommand } from './wp-cli.js';

const STORAGE_STATE_PATH =
process.env.STORAGE_STATE_PATH ||
path.join( process.cwd(), 'artifacts/storage-states/admin.json' );

/**
* Returns a Playwright config object.
*
* @since 0.1.0
*
* @param {import('@playwright/test').PlaywrightTestConfig} options Options to merge with the default config.
* @return {import('@playwright/test').PlaywrightTestConfig} Playwright config object.
*/
export function getPlaywrightConfig( options = {} ) {
const {
wpCliCommand,
use: useOptions = {},
webServer: webServerOptions = {},
...playwrightOptions
} = options;

if ( wpCliCommand ) {
setWpCliCommand( wpCliCommand );
}

return defineConfig( {
testDir: './specs',
globalSetup: path.resolve( __dirname, '../setup/global.setup.cjs' ),
Expand All @@ -28,7 +32,7 @@ export function getPlaywrightConfig( options = {} ) {
trace: 'on-first-retry',
headless: true,
storageState: STORAGE_STATE_PATH,
...options.use,
...useOptions,
},
projects: [
{
Expand All @@ -43,8 +47,8 @@ export function getPlaywrightConfig( options = {} ) {
reuseExistingServer: true,
cwd: process.cwd(),
timeout: 120 * 1000,
...options.webServer,
...webServerOptions,
},
...options,
...playwrightOptions,
} );
}
51 changes: 51 additions & 0 deletions src/config/wp-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { execSync } from 'child_process';

/** @type {string} */
const DEFAULT_WP_CLI_COMMAND = 'npx wp-env run tests-cli';

/**
* Returns the base `wp-env run …` command used to invoke WP-CLI.
*
* Resolution order:
* 1. `process.env.WP_ENV_CLI_COMMAND`
* 2. {@link DEFAULT_WP_CLI_COMMAND}
*
* @since 0.2.0
*
* @return {string} Base wp-env run command (without trailing WP-CLI arguments).
*/
export function getWpCliCommand() {
return process.env.WP_ENV_CLI_COMMAND || DEFAULT_WP_CLI_COMMAND;
}

/**
* Sets the base `wp-env run …` command for utility functions such as `createTranslator`.
*
* @since 0.2.0
*
* @param {string} command Base wp-env run command (without trailing WP-CLI arguments).
*/
export function setWpCliCommand( command ) {
process.env.WP_ENV_CLI_COMMAND = command;
}

/**
* Runs a WP-CLI command in the wp-env test environment.
*
* @since 0.2.0
*
* @param {string} command The WP-CLI command to run (e.g. `wp pll setting get`).
* @param {string} shellPrefix Optional shell prefix (e.g. `echo "y" |`).
* @return {string} The command output.
*/
export function runWpCli( command, shellPrefix = '' ) {
const cliCommand = `${ getWpCliCommand() } ${ command }`;
const fullCommand = shellPrefix
? `${ shellPrefix } ${ cliCommand }`
: cliCommand;

return execSync( fullCommand, {
encoding: 'utf8',
stdio: 'pipe',
} );
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { getPlaywrightConfig } from './config/index.js';
export { getWpCliCommand, runWpCli } from './config/wp-cli.js';
export { default as globalSetup } from './setup/global.setup.js';
export {
getAllLanguages,
Expand Down
12 changes: 4 additions & 8 deletions src/users/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { execSync } from 'child_process';
import { runWpCli } from '../config/wp-cli.js';

/**
* @typedef {import('@playwright/test').Page} Page
Expand All @@ -25,18 +25,14 @@ export async function createTranslator( langSlugs, userName = '' ) {
: userName;
const email = `${ userName.toLowerCase() }@example.com`;
const userId = parseInt(
execSync(
`npx wp-env run tests-cli wp user create ${ userName } ${ email } --role=editor --user_pass=password --porcelain`,
{ encoding: 'utf8' }
runWpCli(
`wp user create ${ userName } ${ email } --role=editor --user_pass=password --porcelain`
).trim(),
10
);

langSlugs.forEach( ( langSlug ) => {
execSync(
`npx wp-env run tests-cli wp user add-cap ${ userId } translate_${ langSlug }`,
{ encoding: 'utf8' }
);
runWpCli( `wp user add-cap ${ userId } translate_${ langSlug }` );
} );

return { id: userId, username: userName, password: 'password' };
Expand Down
Loading