Loading...
Loading...
Use when working with WeChat mini-program automation (mini-program automation, automated testing, E2E) via miniprogram-automator, especially for standalone Node scripts or Jest tests involving DevTools launch/connect, page navigation, waitFor, custom-component selectors, wx method mocking, console or exception listeners, screenshots, regression checks, or troubleshooting launch failures, connection timeouts, and element-not-found issues.
npx skill4agent add whinc/wechat-miniprogram-skills miniprogram-automationminiprogram-automatorprojectPathautomator.launch({ projectPath })project.config.jsonapp.jsondist/build/miniprogram//Applications/wechatwebdevtools.app/Contents/MacOS/cliC:\Program Files (x86)\Tencent\微信web开发者工具\cli.batcliPath0.12.00.5.xcli --auto <path>[error] { code: 31, message: "Error: Missing param 'project / appid'" }npm install miniprogram-automator@latest --save-dev0.12.xcli auto --project <path> --auto-port <port>automator.launch()/pages/home/indexreLaunchnavigateToswitchTablaunchconnect| Scenario | Recommended Method |
|---|---|
| Fully automated: Script starts DevTools itself | |
| DevTools is already running, connect directly | CLI v2 + |
| DevTools is running and automation port is ready | Direct |
automator.launch()Failed to launch wechat web devToolstimeout120000miniProgram = await automator.launch({
cliPath: CLI_PATH,
projectPath: PROJECT_PATH,
timeout: 120000,
})automator.connect()connect()actually still uses the old CLI format (automator.launch()) internally, which may fail on some new versions of DevTools. Ifcli --auto <path>reports an error, switching to this method is usually more stable.launch()
const { spawnSync } = require('node:child_process')
const CLI_PATH = process.env.WECHAT_DEVTOOLS_CLI ||
'/Applications/wechatwebdevtools.app/Contents/MacOS/cli'
const PROJECT_PATH = process.env.MINIPROGRAM_PROJECT_PATH || '/path/to/project'
const AUTO_PORT = Number(process.env.WECHAT_AUTO_PORT || 9420)
// Step 1: Enable automation ws port with CLI v2
// If the DevTools HTTP service port is not the default value, pass it via --port
function enableAutomation(httpPort) {
const args = ['auto', '--project', PROJECT_PATH, '--auto-port', String(AUTO_PORT)]
if (httpPort) args.push('--port', String(httpPort))
const result = spawnSync(CLI_PATH, args, { encoding: 'utf8', timeout: 20000 })
return { success: result.status === 0, output: (result.stdout || '') + (result.stderr || '') }
}
const { success, output } = enableAutomation()
if (!success) throw new Error(`CLI auto command failed:\n${output}`)
// Step 2: Connect to the automation ws endpoint
const miniProgram = await automator.connect({
wsEndpoint: `ws://127.0.0.1:${AUTO_PORT}`,
})--portautomator.connect()const miniProgram = await automator.connect({
wsEndpoint: 'ws://localhost:9420',
})miniProgram.close()connect()| Scenario | Recommendation |
|---|---|
| Temporary verification, single-page debugging, script tooling | Standalone Node.js script |
| Regression testing, multiple cases, team collaboration, CI | Jest |
await page.waitFor('stable selector')await page.waitFor(async () => condition is met)await page.waitFor(number of milliseconds)sleep(2000)waitForpage.waitFor()await page.waitFor('.page-title')
await page.waitFor(async () => (await page.$$('.loaded-item')).length > 0)
await page.waitFor(500)trueconst input = await page.$('form-panel input')const panel = await page.$('form-panel')
const input = await panel.$('input')page.$page.$$form-panel inputelement.$element.$$value()property('value')inputtextareaconst value = await input.value()
// or
const value2 = await input.property('value')const data = await panel.data()data()mockWxMethodwx.requestwx.getLocationawait miniProgram.mockWxMethod('request', (options = {}) => {
const res = {
data: { code: 0, list: [{ id: 1, title: 'Mock Item A' }] },
statusCode: 200,
header: { 'content-type': 'application/json' },
cookies: [],
errMsg: 'request:ok',
}
Promise.resolve().then(() => {
if (typeof options.success === 'function') options.success(res)
if (typeof options.complete === 'function') options.complete(res)
})
return {
abort() {},
onHeadersReceived() {},
offHeadersReceived() {},
onChunkReceived() {},
offChunkReceived() {},
}
})await miniProgram.restoreWxMethod('request')mockWxMethodthis.originevaluate()mockWxMethodminiProgram.screenshot()await miniProgram.screenshot({ path: '/abs/path/to/file.png' }){ path }finallytry {
// Execute test logic
} finally {
await miniProgram.restoreWxMethod('request').catch(() => {})
await miniProgram.close().catch(() => {})
}const automator = require('miniprogram-automator')
const path = require('node:path')
const fs = require('node:fs/promises')
const CLI_PATH = process.env.WECHAT_DEVTOOLS_CLI || '/Applications/wechatwebdevtools.app/Contents/MacOS/cli'
const PROJECT_PATH = process.env.MINIPROGRAM_PROJECT_PATH || '/absolute/path/to/devtools-project'
const TARGET_PAGE = '/pages/home/index' // Must start with /
const OUTPUT_DIR = path.resolve(process.cwd(), 'outputs')
async function main() {
let miniProgram
try {
await fs.mkdir(OUTPUT_DIR, { recursive: true })
// Prerequisite: DevTools is completely closed (Cmd+Q); first run requires confirming trust project in the interface
miniProgram = await automator.launch({
cliPath: CLI_PATH,
projectPath: PROJECT_PATH,
timeout: 120000, // Leave enough time for first launch + user trust confirmation
})
const page = await miniProgram.reLaunch(TARGET_PAGE)
// Prioritize stable selectors for main synchronization, fixed waiting only for short fallback
await page.waitFor('.page-title')
await page.waitFor(100)
const title = await page.$('.page-title')
const titleText = await title.text()
console.log('Current title: ', titleText)
await miniProgram.screenshot({
path: path.join(OUTPUT_DIR, 'current-page.png'),
})
console.log('Screenshot completed')
} finally {
if (miniProgram) {
await miniProgram.close()
}
}
}
main().catch((error) => {
console.error(error)
process.exit(1)
})const automator = require('miniprogram-automator')
async function run() {
let miniProgram
try {
miniProgram = await automator.launch({
cliPath: process.env.WECHAT_DEVTOOLS_CLI || '/Applications/wechatwebdevtools.app/Contents/MacOS/cli',
projectPath: process.env.MINIPROGRAM_PROJECT_PATH || '/absolute/path/to/devtools-project',
})
const page = await miniProgram.reLaunch('/pages/form/index')
await page.waitFor(300)
const panel = await page.$('form-panel')
if (!panel) throw new Error('form-panel not found')
const input = await panel.$('input')
if (!input) throw new Error('Input inside component not found')
await input.tap()
await input.input('13800138000')
await page.waitFor(100)
const value = await input.value()
if (value !== '13800138000') {
throw new Error(`Input value does not match expectation: ${value}`)
}
// panel must be a custom component instance; native components or ordinary elements are not suitable for reading internal state with data()
const panelData = await panel.data().catch(() => null)
console.log('panel.data() =', panelData)
} finally {
if (miniProgram) await miniProgram.close()
}
}
run().catch((error) => {
console.error(error)
process.exit(1)
})page.$value()property('value')data()wx.requestconsole/exceptionloadingconsolepackageAPI/pages/network/request/requestconst automator = require('miniprogram-automator')
async function run() {
let miniProgram
const consoleEvents = []
const exceptionEvents = []
try {
miniProgram = await automator.launch({
cliPath: process.env.WECHAT_DEVTOOLS_CLI || '/Applications/wechatwebdevtools.app/Contents/MacOS/cli',
projectPath: process.env.MINIPROGRAM_PROJECT_PATH || '/absolute/path/to/devtools-project',
})
miniProgram.on('console', (payload) => {
consoleEvents.push(payload)
})
miniProgram.on('exception', (payload) => {
exceptionEvents.push(payload)
})
await miniProgram.mockWxMethod('request', (options = {}) => {
const res = {
data: {
code: 0,
list: [
{ id: 1, title: 'Mock Item A' },
{ id: 2, title: 'Mock Item B' },
],
},
statusCode: 200,
header: { 'content-type': 'application/json' },
cookies: [],
errMsg: 'request:ok',
}
Promise.resolve().then(() => {
if (typeof options.success === 'function') options.success(res)
if (typeof options.complete === 'function') options.complete(res)
})
return {
abort() {},
onHeadersReceived() {},
offHeadersReceived() {},
onChunkReceived() {},
offChunkReceived() {},
}
})
const page = await miniProgram.reLaunch('/packageAPI/pages/network/request/request')
await page.waitFor('button')
const button = await page.$('button')
if (!button) {
throw new Error('Request page button not found')
}
await button.tap()
await page.waitFor(100)
const pageData = await page.data().catch(() => ({}))
if (pageData.loading !== false) {
throw new Error(`Loading status abnormal after request: ${JSON.stringify(pageData)}`)
}
const successLogs = consoleEvents.filter((event) => {
if (!event || typeof event !== 'object' || !Array.isArray(event.args)) return false
return event.args.some((arg) => String(arg).includes('request success'))
})
if (!successLogs.length) {
throw new Error(`No request success logs observed: ${JSON.stringify(consoleEvents)}`)
}
const consoleErrors = consoleEvents.filter((event) => {
return event && typeof event === 'object' && event.type === 'error'
})
if (consoleErrors.length) {
throw new Error(`console.error exists: ${JSON.stringify(consoleErrors)}`)
}
if (exceptionEvents.length) {
throw new Error(`Exceptions exist: ${JSON.stringify(exceptionEvents)}`)
}
} finally {
if (miniProgram) {
await miniProgram.restoreWxMethod('request').catch(() => {})
await miniProgram.close().catch(() => {})
}
}
}
run().catch((error) => {
console.error(error)
process.exit(1)
})consoletypeargspayload.type === 'error'logconnect()const automator = require('miniprogram-automator')
const path = require('node:path')
const fs = require('node:fs/promises')
const { spawnSync } = require('node:child_process')
const CLI_PATH =
process.env.WECHAT_DEVTOOLS_CLI ||
'/Applications/wechatwebdevtools.app/Contents/MacOS/cli'
// projectPath is the actual directory opened by DevTools (contains project.config.json)
const PROJECT_PATH =
process.env.MINIPROGRAM_PROJECT_PATH || '/absolute/path/to/project'
// Automation WebSocket port, default 9420
const AUTO_PORT = Number(process.env.WECHAT_AUTO_PORT || 9420)
const TARGET_PAGE = '/pages/home/index'
const OUTPUT_DIR = path.resolve(process.cwd(), 'outputs')
/**
* Detect the current HTTP service port of DevTools.
* Principle: Call the CLI once with a placeholder port, then parse the actual port from the error output.
* If the tool is not running, return null (CLI will start it automatically).
*/
function detectHttpPort() {
if (process.env.WECHAT_DEVTOOLS_PORT) {
return Number(process.env.WECHAT_DEVTOOLS_PORT)
}
try {
const result = spawnSync(
CLI_PATH,
['auto', '--project', PROJECT_PATH, '--port', '9999'],
{ encoding: 'utf8', timeout: 8000 },
)
const output = (result.stdout || '') + (result.stderr || '')
const match = output.match(/started on http:\/\/127\.0\.0\.1:(\d+)/)
if (match) return Number(match[1])
} catch (_) {}
return null
}
/**
* Enable automation WebSocket port with CLI v2 command.
* Command: cli auto --project <path> --auto-port <port> [--port <httpPort>]
*/
function enableAutomation(httpPort) {
const args = ['auto', '--project', PROJECT_PATH, '--auto-port', String(AUTO_PORT)]
if (httpPort) args.push('--port', String(httpPort))
const result = spawnSync(CLI_PATH, args, { encoding: 'utf8', timeout: 20000 })
return {
success: result.status === 0,
output: (result.stdout || '') + (result.stderr || ''),
}
}
async function main() {
let miniProgram
try {
await fs.mkdir(OUTPUT_DIR, { recursive: true })
// Step 1: Detect HTTP port to avoid CLI port conflict errors
const httpPort = detectHttpPort()
console.log(httpPort ? `HTTP Port: ${httpPort}` : 'No HTTP port detected, started by CLI')
// Step 2: Enable automation ws port with CLI v2
const { success, output } = enableAutomation(httpPort)
if (!success) throw new Error(`CLI auto command failed:\n${output}`)
console.log('Automation port is ready')
// Step 3: Connect to ws endpoint
miniProgram = await automator.connect({
wsEndpoint: `ws://127.0.0.1:${AUTO_PORT}`,
})
console.log('Connected')
// Step 4: Navigate to page and operate
const page = await miniProgram.reLaunch(TARGET_PAGE)
await page.waitFor('.page-title')
await page.waitFor(200)
await miniProgram.screenshot({ path: path.join(OUTPUT_DIR, 'page.png') })
console.log('Screenshot completed')
} finally {
if (miniProgram) {
// In connect mode, close() only disconnects the ws connection, does not close DevTools
await miniProgram.close().catch(() => {})
}
}
}
main().catch((error) => {
console.error(error)
process.exit(1)
})close()connect()close()launch()--auto-port--portdetectHttpPort()const PAGES = [
{ id: 'home', path: '/pages/home/index' },
{ id: 'list', path: '/pages/list/index' },
]
for (const item of PAGES) {
const page = await miniProgram.reLaunch(item.path)
await page.waitFor(500)
await miniProgram.screenshot({
path: path.join(OUTPUT_DIR, `${item.id}.png`),
})
}waitFor(500)index.jsonbeforeAllminiProgramafterAllminiProgramtest/it| Common Error | Correct Approach |
|---|---|
Fix | Set it to "the actual directory opened by DevTools"; in scenarios like Taro, it may be |
| Only remind to install dependencies, not security settings | Must remind to enable the service port |
Rely entirely on fixed | Prioritize |
Use | First |
Use runtime patch | Default to |
| Forget to restore mocks | Use |
| Treat screenshots as a real machine capability | Clearly state that screenshots only support DevTools simulator |
Directly read | For native input boxes, prioritize |
Use old versions of | Upgrade to |
Call | Ensure DevTools is completely closed (Cmd+Q) before launch |
Do not set timeout or set too short timeout for | First launch + user trust confirmation takes time, recommend |
Page path does not start with | Must start with |
| Directly screenshot pages that may time out with selector waiting (e.g., home page list) | For complex pages like home page, fixed waiting |
Still use | Switch to CLI v2 + |
| In connect mode, |
miniprogram-automator>= 0.12.0projectPathproject.config.jsontimeout120000launch()connect()cli auto --project <path> --auto-port <port>detectHttpPort()