Skip to content

Commit c49e829

Browse files
committed
fix: enhance Playwright test workflow and add wpCli helper for WP-CLI commands
1 parent 6e1b965 commit c49e829

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

.github/workflows/playwright-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ jobs:
204204
npx playwright install chromium
205205
206206
- name: Run Playwright tests
207+
env:
208+
WP_E2E_WPCLI_MODE: gh-actions-ci
209+
WP_E2E_WP_CONTAINER: ${{ job.services.wordpress.id }}
210+
WP_E2E_WPCLI_PHAR: /tmp/wp-cli.phar
211+
WP_E2E_WPCLI_URL: http://localhost:8888
207212
run: npm run test:playwright -- --project=${{ inputs.project-name }}
208213

209214
- name: Print WordPress logs on failure

tests/e2e/code-snippets-evaluation.spec.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import util from 'util'
2-
import { exec } from 'child_process'
31
import { expect, test } from '@playwright/test'
42
import { SnippetsTestHelper } from './helpers/SnippetsTestHelper'
53
import { SELECTORS } from './helpers/constants'
4+
import { wpCli } from './helpers/wpCli'
65
import type { Page } from '@playwright/test'
76

87
const TEST_SNIPPET_NAME = 'E2E Snippet Test'
@@ -34,27 +33,22 @@ const verifyShortcodeRendersCorrectly = async (
3433
}
3534

3635
const createPageWithShortcode = async (snippetId: string): Promise<string> => {
37-
const execAsync = util.promisify(exec)
38-
3936
const shortcode = `[code_snippet id=${snippetId} format name="${TEST_SNIPPET_NAME}"]`
4037
const pageContent = `<p>Page content before shortcode.</p>\n\n${shortcode}\n\n<p>Page content after shortcode.</p>`
4138

4239
try {
43-
const createPageCmd = [
44-
'npx wp-env run cli wp post create',
40+
const pageId = (await wpCli([
41+
'post',
42+
'create',
4543
'--post_type=page',
46-
'--post_title="Test Page for Snippet Shortcode"',
47-
`--post_content='${pageContent}'`,
44+
'--post_title=Test Page for Snippet Shortcode',
45+
`--post_content=${pageContent}`,
4846
'--post_status=publish',
4947
'--porcelain'
50-
].join(' ')
51-
52-
const { stdout } = await execAsync(createPageCmd)
53-
const pageId = stdout.trim()
48+
])).trim()
5449

55-
const getUrlCmd = `npx wp-env run cli wp post url ${pageId}`
56-
const { stdout: pageUrl } = await execAsync(getUrlCmd)
57-
return pageUrl.trim()
50+
const pageUrl = (await wpCli(['post', 'url', pageId])).trim()
51+
return pageUrl
5852
} catch (error) {
5953
console.error('Failed to create page via WP-CLI:', error)
6054
throw error

tests/e2e/helpers/wpCli.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import util from 'util'
2+
import { execFile } from 'child_process'
3+
4+
export interface WpCliOptions {
5+
url?: string
6+
}
7+
8+
const execFileAsync = util.promisify(execFile)
9+
10+
const hasArg = (args: string[], prefix: string): boolean =>
11+
args.some(arg => arg === prefix || arg.startsWith(`${prefix}=`))
12+
13+
export const wpCli = async (args: string[], options: WpCliOptions = {}): Promise<string> => {
14+
const mode = (process.env.WP_E2E_WPCLI_MODE ?? '').toLowerCase()
15+
const dockerContainer = process.env.WP_E2E_WP_CONTAINER
16+
17+
const url = options.url ?? process.env.WP_E2E_WPCLI_URL
18+
const urlArgs = url && !hasArg(args, '--url') ? [`--url=${url}`] : []
19+
20+
if (dockerContainer || 'gh-actions-ci' === mode) {
21+
if (!dockerContainer) {
22+
throw new Error('WP_E2E_WP_CONTAINER must be set when WP_E2E_WPCLI_MODE is gh-actions-ci.')
23+
}
24+
25+
const pharPath = process.env.WP_E2E_WPCLI_PHAR ?? '/tmp/wp-cli.phar'
26+
const allowRootArgs = hasArg(args, '--allow-root') ? [] : ['--allow-root']
27+
28+
const { stdout } = await execFileAsync('docker', [
29+
'exec',
30+
'-u',
31+
'root',
32+
'-w',
33+
'/var/www/html',
34+
dockerContainer,
35+
'php',
36+
pharPath,
37+
...urlArgs,
38+
...allowRootArgs,
39+
...args
40+
])
41+
42+
return stdout
43+
}
44+
45+
// Default to wp-env (local dev) for backwards compatibility.
46+
const { stdout } = await execFileAsync('npx', ['wp-env', 'run', 'cli', 'wp', ...urlArgs, ...args])
47+
48+
return stdout
49+
}

0 commit comments

Comments
 (0)