提交 b04d07aa 编写于 作者: fxy060608's avatar fxy060608 提交者: qiang

wip(uvue): 支持 app-webview 自动化测试

# Conflicts:
#	packages/uni-automator/dist/index.js
上级 7cd38647
......@@ -29,6 +29,8 @@ declare namespace NodeJS {
UNI_NVUE_STYLE_COMPILER: 'uni-app' | 'weex'
UNI_APP_CODE_SPLITING?: 'true'
UNI_AUTOMATOR_WS_ENDPOINT?: string
UNI_AUTOMATOR_APP_WEBVIEW?: string
UNI_AUTOMATOR_APP_WEBVIEW_SRC?: string
UNI_H5_BASE?: string
UNI_H5_BROWSER?: 'builtin'
UNI_CUSTOM_SCRIPT?: string
......
......@@ -224,9 +224,11 @@ function normalizeCode(code: string, isMain = false) {
if (!isMain) {
return code
}
const automatorCode = process.env.UNI_AUTOMATOR_WS_ENDPOINT
? 'initAutomator();'
: ''
const automatorCode =
process.env.UNI_AUTOMATOR_WS_ENDPOINT &&
process.env.UNI_AUTOMATOR_APP_WEBVIEW !== 'true'
? 'initAutomator();'
: ''
return `${code}
export function main(app: IApp) {
definePageRoutes();
......
<script>
let firstBackTime = 0
export default {
onLaunch: function () {
console.log('App Launch')
},
onShow: function () {
console.log('App Show')
},
onHide: function () {
console.log('App Hide')
},
// #ifdef APP-ANDROID
onLastPageBackPress: function () {
console.log('App LastPageBackPress')
if (firstBackTime == 0) {
uni.showToast({
title: '再按一次退出应用',
position: 'bottom',
})
firstBackTime = Date.now()
setTimeout(() => {
firstBackTime = 0
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now()
uni.exit()
}
},
// #endif
onExit: function () {
console.log('App Exit')
},
}
</script>
\ No newline at end of file
import App from './App.uvue'
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
\ No newline at end of file
{
"name" : "NAME",
"appid" : "APPID",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"uni-app-x" : {}
}
{
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "uni-app x"
}
}
],
"globalStyle": {
"navigationStyle": "custom"
}
}
<template>
<web-view ref="webview" id="webview" style="flex:1" :webview-styles='webviewStyles' :src="src" @message="message"
@error="error" />
</template>
<script>
export default {
data() {
return {
src: process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC,
webviewElement: null as UniWebViewElement | null,
webviewContext: null as WebviewContext | null,
webviewStyles: {
progress: false
},
}
},
onReady() {
this.webviewElement = this.$refs['webview'] as UniWebViewElement
this.webviewContext = uni.createWebviewContext('webview', this)
},
methods: {
initAutomator() {
const options = {
wsEndpoint: process.env.UNI_AUTOMATOR_WS_ENDPOINT
}
this.webviewContext!.evalJS(`initRuntimeAutomator(${JSON.stringify(options)})`)
console.log('initRuntimeAutomator...')
},
message(msg: UniWebViewMessageEvent) {
const data = msg.detail.data!
const id = data.get("id") as number
const type = data.get("type") as string
const dataObj = data.get("data") as UTSJSONObject
const action = dataObj.getString("action")!
const args = dataObj.get("args")
if (type != 'automator') {
return;
}
if (action == 'ready') {
this.initAutomator()
} else {
console.log(id, action, args)
if (action == 'captureScreenshot') {
// 调用截图
this.webviewElement!.takeSnapshot({
success: (res) => {
const fileManager = uni.getFileSystemManager()
fileManager.readFile({
encoding: 'base64',
filePath: res.tempFilePath,
success: (readFileRes) => {
this.callback(id, { data: readFileRes.data }, '')
},
fail: (error) => {
this.callback(id, '', error.message)
},
} as ReadFileOptions)
},
fail: (res) => {
this.callback(id, '', res.errMsg)
}
})
}
}
},
error(event: WebViewErrorEvent) {
console.log('webview load error', JSON.stringify(event.detail));
},
callback(id: number, res: any | null, error: string) {
this.webviewContext!.evalJS(`onPostMessageFromUniXWebView(${id},${JSON.stringify(res)},${JSON.stringify(error)})`)
}
}
}
</script>
\ No newline at end of file
......@@ -29,6 +29,10 @@ exports.default = [
if (opts.filter(id)) {
const platform = process.env.UNI_PLATFORM;
if (platform === 'app' && process.env.UNI_APP_X === 'true') {
// app-webview,不增加 initAutomator
if (process.env.UNI_AUTOMATOR_APP_WEBVIEW === 'true') {
return null;
}
const automatorPath = (0, uni_cli_shared_1.normalizePath)((0, uni_cli_shared_1.resolveBuiltIn)(`@dcloudio/uni-app-uts/lib/automator/index.uts`));
return {
code:
......
......@@ -38,6 +38,10 @@ export default [
if (opts.filter(id)) {
const platform = process.env.UNI_PLATFORM
if (platform === 'app' && process.env.UNI_APP_X === 'true') {
// app-webview,不增加 initAutomator
if (process.env.UNI_AUTOMATOR_APP_WEBVIEW === 'true') {
return null
}
const automatorPath = normalizePath(
resolveBuiltIn(`@dcloudio/uni-app-uts/lib/automator/index.uts`)
)
......
......@@ -40,6 +40,9 @@ export function initDefine(stringifyBoolean: boolean = false) {
'process.env.UNI_AUTOMATOR_WS_ENDPOINT': JSON.stringify(
process.env.UNI_AUTOMATOR_WS_ENDPOINT || ''
),
'process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC': JSON.stringify(
process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC || ''
),
'process.env.UNI_CLOUD_PROVIDER': JSON.stringify(
process.env.UNI_CLOUD_PROVIDER || ''
),
......
......@@ -126,6 +126,10 @@ export function getRouterOptions(manifestJson: Record<string, any>): {
}
export function isEnableTreeShaking(manifestJson: Record<string, any>) {
// 自动化测试时,一定不摇树
if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
return false
}
return manifestJson.h5?.optimization?.treeShaking?.enable !== false
}
......
此差异已折叠。
......@@ -101,6 +101,8 @@ export async function compileApp(entry: string, options: CompileAppOptions) {
// 自动化测试
NODE_ENV: process.env.NODE_ENV,
UNI_AUTOMATOR_WS_ENDPOINT: process.env.UNI_AUTOMATOR_WS_ENDPOINT || '',
UNI_AUTOMATOR_APP_WEBVIEW_SRC:
process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC || '',
},
},
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册