refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,85 @@
|
||||
<template>
|
||||
<enum-data title="android-nested-scroll(默认值为all)" :items="types" @change="onTypeChange"></enum-data>
|
||||
<scroll-view style="flex:1;">
|
||||
<view v-for="item in 5" class="scroll-item">
|
||||
<text class="scroll-item-title">item{{item}}</text>
|
||||
</view>
|
||||
<web-view class="web-view" src="/hybrid/html/local.html" :android-nested-scroll="type"></web-view>
|
||||
<view v-for="item in 5" class="scroll-item">
|
||||
<text class="scroll-item-title">item{{item}}</text>
|
||||
</view>
|
||||
<web-view class="web-view" src="/hybrid/html/simple.html" :android-nested-scroll="type"></web-view>
|
||||
<view v-for="item in 5" class="scroll-item">
|
||||
<text class="scroll-item-title">item{{item}}</text>
|
||||
</view>
|
||||
<text style="color: coral;text-align: center;margin-top: 6px;">下面是测试嵌套横向滚动scroll-view的场景</text>
|
||||
<scroll-view style="flex-direction: row;" direction="horizontal">
|
||||
<view v-for="item in 5" class="scroll-item2">
|
||||
<text class="scroll-item-title2">item{{item}}</text>
|
||||
</view>
|
||||
<web-view class="web-view" src="/hybrid/html/local.html" :android-nested-scroll="type"></web-view>
|
||||
<view v-for="item in 5" class="scroll-item2">
|
||||
<text class="scroll-item-title2">item{{item}}</text>
|
||||
</view>
|
||||
<web-view class="web-view" src="/hybrid/html/simple.html" :android-nested-scroll="type"></web-view>
|
||||
<view v-for="item in 5" class="scroll-item2">
|
||||
<text class="scroll-item-title2">item{{item}}</text>
|
||||
</view>
|
||||
</scroll-view>
|
||||
<view v-for="item in 5" class="scroll-item">
|
||||
<text class="scroll-item-title">item{{item}}</text>
|
||||
</view>
|
||||
|
||||
<bottom-safe-area />
|
||||
</scroll-view>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ItemType } from '@/components/enum-data/enum-data-types';
|
||||
|
||||
const types : ItemType[] = [{ "value": 0, "name": "all" }, { "value": 1, "name": "vertical" }, { "value": 2, "name": "horizontal" }, { "value": 3, "name": "none" }];
|
||||
const type = ref("all");
|
||||
|
||||
const onTypeChange = (value : number) => {
|
||||
type.value = types[value].name;
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.web-view {
|
||||
width: 300px;
|
||||
height: 300px;
|
||||
margin-left: 6px;
|
||||
margin-right: 6px;
|
||||
margin-top: 6px;
|
||||
border: black 1px solid;
|
||||
align-self: center;
|
||||
}
|
||||
|
||||
.scroll-item {
|
||||
margin-left: 6px;
|
||||
margin-right: 6px;
|
||||
margin-top: 6px;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.scroll-item2 {
|
||||
width: 80px;
|
||||
justify-content: center;
|
||||
margin-left: 6px;
|
||||
margin-right: 6px;
|
||||
margin-top: 6px;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.scroll-item-title {
|
||||
line-height: 60px;
|
||||
text-align: center;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.scroll-item-title2 {
|
||||
text-align: center;
|
||||
color: #555;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,233 @@
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isIOS = platformInfo.startsWith('ios')
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
const isHarmony = platformInfo.startsWith('harmony')
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
const isAppWebView = process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true'
|
||||
|
||||
|
||||
|
||||
describe('component-native-web-view', () => {
|
||||
if (isWeb || isAppWebView) {
|
||||
it('web', async () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
let page;
|
||||
let start = 0;
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch('/pages/component/web-view/web-view');
|
||||
await page.waitFor(3000);
|
||||
});
|
||||
|
||||
// 测试辅助函数
|
||||
async function setPageData(newData) {
|
||||
return await page.setData({ data: newData });
|
||||
}
|
||||
|
||||
it('check_load_url', async () => {
|
||||
expect(await page.data('data.loadError')).toBe(false)
|
||||
});
|
||||
|
||||
it('test attr webview-styles', async () => {
|
||||
await setPageData({
|
||||
webview_progress_color: '#FF0'
|
||||
});
|
||||
await page.waitFor(100);
|
||||
await page.callMethod('reload');
|
||||
await page.waitFor(100);
|
||||
await setPageData({
|
||||
webview_progress_color: 'yellow'
|
||||
});
|
||||
await page.waitFor(100);
|
||||
await page.callMethod('reload');
|
||||
});
|
||||
|
||||
if (isMP) {
|
||||
return
|
||||
}
|
||||
it('set auto test', async () => {
|
||||
await setPageData({
|
||||
autoTest: true
|
||||
});
|
||||
expect(1).toBe(1)
|
||||
});
|
||||
it('test touch event', async () => {
|
||||
const windowInfo = await program.callUniMethod('getWindowInfo');
|
||||
await program.tap({
|
||||
x: 10,
|
||||
y: windowInfo.safeAreaInsets.top + 44 + 10
|
||||
});
|
||||
await page.waitFor(500);
|
||||
if (!isIOS && !isAndroid) {
|
||||
expect(await page.data('data.isTouchEnable')).toBe(true);
|
||||
}
|
||||
|
||||
await setPageData({
|
||||
pointerEvents: 'none',
|
||||
isTouchEnable: false
|
||||
});
|
||||
await page.waitFor(100);
|
||||
await program.tap({
|
||||
x: 10,
|
||||
y: windowInfo.safeAreaInsets.top + 44 + 10
|
||||
});
|
||||
await page.waitFor(500);
|
||||
if (!isIOS && !isHarmony && !isAndroid) {
|
||||
expect(await page.data('data.isTouchEnable')).toBe(false);
|
||||
}
|
||||
await setPageData({
|
||||
pointerEvents: 'auto'
|
||||
});
|
||||
});
|
||||
|
||||
it('test event loading load', async () => {
|
||||
await page.callMethod('reload');
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventLoading')) || (Date.now() - start > 500);
|
||||
});
|
||||
if (isIOS) {
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
if (
|
||||
platformInfo.indexOf('14.5') != -1 ||
|
||||
platformInfo.indexOf('13.7') != -1 ||
|
||||
platformInfo.indexOf('12.4') != -1
|
||||
) {
|
||||
expect(1).toBe(1)
|
||||
return
|
||||
}
|
||||
expect(await page.data('data.eventLoading')).toEqual({
|
||||
"tagName": "WEB-VIEW",
|
||||
type: 'loading',
|
||||
src: 'https://www.dcloud.io/'
|
||||
});
|
||||
} else {
|
||||
expect(await page.data('data.eventLoading')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'loading',
|
||||
src: 'https://www.dcloud.io/'
|
||||
});
|
||||
}
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventLoad')) || (Date.now() - start > 5000);
|
||||
});
|
||||
expect(await page.data('data.eventLoad')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'load',
|
||||
src: 'https://www.dcloud.io/',
|
||||
url: 'https://www.dcloud.io/'
|
||||
});
|
||||
});
|
||||
|
||||
it('test event contentheightchange', async () => {
|
||||
if (!isAndroid && !isIOS && !isHarmony) {
|
||||
expect(1).toBe(1);
|
||||
return;
|
||||
}
|
||||
expect(await page.callMethod('getContentHeight')).toBeGreaterThan(0);
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventContentHeightChange')) || (Date.now() - start > 500);
|
||||
});
|
||||
expect(await page.data('data.eventContentHeightChange')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'contentheightchange',
|
||||
isValidHeight: true
|
||||
});
|
||||
});
|
||||
|
||||
it('test event error', async () => {
|
||||
const infos = process.env.uniTestPlatformInfo.split(' ');
|
||||
const version = parseInt(infos[infos.length - 1]);
|
||||
if (isAndroid && version > 5) {
|
||||
await setPageData({
|
||||
src: 'https://www.dclou.io/uni-app-x'
|
||||
});
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventError')) || (Date.now() - start > 5000);
|
||||
});
|
||||
expect(await page.data('data.eventError')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'error',
|
||||
errCode: 100002,
|
||||
errMsg: 'page error',
|
||||
url: 'https://www.dclou.io',
|
||||
fullUrl: 'https://www.dclou.io/uni-app-x',
|
||||
src: 'https://www.dclou.io/uni-app-x'
|
||||
});
|
||||
}
|
||||
await setPageData({
|
||||
autoTest: false
|
||||
});
|
||||
});
|
||||
|
||||
it('checkNativeWebView', async () => {
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
if (
|
||||
platformInfo.indexOf('14.5') != -1 ||
|
||||
platformInfo.indexOf('13.7') != -1 ||
|
||||
platformInfo.indexOf('12.4') != -1
|
||||
) {
|
||||
expect(1).toBe(1)
|
||||
return
|
||||
}
|
||||
await page.waitFor(300);
|
||||
const has = await page.callMethod('checkNativeWebView')
|
||||
expect(has).toBe(true)
|
||||
})
|
||||
|
||||
it('checkLoadingCount', async () => {
|
||||
if (
|
||||
platformInfo.indexOf('14.5') != -1 ||
|
||||
platformInfo.indexOf('13.7') != -1 ||
|
||||
platformInfo.indexOf('12.4') != -1
|
||||
) {
|
||||
expect(1).toBe(1)
|
||||
return
|
||||
}
|
||||
await page.callMethod('checkLoadingCount')
|
||||
await page.waitFor(300);
|
||||
const has = await page.callMethod('checkNativeWebView')
|
||||
if (has) {
|
||||
if (isHarmony) {
|
||||
expect(await page.data('data.loadingCount')).toBeGreaterThanOrEqual(1);
|
||||
} else {
|
||||
expect(await page.data('data.loadingCount')).toBe(1);
|
||||
}
|
||||
} else {
|
||||
expect(await page.data('data.loadingCount')).toBe(0);
|
||||
}
|
||||
})
|
||||
|
||||
it('test lodaData', async () => {
|
||||
await page.callMethod('loadData');
|
||||
await page.waitFor(1000);
|
||||
const image = await program.screenshot({ fullPage: true });
|
||||
expect(image).toSaveImageSnapshot();
|
||||
});
|
||||
|
||||
it('test half screen toggle', async () => {
|
||||
// 点击宽窄屏切换按钮
|
||||
const toggleButton = await page.$('#half-screen-toggle');
|
||||
expect(toggleButton).toBeTruthy();
|
||||
|
||||
await toggleButton.tap();
|
||||
await page.waitFor(500);
|
||||
|
||||
// 点击后截图
|
||||
const image = await program.screenshot({
|
||||
fullPage: true,
|
||||
});
|
||||
expect(image).toSaveImageSnapshot();
|
||||
|
||||
// 截图点击恢复
|
||||
await page.waitFor(200);
|
||||
await toggleButton.tap();
|
||||
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,373 @@
|
||||
<template>
|
||||
<view class="uni-flex-item">
|
||||
<web-view id="web-view" class="uni-flex-item" :style="webViewStyle" :src="data.src"
|
||||
:webview-styles="{ progress: {color:data.webview_progress_color} }" :horizontalScrollBarAccess="data.horizontalScrollBarAccess" :verticalScrollBarAccess="data.verticalScrollBarAccess"
|
||||
:bounces="data.bounces" :disable-user-select-menu="data.disableUserSelectMenu" @message="message" @error="error" @loading="loading"
|
||||
@load="load" @download="download" @contentheightchange="contentheightchange" @touchstart="touchstart" @tap="tap">
|
||||
</web-view>
|
||||
<!-- #ifdef APP -->
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-btn-v">
|
||||
<input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" :maxlength="-1" />
|
||||
</view>
|
||||
<view class="uni-row uni-btn-v">
|
||||
<button class="uni-flex-item" type="primary" :disabled="!data.canGoBack" @click="back">后退</button>
|
||||
<button class="margin-left-5 uni-flex-item" type="primary" :disabled="!data.canGoForward"
|
||||
@click="forward">前进</button>
|
||||
</view>
|
||||
<view class="uni-row uni-btn-v">
|
||||
<button class="uni-flex-item" type="primary" @click="reload">重新加载</button>
|
||||
<button class="margin-left-5 uni-flex-item" type="primary" @click="stop">停止加载</button>
|
||||
</view>
|
||||
<view class="uni-row uni-btn-v">
|
||||
<button class="uni-flex-item" type="primary" @click="nativeToWeb">原生和Web通信</button>
|
||||
<!-- #ifdef APP-ANDROID || APP-IOS || APP-HARMONY -->
|
||||
<button class="margin-left-5 uni-flex-item" type="primary" @click="getContentHeight">获取内容高度</button>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
<view class="uni-row uni-btn-v">
|
||||
<button class="uni-flex-item" type="primary" @click="loadData">加载页面内容</button>
|
||||
<!-- 用于演示大尺寸平板中能用窄屏展示响应式内容 -->
|
||||
<button id="half-screen-toggle" class="margin-left-5 uni-flex-item" type="primary" @click="setHalfScreen">宽窄屏切换</button>
|
||||
</view>
|
||||
<view class="uni-btn-v">
|
||||
<navigator url="/pages/component/web-view/web-view-scroll">
|
||||
<button type="primary">scroll-view嵌套web-view</button>
|
||||
</navigator>
|
||||
</view>
|
||||
<!-- #ifdef APP-ANDROID || APP-HARMONY || APP-IOS -->
|
||||
<view class="uni-row uni-btn-v">
|
||||
<view class="uni-row uni-flex-item align-items-center">
|
||||
<text>显示横向滚动条</text>
|
||||
<switch :checked="true" @change="changeHorizontalScrollBarAccess"></switch>
|
||||
</view>
|
||||
<view class="uni-row uni-flex-item align-items-center">
|
||||
<text>显示竖向滚动条</text>
|
||||
<switch :checked="true" @change="changeVerticalScrollBarAccess"></switch>
|
||||
</view>
|
||||
</view>
|
||||
<view class="uni-row uni-btn-v">
|
||||
<view class="uni-row uni-flex-item align-items-center">
|
||||
<text>开启bounces</text>
|
||||
<switch :checked="true" @change="changeBounces"></switch>
|
||||
<!-- #ifdef APP-ANDROID || APP-IOS-->
|
||||
<text>禁用选择菜单</text>
|
||||
<switch :checked="false" @change="changeDisableUserSelectMenu"></switch>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifdef APP-IOS -->
|
||||
<view class="uni-row uni-btn-v" v-if="isProd() === false">
|
||||
<view class="uni-row uni-flex-item align-items-center">
|
||||
<text>前进、后退功能在Windows端需要打自定义基座,MAC端需要配置Xcode环境后进行真机运行或者打自定义基座</text>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
|
||||
<bottom-safe-area />
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
// #ifdef APP
|
||||
import { canWebViewGoBack, canWebViewGoForward, hasNativeView } from '@/uni_modules/uts-get-native-view';
|
||||
// #endif
|
||||
|
||||
type DataType = {
|
||||
src: string;
|
||||
webview_progress_color: string;
|
||||
halfWindowMode: boolean;
|
||||
webviewContext: WebviewContext | null;
|
||||
loadError: boolean;
|
||||
horizontalScrollBarAccess: boolean;
|
||||
verticalScrollBarAccess: boolean;
|
||||
bounces: boolean;
|
||||
disableUserSelectMenu: boolean;
|
||||
canGoBack: boolean;
|
||||
canGoForward: boolean;
|
||||
autoTest: boolean;
|
||||
eventLoading: UTSJSONObject | null;
|
||||
eventLoad: UTSJSONObject | null;
|
||||
eventError: UTSJSONObject | null;
|
||||
eventContentHeightChange: UTSJSONObject | null;
|
||||
pointerEvents: string;
|
||||
isTouchEnable: boolean;
|
||||
loadingCount: number;
|
||||
}
|
||||
// 使用reactive避免ref数据在自动化测试中无法访问
|
||||
const data = reactive({
|
||||
src: 'https://www.dcloud.io',
|
||||
webview_progress_color:'#FF3333',
|
||||
halfWindowMode: false,
|
||||
webviewContext: null as WebviewContext | null,
|
||||
loadError: false,
|
||||
horizontalScrollBarAccess: true,
|
||||
verticalScrollBarAccess: true,
|
||||
bounces: true,
|
||||
disableUserSelectMenu: false,
|
||||
canGoBack: false,
|
||||
canGoForward: false,
|
||||
autoTest: false,
|
||||
eventLoading: null as UTSJSONObject | null,
|
||||
eventLoad: null as UTSJSONObject | null,
|
||||
eventError: null as UTSJSONObject | null,
|
||||
eventContentHeightChange: null as UTSJSONObject | null,
|
||||
pointerEvents: 'auto',
|
||||
isTouchEnable: false,
|
||||
loadingCount: 0
|
||||
} as DataType)
|
||||
|
||||
let webviewElement = null as UniWebViewElement | null
|
||||
let webviewContext = null as WebViewContext | null
|
||||
const fullScreen = computed(() => {
|
||||
return !data.halfWindowMode
|
||||
})
|
||||
|
||||
const webViewStyle = computed(() => {
|
||||
return {
|
||||
width: data.halfWindowMode ? '50%' : '100%',
|
||||
'pointer-events': data.pointerEvents
|
||||
}
|
||||
})
|
||||
|
||||
const getPackageName = (): string => {
|
||||
let packageName: string = ""
|
||||
|
||||
// #ifdef APP-IOS
|
||||
const res = uni.getAppBaseInfo();
|
||||
packageName = res.bundleId
|
||||
// #endif
|
||||
|
||||
return packageName
|
||||
}
|
||||
|
||||
const isProd = (): boolean => {
|
||||
if (getPackageName() == 'io.dcloud.hellouniappx') {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const setHalfScreen = () => {
|
||||
data.halfWindowMode = !data.halfWindowMode
|
||||
}
|
||||
|
||||
const back = () => {
|
||||
webviewContext?.back();
|
||||
}
|
||||
|
||||
const forward = () => {
|
||||
webviewContext?.forward();
|
||||
}
|
||||
|
||||
const reload = () => {
|
||||
data.loadingCount = 0
|
||||
webviewContext?.reload();
|
||||
}
|
||||
|
||||
const stop = () => {
|
||||
webviewContext?.stop();
|
||||
}
|
||||
|
||||
const nativeToWeb = () => {
|
||||
webviewContext?.evalJS("alert('hello uni-app x')");
|
||||
}
|
||||
|
||||
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
|
||||
const getContentHeight = (): number => {
|
||||
const height = webviewContext?.getContentHeight() ?? 0;
|
||||
console.log('contentHeight', height);
|
||||
if (!data.autoTest) {
|
||||
uni.showModal({
|
||||
content: ' 当前内容高度: ' + height,
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
return height;
|
||||
}
|
||||
|
||||
const loadData = () => {
|
||||
// webviewElement
|
||||
|
||||
const options: UniWebviewContextLoadDataOptions = {
|
||||
data: '<p><a href="https://www.dcloud.io/hbuilderx.html">HBuilderX</a><br/><img src="/unidoc/zh/uni@2x.png"></img><h1>HBuilderX,轻巧、极速,极客编辑器</h1><p style="color:red;"><small>HBuilderX,轻巧、极速,极客编辑器 </small><big>HBuilderX,轻巧、极速,极客编辑器</big><strong>HBuilderX,轻巧、极速,极客编辑器</strong><i>HBuilderX,轻巧、极速,极客编辑器 </i><u>HBuilderX,轻巧、极速,极客编辑器</u><del>HBuilderX,轻巧、极速,极客编辑器</del></p><h2>uni-app x,终极跨平台方案</h2>、<p style="background-color: yellow;"><small>uni-app x,终极跨平台方案 </small><big>uni-app x,终极跨平台方案</big><strong>uni-appx,终极跨平台方案 </strong><i>uni-app x,终极跨平台方案 </i><u>uni-app x,终极跨平台方案 </u><del>uni-appx,终极跨平台方案</del></p><h3>uniCloud,js serverless云服务</h3><p style="text-decoration: line-through;"><small>uniCloud,js serverless云服务 </small><big>uniCloud,jsserverless云服务</big><strong>uniCloud,js serverless云服务 </strong><i>uniCloud,js serverless云服务 </i><u>uniCloud,jsserverless云服务</u><del>uniCloud,js serverless云服务</del></p><h4>uts,大一统语言</h4><p style="text-align: center;"><small>uts,大一统语言 </small><big>uts,大一统语言 </big><strong>uts,大一统语言</strong><i>uts,大一统语言</i><u>uts,大一统语言 </u><del>uts,大一统语言</del></p><h5>uniMPSdk,让你的App具备小程序能力</h5><h6>uni-admin,开源、现成的全端管理后台</h6><ul><li style="color: red; text-align: left;">uni-app x,终极跨平台方案</li><li style="color: green; text-align: center;">uni-app x,终极跨平台方案</li><li style="color: blue; text-align: right;">uni-app x,终极跨平台方案</li></ul><a href="https://uniapp.dcloud.net.cn">uni-app</a><br/><img src="/unidoc/zh/uni@2x.png"></img></p>',
|
||||
baseURL: 'https://qiniu-web-assets.dcloud.net.cn'
|
||||
}
|
||||
webviewContext?.loadData(options)
|
||||
// webviewElement?.loadData(options)
|
||||
}
|
||||
// #endif
|
||||
|
||||
const message = (event: UniWebViewMessageEvent) => {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
}
|
||||
|
||||
const error = (event: UniWebViewErrorEvent) => {
|
||||
data.loadError = true
|
||||
console.log(JSON.stringify(event.detail));
|
||||
if (data.autoTest) {
|
||||
data.eventError = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"errCode": event.detail.errCode,
|
||||
"errMsg": event.detail.errMsg,
|
||||
"url": event.detail.url,
|
||||
"fullUrl": event.detail.fullUrl,
|
||||
"src": event.detail.src
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const loading = (event: UniWebViewLoadingEvent) => {
|
||||
data.loadingCount++
|
||||
// console.log(JSON.stringify(event.detail));
|
||||
// #ifndef APP-HARMONY
|
||||
// 鸿蒙平台在页面加载完成后,重复调用 reload 不会触发 loading 事件
|
||||
if (data.autoTest) {
|
||||
// #endif
|
||||
data.eventLoading = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"src": event.detail.src
|
||||
};
|
||||
// #ifndef APP-HARMONY
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
const load = (event: UniWebViewLoadEvent) => {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
// #ifdef APP
|
||||
data.canGoBack = canWebViewGoBack('web-view');
|
||||
data.canGoForward = canWebViewGoForward('web-view');
|
||||
// #endif
|
||||
// #ifndef APP-HARMONY
|
||||
// 鸿蒙平台在页面加载完成后,重复调用 reload 不会触发 loading 事件
|
||||
if (data.autoTest) {
|
||||
// #endif
|
||||
data.eventLoad = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"src": event.detail.src,
|
||||
"url": event.detail.url,
|
||||
};
|
||||
// #ifndef APP-HARMONY
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
const download = (event: UniWebViewDownloadEvent) => {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
uni.showModal({
|
||||
content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
|
||||
const contentheightchange = (event: UniWebViewContentHeightChangeEvent) => {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
data.eventContentHeightChange = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"isValidHeight": event.detail.height > 0
|
||||
};
|
||||
}
|
||||
|
||||
const confirm = (event: UniInputConfirmEvent) => {
|
||||
let url = event.detail.value;
|
||||
if (!url.startsWith('https://') && !url.startsWith('http://')) {
|
||||
url = 'https://' + url;
|
||||
}
|
||||
data.src = url;
|
||||
}
|
||||
|
||||
const changeHorizontalScrollBarAccess = (event: UniSwitchChangeEvent) => {
|
||||
data.horizontalScrollBarAccess = event.detail.value;
|
||||
}
|
||||
|
||||
const changeVerticalScrollBarAccess = (event: UniSwitchChangeEvent) => {
|
||||
data.verticalScrollBarAccess = event.detail.value;
|
||||
}
|
||||
|
||||
const changeBounces = (event: UniSwitchChangeEvent) => {
|
||||
data.bounces = event.detail.value;
|
||||
}
|
||||
|
||||
const changeDisableUserSelectMenu = (event: UniSwitchChangeEvent) => {
|
||||
data.disableUserSelectMenu = event.detail.value;
|
||||
}
|
||||
|
||||
const touchstart = (event: UniTouchEvent) => {
|
||||
if (data.autoTest) {
|
||||
data.isTouchEnable = event.touches[0].clientX > 0 && event.touches[0].clientY > 0;
|
||||
}
|
||||
}
|
||||
|
||||
const tap = (event: UniPointerEvent) => {
|
||||
if (data.autoTest) {
|
||||
data.isTouchEnable = event.clientX > 0 && event.clientY > 0;
|
||||
}
|
||||
}
|
||||
|
||||
const checkNativeWebView = (): boolean => {
|
||||
// #ifdef APP
|
||||
return hasNativeView('web-view')
|
||||
// #endif
|
||||
// #ifdef WEB
|
||||
return true
|
||||
// #endif
|
||||
}
|
||||
|
||||
const checkLoadingCount = () => {
|
||||
// #ifndef APP-HARMONY
|
||||
// 鸿蒙平台在页面加载完成后,重复调用 reload 不会触发 loading 事件
|
||||
data.loadingCount = 0
|
||||
// #endif
|
||||
// webviewContext?.reload();
|
||||
webviewContext?.reload();
|
||||
}
|
||||
|
||||
const currentInstance = getCurrentInstance()
|
||||
|
||||
onReady(() => {
|
||||
// TODO web 实现createWebviewContext
|
||||
// #ifdef APP
|
||||
webviewContext = uni.createWebViewContext('web-view', currentInstance?.proxy)
|
||||
console.log("webviewContext:",webviewContext)
|
||||
// NOTE 绑定到 this 上会被代理导致无法调用方法
|
||||
webviewElement = uni.getElementById('web-view') as UniWebViewElement //推荐使用element,功能更丰富
|
||||
console.log("webviewElement:",webviewElement)
|
||||
// console.log('url: ',this.webviewContext?.getAttribute("src"));
|
||||
// this.webviewContext?.setAttribute("src","https://ext.dcloud.net.cn/")
|
||||
// #endif
|
||||
})
|
||||
|
||||
onUnload(() => {
|
||||
webviewElement = null;
|
||||
webviewContext = null;
|
||||
})
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
reload,
|
||||
checkNativeWebView,
|
||||
checkLoadingCount,
|
||||
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
|
||||
getContentHeight,
|
||||
loadData
|
||||
// #endif
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.margin-left-5 {
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
.align-items-center {
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,122 @@
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isHarmony = platformInfo.startsWith('harmony')
|
||||
const isIOS = platformInfo.startsWith('ios')
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
const isAppWebView = process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true'
|
||||
|
||||
describe('component-native-web-view', () => {
|
||||
if(isWeb || isMP || isAppWebView || isIOS || isHarmony){
|
||||
it('not support', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page;
|
||||
let start = 0;
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch('/pages/component/web-view/web-view/web-view-local');
|
||||
await page.waitFor(1000);
|
||||
});
|
||||
|
||||
it('check_load_url', async () => {
|
||||
expect(await page.data('data.loadError')).toBe(false)
|
||||
});
|
||||
|
||||
it('screenshot', async () => {
|
||||
await page.waitFor(async () => {
|
||||
return await page.data('data.loadFinish') === true;
|
||||
});
|
||||
const image = await program.screenshot({
|
||||
fullPage: true
|
||||
});
|
||||
expect(image).toSaveImageSnapshot();
|
||||
});
|
||||
|
||||
it('test event download', async () => {
|
||||
await page.setData({
|
||||
data: { autoTest: true }
|
||||
});
|
||||
await page.callMethod('testEventDownload');
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventDownload')) || (Date.now() - start > 1000);
|
||||
});
|
||||
if (isHarmony) {
|
||||
await page.waitFor(1500);
|
||||
// 关闭弹框
|
||||
await program.tap({ x: 26, y: 80 })
|
||||
|
||||
expect(await page.data('data.eventDownload')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'download',
|
||||
url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniapp-x.apk',
|
||||
userAgent: 'Mobile',
|
||||
contentDisposition: `attachment; filename="hello-uniapp-x.apk"; filename*=utf-8''hello-uniapp-x.apk`,
|
||||
mimetype: 'application/vnd.android.package-archive',
|
||||
isContentLengthValid: true
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isIOS) {
|
||||
// expect(await page.data('eventDownload')).toEqual({
|
||||
// tagName: 'WEB-VIEW',
|
||||
// type: 'download',
|
||||
// url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniapp-x.apk',
|
||||
// userAgent: `uni-app-x/${process.env.HX_Version.split('-')[0].split('.').slice(0, 2).join('.')}`,
|
||||
// contentDisposition: '',
|
||||
// mimetype: 'application/vnd.android.package-archive',
|
||||
// isContentLengthValid: true
|
||||
// });
|
||||
return;
|
||||
}
|
||||
const infos = process.env.uniTestPlatformInfo.split(' ');
|
||||
const version = parseInt(infos[infos.length - 1]);
|
||||
if (version == 8) return; // android8测试结果不稳定
|
||||
if (version >= 9) {
|
||||
expect(await page.data('data.eventDownload')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'download',
|
||||
url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniapp-x.apk',
|
||||
userAgent: `uni-app-x/${process.env.HX_Version.split('-')[0].split('.').slice(0, 2).join('.')}`,
|
||||
contentDisposition: `attachment; filename="hello-uniapp-x.apk"; filename*=utf-8''hello-uniapp-x.apk`,
|
||||
mimetype: 'application/vnd.android.package-archive',
|
||||
isContentLengthValid: true
|
||||
});
|
||||
} else if (version >= 7) { // 低版本webview内核,部分属性无有效值
|
||||
expect(await page.data('data.eventDownload')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'download',
|
||||
url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniapp-x.apk',
|
||||
userAgent: '',
|
||||
contentDisposition: '',
|
||||
mimetype: '',
|
||||
isContentLengthValid: false
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('test event message', async () => {
|
||||
const infos = process.env.uniTestPlatformInfo.split(' ');
|
||||
const version = parseInt(infos[infos.length - 1]);
|
||||
if (isAndroid && version > 6) {
|
||||
await page.callMethod('testEventMessage');
|
||||
start = Date.now();
|
||||
await page.waitFor(async () => {
|
||||
return (await page.data('data.eventMessage')) || (Date.now() - start > 500);
|
||||
});
|
||||
expect(await page.data('data.eventMessage')).toEqual({
|
||||
tagName: 'WEB-VIEW',
|
||||
type: 'message',
|
||||
data: [{
|
||||
action: 'message'
|
||||
}]
|
||||
});
|
||||
}
|
||||
await page.setData({
|
||||
data: { autoTest: false }
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,109 @@
|
||||
<template>
|
||||
<web-view id="web-view" ref="web-view" class="web-view" src="/hybrid/html/local.html" @message="message"
|
||||
@error="error" @loading="loading" @load="load" @download="download">
|
||||
</web-view>
|
||||
<button @click="evalJS">evalJS</button>
|
||||
<!-- #ifdef APP-ANDROID || APP-IOS -->
|
||||
<view class="safe-area-inset-bottom"></view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
type DataType = {
|
||||
loadError: boolean,
|
||||
loadFinish: boolean,
|
||||
// 自动化测试
|
||||
autoTest: boolean,
|
||||
eventMessage: UTSJSONObject | null,
|
||||
eventDownload: UTSJSONObject | null,
|
||||
}
|
||||
|
||||
// 使用reactive避免ref数据在自动化测试中无法访问
|
||||
const data = reactive({
|
||||
loadError: false,
|
||||
loadFinish: false,
|
||||
// 自动化测试
|
||||
autoTest: false,
|
||||
eventMessage: null as UTSJSONObject | null,
|
||||
eventDownload: null as UTSJSONObject | null,
|
||||
} as DataType)
|
||||
|
||||
function evalJS() {
|
||||
const webview = uni.getElementById('web-view') as UniWebViewElement
|
||||
webview.evalJS('postMessage("evalJS")')
|
||||
}
|
||||
|
||||
function message(event : UniWebViewMessageEvent) {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
let contentStr = JSON.stringify(event.detail);
|
||||
// #ifdef APP-IOS
|
||||
contentStr = JSON.stringify(event.detail.data[0]);
|
||||
// #endif
|
||||
uni.showModal({
|
||||
content: contentStr,
|
||||
showCancel: false
|
||||
});
|
||||
if (data.autoTest) {
|
||||
data.eventMessage = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"data": event.detail.data
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function error(event : UniWebViewErrorEvent) {
|
||||
// loadError.value = true
|
||||
console.log(JSON.stringify(event.detail));
|
||||
}
|
||||
|
||||
function loading(event : UniWebViewLoadingEvent) {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
}
|
||||
|
||||
function load(event : UniWebViewLoadEvent) {
|
||||
data.loadFinish = true;
|
||||
console.log(JSON.stringify(event.detail));
|
||||
}
|
||||
|
||||
function download(event : UniWebViewDownloadEvent) {
|
||||
console.log(JSON.stringify(event.detail));
|
||||
if (data.autoTest) {
|
||||
const arr = event.detail.userAgent.split(' ');
|
||||
data.eventDownload = {
|
||||
"tagName": event.target?.tagName,
|
||||
"type": event.type,
|
||||
"url": event.detail.url,
|
||||
"userAgent": arr[arr.length - 1],
|
||||
"contentDisposition": event.detail.contentDisposition,
|
||||
"mimetype": event.detail.mimetype,
|
||||
"isContentLengthValid": Math.trunc(event.detail.contentLength / 1024 / 1024) > 1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 自动化测试
|
||||
function testEventDownload() {
|
||||
uni.createWebViewContext('web-view')?.evalJS("document.getElementsByTagName('a')[0].click()");
|
||||
}
|
||||
|
||||
function testEventMessage() {
|
||||
uni.createWebViewContext('web-view')?.evalJS("document.getElementById('postMessage').click()");
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
testEventDownload,
|
||||
testEventMessage
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.web-view {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.safe-area-inset-bottom {
|
||||
height: var(--uni-safe-area-inset-bottom);
|
||||
}
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户