Commit 4e65f484 by 张立顺

test: add mini program order smoke entry

1 parent 03eb4cc5
...@@ -8,9 +8,18 @@ ...@@ -8,9 +8,18 @@
"devDependencies": { "devDependencies": {
"@babel/generator": "^7.28.5", "@babel/generator": "^7.28.5",
"@babel/parser": "^7.28.5", "@babel/parser": "^7.28.5",
"miniprogram-automator": "^0.12.1",
"prettier": "^3.8.1", "prettier": "^3.8.1",
"prettier-plugin-mp": "^2.5.0" "prettier-plugin-mp": "^2.5.0"
}, },
"overrides": {
"miniprogram-automator": {
"jimp": "0.22.12"
}
},
"resolutions": {
"jimp": "0.22.12"
},
"scripts": { "scripts": {
"postinstall": "node scripts/setup-package-used-car.mjs", "postinstall": "node scripts/setup-package-used-car.mjs",
"setup": "npm install", "setup": "npm install",
...@@ -20,10 +29,11 @@ ...@@ -20,10 +29,11 @@
"test:equity": "node --experimental-detect-module --test packageEquity/tests/**/*.test.mjs", "test:equity": "node --experimental-detect-module --test packageEquity/tests/**/*.test.mjs",
"test:dynamicForm": "node --test packageDynamicForm/tests/**/*.test.mjs", "test:dynamicForm": "node --test packageDynamicForm/tests/**/*.test.mjs",
"test:createOrder": "node --experimental-detect-module --test tabWorkOrder/pages/createOrder/tests/**/*.test.mjs", "test:createOrder": "node --experimental-detect-module --test tabWorkOrder/pages/createOrder/tests/**/*.test.mjs",
"test:orderLifecycle": "node --experimental-detect-module --test tests/order-lifecycle/*.test.mjs tests/order-lifecycle/e2e/*.test.mjs", "test:orderLifecycle": "node --experimental-detect-module --test tests/order-lifecycle/*.test.mjs tests/order-lifecycle/e2e/*.test.mjs tests/order-lifecycle/ui/*.test.mjs",
"test:orderLifecycle:contract": "node --experimental-detect-module --test tests/order-lifecycle/apiContracts.test.mjs", "test:orderLifecycle:contract": "node --experimental-detect-module --test tests/order-lifecycle/apiContracts.test.mjs",
"test:orderLifecycle:preflight": "node scripts/order-lifecycle-preflight.mjs", "test:orderLifecycle:preflight": "node scripts/order-lifecycle-preflight.mjs",
"test:orderLifecycle:e2e": "node --test tests/order-lifecycle/e2e/orderLifecycle.live.test.mjs", "test:orderLifecycle:e2e": "node --test tests/order-lifecycle/e2e/orderLifecycle.live.test.mjs",
"test:orderLifecycle:ui": "node --test tests/order-lifecycle/ui/miniProgramSmoke.live.test.mjs",
"test:quotation": "node --experimental-detect-module --test packageEquity/tests/quotation*.test.mjs packageEquity/tests/ruleFn.test.mjs", "test:quotation": "node --experimental-detect-module --test packageEquity/tests/quotation*.test.mjs packageEquity/tests/ruleFn.test.mjs",
"test:components": "node --test tests/popupBehavior.test.mjs tests/calendarMonthMode.test.mjs tests/componentsImageUrl.test.mjs tests/technicianSelect.helpers.test.mjs tests/sideSelectDataSource.test.mjs tests/storePickerConfirm.test.mjs", "test:components": "node --test tests/popupBehavior.test.mjs tests/calendarMonthMode.test.mjs tests/componentsImageUrl.test.mjs tests/technicianSelect.helpers.test.mjs tests/sideSelectDataSource.test.mjs tests/storePickerConfirm.test.mjs",
"gen:component-index": "node scripts/gen-component-page-index.mjs" "gen:component-index": "node scripts/gen-component-page-index.mjs"
......
import assert from 'node:assert/strict'
import test from 'node:test'
import automator from 'miniprogram-automator'
import {
loadUiSmokeConfig,
requireUiSmokeConfig
} from './uiSmokeConfig.mjs'
import {
findExactIndex,
waitForListData
} from './uiSmokeHelpers.mjs'
const liveEnabled = loadUiSmokeConfig().enabled
const waitForPage = async (
miniProgram,
predicate,
{ timeoutMs = 30000, intervalMs = 300 } = {}
) => {
const deadline = Date.now() + timeoutMs
let lastPath = ''
while (Date.now() < deadline) {
const page = await miniProgram.currentPage()
lastPath = page?.path ?? ''
if (page && predicate(page)) return page
await new Promise(resolve => setTimeout(resolve, intervalMs))
}
throw new Error(`Timed out waiting for mini program page; last=${lastPath}`)
}
const tapListItem = async (page, selector, index, label) => {
const elements = await page.$$(selector)
const element = elements[index]
if (!element) throw new Error(`${label} UI item index ${index} is missing`)
await element.tap()
}
test(
'IT-OL-UI-LIVE-001: login selects exact park, store, premium scene and opens create order',
{ skip: !liveEnabled, timeout: 120000 },
async () => {
const config = requireUiSmokeConfig()
let miniProgram
try {
miniProgram = await automator.connect({
wsEndpoint: config.wsEndpoint
})
const loginPage = await miniProgram.reLaunch('/pages/wxLogin/wxLogin')
assert.equal(loginPage?.path, 'pages/wxLogin/wxLogin')
await loginPage.setData({ loginType: 1 })
await loginPage.waitFor(300)
const authSetting = await loginPage.$('auth-setting')
const authCheckbox = await authSetting?.$('.choose-box')
assert.ok(authCheckbox, 'privacy agreement checkbox should be rendered')
await authCheckbox.tap()
const inputs = await loginPage.$$('input')
assert.equal(inputs.length, 2)
await inputs[0].input(config.mobile)
await inputs[1].input(config.code)
const loginButton = await loginPage.$('.login_btn')
assert.ok(loginButton, 'mobile login button should be rendered')
await loginButton.tap()
const parkPage = await waitForPage(
miniProgram,
page => page.path === 'pages/accountItems/accountItems'
)
const parkItems = await waitForListData(
parkPage,
'loginUserInfoList'
)
const parkIndex = findExactIndex(
parkItems,
'merchantName',
config.parkName,
'park'
)
await tapListItem(parkPage, '.items', parkIndex, 'park')
const storePage = await waitForPage(
miniProgram,
page => page.path === 'pages/merchantList/merchantList'
)
const storeItems = await waitForListData(
storePage,
'loginUserInfoList'
)
const storeIndex = findExactIndex(
storeItems,
'merchantName',
config.storeName,
'store'
)
await tapListItem(storePage, '.items', storeIndex, 'store')
const scenePage = await waitForPage(
miniProgram,
page => page.path === 'pages/sceneIndex/sceneIndex'
)
const scenes = await waitForListData(scenePage, 'sceneList')
const sceneIndex = findExactIndex(
scenes,
'sceneName',
config.sceneName,
'scene'
)
await tapListItem(scenePage, '.scene-item', sceneIndex, 'scene')
await waitForPage(
miniProgram,
page => page.path === 'tabIndex/pages/index/index'
)
const createOrderPage = await miniProgram.navigateTo(
'/tabWorkOrder/pages/createOrder/createOrder'
)
assert.equal(
createOrderPage?.path,
'tabWorkOrder/pages/createOrder/createOrder'
)
await createOrderPage.waitFor(1000)
const pageData = await createOrderPage.data()
assert.equal(Number(pageData.sceneType), 1)
} finally {
if (miniProgram) await miniProgram.close()
}
}
)
const DEFAULT_CLI_PATH =
'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat'
export const loadUiSmokeConfig = (env = process.env) => ({
enabled: env.RUN_ORDER_UI_E2E === '1',
cliPath: env.ORDER_UI_CLI_PATH?.trim() || DEFAULT_CLI_PATH,
projectPath: env.ORDER_UI_PROJECT_PATH?.trim() || process.cwd(),
wsEndpoint:
env.ORDER_UI_WS_ENDPOINT?.trim() || 'ws://127.0.0.1:9420',
mobile: env.ORDER_E2E_MOBILE?.trim() || '',
code: env.ORDER_E2E_CODE?.trim() || '',
parkName: env.ORDER_E2E_YQ_NAME?.trim() || '流程测试园区门店',
storeName: env.ORDER_E2E_STORE_NAME?.trim() || '波波的单店门店',
sceneName: env.ORDER_E2E_SCENE_NAME?.trim() || '精品升级'
})
export const requireUiSmokeConfig = (env = process.env) => {
const config = loadUiSmokeConfig(env)
const missing = []
if (!config.enabled) missing.push('RUN_ORDER_UI_E2E')
if (!config.mobile) missing.push('ORDER_E2E_MOBILE')
if (!config.code) missing.push('ORDER_E2E_CODE')
if (missing.length) {
throw new Error(`Missing UI smoke runtime values: ${missing.join(', ')}`)
}
const endpoint = new URL(config.wsEndpoint)
if (
endpoint.protocol !== 'ws:' ||
!['127.0.0.1', 'localhost', '::1'].includes(endpoint.hostname)
) {
throw new Error('ORDER_UI_WS_ENDPOINT must be a loopback ws endpoint')
}
return config
}
import assert from 'node:assert/strict'
import test from 'node:test'
import {
loadUiSmokeConfig,
requireUiSmokeConfig
} from './uiSmokeConfig.mjs'
test('IT-OL-UI-001: UI smoke defaults contain only non-sensitive values', () => {
const config = loadUiSmokeConfig({})
assert.equal(config.enabled, false)
assert.equal(config.parkName, '流程测试园区门店')
assert.equal(config.storeName, '波波的单店门店')
assert.equal(config.sceneName, '精品升级')
assert.equal(config.wsEndpoint, 'ws://127.0.0.1:9420')
assert.equal(config.mobile, '')
assert.equal(config.code, '')
})
test('IT-OL-UI-002: live UI smoke requires explicit switch and credentials', () => {
assert.throws(
() =>
requireUiSmokeConfig({
RUN_ORDER_UI_E2E: '1',
ORDER_E2E_MOBILE: '',
ORDER_E2E_CODE: ''
}),
/ORDER_E2E_MOBILE, ORDER_E2E_CODE/
)
})
test('IT-OL-UI-003: UI smoke accepts runtime-only credentials', () => {
const config = requireUiSmokeConfig({
RUN_ORDER_UI_E2E: '1',
ORDER_E2E_MOBILE: 'runtime-mobile',
ORDER_E2E_CODE: 'runtime-code',
ORDER_E2E_YQ_NAME: '园区 A',
ORDER_E2E_STORE_NAME: '门店 B'
})
assert.equal(config.enabled, true)
assert.equal(config.parkName, '园区 A')
assert.equal(config.storeName, '门店 B')
assert.equal(config.mobile, 'runtime-mobile')
assert.equal(config.code, 'runtime-code')
})
test('IT-OL-UI-004: UI smoke only connects to a loopback automator endpoint', () => {
assert.throws(
() =>
requireUiSmokeConfig({
RUN_ORDER_UI_E2E: '1',
ORDER_E2E_MOBILE: 'runtime-mobile',
ORDER_E2E_CODE: 'runtime-code',
ORDER_UI_WS_ENDPOINT: 'ws://192.168.1.10:9420'
}),
/loopback/
)
})
const delay = milliseconds =>
new Promise(resolve => setTimeout(resolve, milliseconds))
export const findExactIndex = (items, field, expected, label) => {
const indexes = items
.map((item, index) => ({ index, value: String(item?.[field] ?? '') }))
.filter(item => item.value === expected)
if (indexes.length !== 1) {
throw new Error(
`${label} exact match count=${indexes.length}; expected one ${field}`
)
}
return indexes[0].index
}
export const waitForListData = async (
page,
field,
{ timeoutMs = 30000, intervalMs = 300 } = {}
) => {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
const items = await page.data(field)
if (Array.isArray(items) && items.length) return items
await delay(intervalMs)
}
throw new Error(`Timed out waiting for page data: ${field}`)
}
import assert from 'node:assert/strict'
import test from 'node:test'
import {
findExactIndex,
waitForListData
} from './uiSmokeHelpers.mjs'
test('IT-OL-UI-005: exact context match ignores partial names', () => {
const items = [
{ merchantName: '流程测试园区门店-备选' },
{ merchantName: '流程测试园区门店' }
]
assert.equal(
findExactIndex(items, 'merchantName', '流程测试园区门店', 'park'),
1
)
})
test('IT-OL-UI-006: duplicate exact context is rejected', () => {
assert.throws(
() =>
findExactIndex(
[{ sceneName: '精品升级' }, { sceneName: '精品升级' }],
'sceneName',
'精品升级',
'scene'
),
/count=2/
)
})
test('IT-OL-UI-007: page list polling returns once data is available', async () => {
let reads = 0
const page = {
async data(field) {
assert.equal(field, 'sceneList')
reads += 1
return reads === 1 ? [] : [{ sceneName: '精品升级' }]
}
}
const result = await waitForListData(page, 'sceneList', {
timeoutMs: 100,
intervalMs: 1
})
assert.equal(reads, 2)
assert.equal(result[0].sceneName, '精品升级')
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!