提交 6670bf85 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

wip(uts): automator

上级 7a5b9013
import { JSONArray, JSONObject } from 'com.alibaba.fastjson'
import {
ViewToTempFilePathSuccess,
ViewToTempFilePathFail
} from 'io.dcloud.uniapp.runtime'
import { getCurrentPages } from 'io.dcloud.uts.framework'
import { parsePage } from './util.uts'
export type getPageStackParams = {
callback: (res: any) => void
}
export const getPageStack = (params: getPageStackParams): void => {
params.callback({
pageStack: getCurrentPages().map((page: BasePage): UTSJSONObject => {
return parsePage(page)
})
})
}
export type getCurrentPageParams = {
callback: (res: any) => void
}
function _getCurrentPage(): BasePage | null {
const pages = getCurrentPages()
return pages.length > 0 ? pages[pages.length - 1] : null
}
export const getCurrentPage = (params: getCurrentPageParams): void => {
const page = _getCurrentPage()
params.callback({ page: page !== null ? parsePage(page) : null })
}
export type CallUniMethodParams = {
method: string
args: JSONArray
callback: (res: any) => void
}
export const callUniMethod = (params: CallUniMethodParams): void => {
const method = params.method
const args = params.args
const callback = params.callback
let animationType: string = 'pop-in'
let animationDuration: number = 300
let arg = {} as JSONObject
if (args.size > 0) {
arg = args[0] as JSONObject
}
const success = (result: any) => {
const timeout = method == 'pageScrollTo' ? 350 : 0
setTimeout(() => {
callback(result)
}, timeout)
}
const fail = (err: any) => {
callback(err)
}
switch (method) {
case 'navigateTo':
if (arg['animationType'] !== null) {
animationType = arg['animationType'] as string
}
if (arg['animationDuration'] !== null) {
animationDuration = arg['animationDuration'] as number
}
uni.navigateTo({
url: arg['url'] as string,
animationType,
animationDuration,
success,
fail
})
break
case 'redirectTo':
uni.redirectTo({
url: arg['url'] as string,
success,
fail
})
break
case 'reLaunch':
uni.reLaunch({
url: arg['url'] as string,
success,
fail
})
break
case 'navigateBack':
animationType = 'pop-out'
if (arg['animationType'] !== null) {
animationType = arg['animationType'] as string
}
if (arg['animationDuration'] !== null) {
animationDuration = arg['animationDuration'] as number
}
uni.navigateBack({
animationType,
animationDuration,
success,
fail
})
break
default:
callback({ errMsg: 'uni.' + method + ' not exists' })
break
}
}
export type captureScreenshotParams = {
id?: string | null,
path: string
callback: (res: any) => void
}
export const captureScreenshot = (params: captureScreenshotParams): void => {
const callback = params.callback
const currentPage = _getCurrentPage()
if (currentPage !== null) {
currentPage.$viewToTempFilePath({
id: params.id,
path: params.path,
success: (res: ViewToTempFilePathSuccess) => {
callback({
errMsg: 'screenshot:ok',
tempFilePath: res.tempFilePath,
data: UTSAndroid.dcloud_private_getFileBase64(res.tempFilePath)
})
},
fail: (err: ViewToTempFilePathFail) => {
callback(err)
}
})
} else {
callback({
errMsg: `captureScreenshot:fail, currentPage is not found.`
})
}
}
// @ts-ignore
import { JSONObject } from 'com.alibaba.fastjson'
import { getData, setData, getPageVm } from './util.uts'
export type getDataParams = {
pageId: number
path: string
callback: (res: any) => void
}
export const pageGetData = (params: getDataParams): void => {
const callback = params.callback
const result = getData(getPageVm(params.pageId)!, params.path)
callback(result)
}
export type setDataParams = {
pageId: number
data: JSONObject
callback: (res?: any | null) => void
}
export const pageSetData = (params: setDataParams): void => {
const pageId = params.pageId
const callback = params.callback
const page = getPageVm(pageId)
if (page !== null) {
setData(page, params.data)
callback(null)
} else {
callback({ errMsg: `setData:fail, Page:${pageId} is not found.` })
}
}
import { JSONObject } from 'com.alibaba.fastjson'
import { getCurrentPages } from 'io.dcloud.uts.framework'
function getPageId(page: BasePage): number {
return page.$.uid
}
function getPagePath(page: BasePage): string {
return page.route
}
function getPageQuery(page: BasePage): VueComponentOptions {
return page.$options
}
function getPageById(id: number): BasePage | null {
const pages = getCurrentPages()
let result: BasePage | null = null
pages.forEach((page: BasePage) => {
if (getPageId(page) === id) {
result = page
}
})
return result
}
export function getPageVm(id: number): BasePage | null {
return getPageById(id)
}
export function getData(
vm: BasePage | null,
path?: string
): Map<string, any | null> {
let data = new Map<string, any | null>()
if (vm !== null) {
// TODO: path
// data = path ? getDataByPath(vm.$data, path) : Object.assign({}, vm.$data)
if(path !== null){
data.set("errMsg", `getData:fail, path:${path} is not supported.`)
}else{
data = vm.$data
}
}
return data
}
export function setData(vm: BasePage, data: JSONObject): void {
for (const key in data) {
// @ts-ignore
const _key = key.key
// @ts-ignore
const value = key.value
vm.$data.set(_key, value)
}
}
export function parsePage(page: BasePage): UTSJSONObject {
return {
id: getPageId(page),
path: getPagePath(page),
query: getPageQuery(page),
} as UTSJSONObject
}
\ No newline at end of file
import { JSONArray, JSONObject } from 'com.alibaba.fastjson'
import { SocketTask, SendSocketMessageOptions } from 'uts.sdk.modules.DCloudUniWebsocket'
import {
callUniMethod,
CallUniMethodParams,
captureScreenshot,
captureScreenshotParams,
getPageStack,
getPageStackParams,
getCurrentPage,
getCurrentPageParams
} from './apis/App.uts'
import {
pageGetData,
getDataParams,
pageSetData,
setDataParams
} from './apis/Page.uts'
let socketTask: SocketTask | null = null
const wsEndpoint = "ws://192.168.31.114:9520"
function send(data: any) {
socketTask?.send({ data: JSON.stringify(data) } as SendSocketMessageOptions)
}
function onMessage(msg: string) {
// 解析 msg 触发指令
const json = JSON.parse(msg)!
const method = json['method'] as string
const params = json['params'] as JSONObject
const path = params['path'] !== null ? (params['path'] as string) : ''
const res = new Map<string, any | null>([['id', json['id'] as string]])
try {
const callback = (result?: any | null) => {
if (result !== null) {
res.set('result', result)
}
send(res)
}
if (method.startsWith('App.')) {
switch (method) {
case 'App.callUniMethod':
const method = params['method'] as string
const args = params['args'] as JSONArray
callUniMethod({
method,
args,
callback
} as CallUniMethodParams)
break
case 'App.captureScreenshot':
const id = params['id'] !== null ? (params['id'] as string) : null
captureScreenshot({ id, path, callback } as captureScreenshotParams)
break
case 'App.getPageStack':
getPageStack({ callback } as getPageStackParams)
break
case 'App.getCurrentPage':
getCurrentPage({ callback } as getCurrentPageParams)
break
}
} else if (method.startsWith('Page.')) {
const pageId = params['pageId'] as number
const data =
params['data'] !== null
? (params['data'] as JSONObject)
: ({} as JSONObject)
switch (method) {
case 'Page.getData':
pageGetData({ pageId, path, callback } as getDataParams)
break
case 'Page.setData':
pageSetData({ pageId, data, callback } as setDataParams)
break
}
}
} catch (error) {
res.set('error', error)
send(res)
}
}
export function initAutomator() {
console.log('initAutomator')
socketTask = uni.connectSocket({
url: wsEndpoint
});
socketTask!.onMessage((res) => {onMessage(res.data as string)})
socketTask!.onOpen((_) => {
console.warn("已开启自动化测试...")
})
socketTask!.onError((err) => {
console.warn(`automator.onError: ${JSON.stringify(err)}`);
})
socketTask!.onClose((_) => {
console.warn("automator.onClose");
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册