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

test: 优化截图对比测试

上级 11c703bf
...@@ -7,23 +7,14 @@ ...@@ -7,23 +7,14 @@
<view class="uni-common-mt"> <view class="uni-common-mt">
<view class="uni-list"> <view class="uni-list">
<view class="uni-list"> <view class="uni-list">
<view <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
class="uni-list-cell"
v-for="(item, _) in items"
style="align-items: center"
>
<view class="uni-pd"> <view class="uni-pd">
<view class="uni-label" style="width: 180px">{{ <view class="uni-label" style="width: 180px">{{
item.label item.label
}}</view> }}</view>
</view> </view>
<view class="uni-list-cell-db"> <view class="uni-list-cell-db">
<textarea <textarea :auto-height="true" :disabled="true" placeholder="未获取" :value="item.value" />
:auto-height="true"
:disabled="true"
placeholder="未获取"
:value="item.value"
/>
</view> </view>
</view> </view>
</view> </view>
...@@ -40,40 +31,44 @@ ...@@ -40,40 +31,44 @@
<!-- #endif --> <!-- #endif -->
</template> </template>
<script> <script>
type Item = { import { setDevicePixelRatio } from '@/store/index.uts'
type Item = {
label : string, label : string,
value : string, value : string,
} }
export default { export default {
data() { data() {
return { return {
title: 'getDeviceInfo', title: 'getDeviceInfo',
items: [] as Item[], items: [] as Item[],
} }
}, },
onUnload:function(){ onUnload: function () {
}, },
methods: { methods: {
getDeviceInfo: function () { getDeviceInfo: function () {
const res = uni.getDeviceInfo(); const res = uni.getDeviceInfo();
// 获取像素比, 供截图对比使用
setDevicePixelRatio(res.devicePixelRatio !== null ? parseFloat(res.devicePixelRatio!) : 1)
this.items = [] as Item[]; this.items = [] as Item[];
for(const key in res){ for (const key in res) {
const value = res[key]; const value = res[key];
if(value != null){ if (value != null) {
const item = { const item = {
label: key, label: key,
value: "" + ((typeof value == "object")? JSON.stringify(value) : value) value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
} as Item; } as Item;
this.items.push(item); this.items.push(item);
} }
} }
} }
} }
} }
</script> </script>
<style> <style>
.uni-pd { .uni-pd {
padding-left: 30rpx; padding-left: 30rpx;
} }
</style> </style>
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<view class="uni-common-mt"> <view class="uni-common-mt">
<view class="uni-list"> <view class="uni-list">
<view class="uni-list-cell" v-for="(item,_) in items" style="align-items: center;"> <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
<view class="uni-pd"> <view class="uni-pd">
<view class="uni-label" style="width:180px;">{{item.label}}</view> <view class="uni-label" style="width: 180px">{{ item.label }}</view>
</view> </view>
<view class="uni-list-cell-db"> <view class="uni-list-cell-db">
<textarea :auto-height="true" :disabled="true" placeholder="未获取" :value="item.value" /> <textarea :auto-height="true" :disabled="true" placeholder="未获取" :value="item.value" />
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
</view> </view>
</template> </template>
<script> <script>
import { setHeaderHeight } from '@/store/index.uts'
type Item = { type Item = {
label : string, label : string,
value : string, value : string,
...@@ -37,20 +39,22 @@ ...@@ -37,20 +39,22 @@
methods: { methods: {
getWindowInfo: function () { getWindowInfo: function () {
const res = uni.getWindowInfo(); const res = uni.getWindowInfo();
// 获取状态栏+导航栏高度, 供截图对比使用
setHeaderHeight(res.screenHeight - res.windowHeight);
this.items = [] as Item[]; this.items = [] as Item[];
for(const key in res){ for (const key in res) {
const value = res[key]; const value = res[key];
if(value != null){ if (value != null) {
const item = { const item = {
label: key, label: key,
value: "" + ((typeof value == "object")? JSON.stringify(value) : value) value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
} as Item; } as Item;
this.items.push(item); this.items.push(item);
} }
} }
}, },
//自动化测试例专用 //自动化测试例专用
jest_getWindowInfo():GetWindowInfoResult{ jest_getWindowInfo() : GetWindowInfoResult {
return uni.getWindowInfo(); return uni.getWindowInfo();
}, },
} }
......
...@@ -37,7 +37,6 @@ const pages = [ ...@@ -37,7 +37,6 @@ const pages = [
"pages/component/scroll-view/scroll-view-refresher", "pages/component/scroll-view/scroll-view-refresher",
"pages/tabBar/API", "pages/tabBar/API",
"pages/API/get-app/get-app", "pages/API/get-app/get-app",
"pages/API/exit/exit",
"pages/API/get-current-pages/get-current-pages", "pages/API/get-current-pages/get-current-pages",
"pages/API/get-launch-options-sync/get-launch-options-sync", "pages/API/get-launch-options-sync/get-launch-options-sync",
"pages/API/navigator/navigator", "pages/API/navigator/navigator",
...@@ -79,7 +78,6 @@ const pages = [ ...@@ -79,7 +78,6 @@ const pages = [
"pages/API/unicloud-file-api/unicloud-file-api", "pages/API/unicloud-file-api/unicloud-file-api",
"pages/API/unicloud-database/unicloud-database", "pages/API/unicloud-database/unicloud-database",
"pages/API/get-window-info/get-window-info", "pages/API/get-window-info/get-window-info",
"pages/API/element-takesnapshot/element-takesnapshot",
"pages/API/get-element-by-id/get-element-by-id", "pages/API/get-element-by-id/get-element-by-id",
"pages/API/get-element-by-id/get-element-by-id-multiple-root-node", "pages/API/get-element-by-id/get-element-by-id-multiple-root-node",
"pages/API/navigator/new-page/onLoad", "pages/API/navigator/new-page/onLoad",
...@@ -148,7 +146,6 @@ const pages = [ ...@@ -148,7 +146,6 @@ const pages = [
"pages/template/navbar-lite/navbar-lite", "pages/template/navbar-lite/navbar-lite",
"pages/template/pull-zoom-image/pull-zoom-image", "pages/template/pull-zoom-image/pull-zoom-image",
"pages/template/scroll-fold-nav/scroll-fold-nav", "pages/template/scroll-fold-nav/scroll-fold-nav",
"pages/template/scroll-sticky/scroll-sticky",
// 自动获取焦点 // 自动获取焦点
// "pages/component/input/input", // "pages/component/input/input",
...@@ -163,7 +160,6 @@ const pages = [ ...@@ -163,7 +160,6 @@ const pages = [
// 动态内容 // 动态内容
// "pages/component/web-view/web-view" // "pages/component/web-view/web-view"
// "pages/template/calendar/calendar",
// "pages/template/custom-refresher/custom-refresher", // "pages/template/custom-refresher/custom-refresher",
// "pages/component/picker-view/picker-view", // "pages/component/picker-view/picker-view",
// "pages/template/long-list/long-list", // "pages/template/long-list/long-list",
...@@ -184,6 +180,7 @@ const pages = [ ...@@ -184,6 +180,7 @@ const pages = [
// "pages/API/websocket-global/websocket-global" // "pages/API/websocket-global/websocket-global"
// 仅 app // 仅 app
// "pages/template/calendar/calendar",
// pages/component/unicloud-db-contacts/list // pages/component/unicloud-db-contacts/list
// pages/component/unicloud-db-contacts/add // pages/component/unicloud-db-contacts/add
// pages/component/unicloud-db-contacts/edit // pages/component/unicloud-db-contacts/edit
...@@ -196,8 +193,12 @@ const pages = [ ...@@ -196,8 +193,12 @@ const pages = [
// pages/API/facial-recognition-verify/facial-recognition-verify // pages/API/facial-recognition-verify/facial-recognition-verify
// pages/API/get-file-system-manager/get-file-system-manager // pages/API/get-file-system-manager/get-file-system-manager
// "pages/API/install-apk/install-apk", // "pages/API/install-apk/install-apk",
// "pages/template/scroll-sticky/scroll-sticky",
// "pages/API/exit/exit",
// "pages/API/element-takesnapshot/element-takesnapshot",
// 仅 web // 仅 web
// pages/template/browser-canvas/browser-canvas
// pages/template/schema/schema // pages/template/schema/schema
// pages/template/share/share // pages/template/share/share
]; ];
...@@ -208,6 +209,12 @@ const childToParentPagesMap = new Map([ ...@@ -208,6 +209,12 @@ const childToParentPagesMap = new Map([
], ],
]); ]);
const customNavigationPages = [
"pages/template/navbar-lite/navbar-lite",
"pages/template/pull-zoom-image/pull-zoom-image",
"pages/template/scroll-fold-nav/scroll-fold-nav"
]
const needAdbScreenshotPages = [ const needAdbScreenshotPages = [
"pages/tabBar/component", "pages/tabBar/component",
"pages/tabBar/API", "pages/tabBar/API",
...@@ -218,6 +225,7 @@ const needAdbScreenshotPages = [ ...@@ -218,6 +225,7 @@ const needAdbScreenshotPages = [
"pages/API/loading/loading", "pages/API/loading/loading",
"pages/API/toast/toast", "pages/API/toast/toast",
]; ];
const needAdbScreenshot = (url) => { const needAdbScreenshot = (url) => {
return needAdbScreenshotPages.includes(url); return needAdbScreenshotPages.includes(url);
}; };
...@@ -231,12 +239,19 @@ describe("shot-compare", () => { ...@@ -231,12 +239,19 @@ describe("shot-compare", () => {
let pageIndex = 0; let pageIndex = 0;
let baseSrc = ""; let baseSrc = "";
beforeAll(async () => { beforeAll(async () => {
// 获取导航栏+状态栏高度
page = await program.reLaunch('/pages/API/get-window-info/get-window-info')
await page.callMethod('getWindowInfo')
// 获取设备像素比
page = await program.reLaunch('/pages/API/get-device-info/get-device-info')
await page.callMethod('getDeviceInfo')
page = await program.reLaunch(PAGE_PATH); page = await program.reLaunch(PAGE_PATH);
await page.waitFor(500); await page.waitFor(500);
// set webview-screenshot-comparison page baseSrc // set webview-screenshot-comparison page baseSrc
baseSrc = baseSrc =
process.env.UNI_WEB_SERVICE_URL ? `${process.env.UNI_WEB_SERVICE_URL}/#/` : "http://test.dcloud.io/unix_h5_build/98_dev_hello-uni-app-x/#/"; process.env.UNI_WEB_SERVICE_URL ? `${process.env.UNI_WEB_SERVICE_URL}/#/` :
"http://test.dcloud.io/unix_h5_build/98_dev_hello-uni-app-x/#/";
page.setData({ page.setData({
baseSrc, baseSrc,
}); });
...@@ -252,6 +267,20 @@ describe("shot-compare", () => { ...@@ -252,6 +267,20 @@ describe("shot-compare", () => {
test.each(pages)("%s", async () => { test.each(pages)("%s", async () => {
const isNeedAdbScreenshot = needAdbScreenshot(pages[pageIndex]); const isNeedAdbScreenshot = needAdbScreenshot(pages[pageIndex]);
const isCustomNavigation = customNavigationPages.includes(pages[pageIndex]);
const {
headerHeight,
devicePixelRatio
} = await page.data();
const screenshotParams = {
fullPage: true,
adb: isNeedAdbScreenshot,
// adb 截图时跳过状态栏
area: {
x: 0,
y: (headerHeight - 44) * devicePixelRatio,
},
}
const screenshotPath = `__webview__${pages[pageIndex].replace(/\//g, "-")}`; const screenshotPath = `__webview__${pages[pageIndex].replace(/\//g, "-")}`;
// web in webview screenshot // web in webview screenshot
...@@ -259,11 +288,10 @@ describe("shot-compare", () => { ...@@ -259,11 +288,10 @@ describe("shot-compare", () => {
if (childToParentPagesMap.get(pages[pageIndex])) { if (childToParentPagesMap.get(pages[pageIndex])) {
await page.setData({ await page.setData({
src: `${baseSrc}${childToParentPagesMap.get(pages[pageIndex])}`, src: `${baseSrc}${childToParentPagesMap.get(pages[pageIndex])}`,
isLoaded: false, isLoaded: false
needRemoveWebHead: !isNeedAdbScreenshot,
}); });
await page.waitFor(async () => { await page.waitFor(async () => {
const isLoaded = page.data("isLoaded"); const isLoaded = await page.data("isLoaded");
return isLoaded || Date.now() - startTime > 10000; return isLoaded || Date.now() - startTime > 10000;
}); });
await page.waitFor(200); await page.waitFor(200);
...@@ -276,21 +304,18 @@ describe("shot-compare", () => { ...@@ -276,21 +304,18 @@ describe("shot-compare", () => {
const startTime = Date.now(); const startTime = Date.now();
await page.waitFor(async () => { await page.waitFor(async () => {
const isLoaded = page.data("isLoaded"); const isLoaded = await page.data("isLoaded");
return isLoaded || Date.now() - startTime > 3000; return isLoaded || Date.now() - startTime > 3000;
}); });
await page.waitFor(200); await page.waitFor(800);
if (pages[pageIndex].includes("load-font-face")) { if (pages[pageIndex].includes("load-font-face")) {
await page.waitFor(3000); await page.waitFor(3000);
} }
// web 端非 adb 截图时设置 offsetY 移除导航栏
const webSnapshot = await program.screenshot({ const webSnapshot = await program.screenshot({
fullPage: isNeedAdbScreenshot ? false : true, ...screenshotParams,
adb: isNeedAdbScreenshot ? true : false, offsetY: `${isCustomNavigation ? 0 : headerHeight}`
area: {
x: 0,
y: 100,
},
}); });
expect(webSnapshot).toMatchImageSnapshot({ expect(webSnapshot).toMatchImageSnapshot({
customSnapshotIdentifier() { customSnapshotIdentifier() {
...@@ -307,14 +332,7 @@ describe("shot-compare", () => { ...@@ -307,14 +332,7 @@ describe("shot-compare", () => {
if (pages[pageIndex].includes("load-font-face")) { if (pages[pageIndex].includes("load-font-face")) {
await page.waitFor(3000); await page.waitFor(3000);
} }
const appAndroidSnapshot = await program.screenshot({ const appAndroidSnapshot = await program.screenshot(screenshotParams);
fullPage: isNeedAdbScreenshot ? false : true,
adb: isNeedAdbScreenshot ? true : false,
area: {
x: 0,
y: 100,
},
});
expect(appAndroidSnapshot).toMatchImageSnapshot({ expect(appAndroidSnapshot).toMatchImageSnapshot({
customSnapshotIdentifier() { customSnapshotIdentifier() {
return screenshotPath; return screenshotPath;
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
</template> </template>
<script> <script>
import { state } from '@/store/index.uts'
export default { export default {
data() { data() {
return { return {
...@@ -12,7 +14,8 @@ ...@@ -12,7 +14,8 @@
src: '', src: '',
webviewContext: null as WebviewContext | null, webviewContext: null as WebviewContext | null,
isLoaded: false, isLoaded: false,
needRemoveWebHead: false, headerHeight: state.headerHeight,
devicePixelRatio: state.devicePixelRatio,
webviewStyles: { webviewStyles: {
progress: false progress: false
} }
...@@ -22,31 +25,39 @@ ...@@ -22,31 +25,39 @@
this.webviewContext = uni.createWebviewContext('webview-screenshot', this) this.webviewContext = uni.createWebviewContext('webview-screenshot', this)
}, },
methods: { methods: {
removeWebHead() {
this.webviewContext?.evalJS(`document.querySelector('uni-page-head').style.display='none'`);
},
appendWebHeadPlaceholder() { appendWebHeadPlaceholder() {
if (src.indexOf('pages/template/navbar-lite/navbar-lite') > -1) {
this.webviewContext?.evalJS(`
const uniNavbar = document.querySelector('.uni-navbar');
uniNavbar.style.paddingTop = '${state.headerHeight - 44}px';
`)
} else if (src.indexOf('pages/template/scroll-fold-nav/scroll-fold-nav') > -1) {
this.webviewContext?.evalJS(`
const heightSeat = document.querySelector('.height-seat');
heightSeat.style.height = '125px';
heightSeat.style.backgroundColor = '#f0f8ff';
const topBox = document.querySelector('.top-box');
topBox.style.top = '35px';
`)
} else {
this.webviewContext?.evalJS(` this.webviewContext?.evalJS(`
const hasWebHeadPlaceholder = document.querySelector('.web-head-placeholder-for-screenshot-comparison'); const hasWebHeadPlaceholder = document.querySelector('.web-head-placeholder-for-screenshot-comparison');
if (hasWebHeadPlaceholder) { if (hasWebHeadPlaceholder) {
return; return;
} }
const webHeadPlaceholder = document.createElement('div'); const webHeadPlaceholder = document.createElement('div');
webHeadPlaceholder.style.height = '52px'; webHeadPlaceholder.style.height = '${state.headerHeight - 44}px';
webHeadPlaceholder.style.backgroundColor = '#007aff'; webHeadPlaceholder.style.backgroundColor = '#007aff';
webHeadPlaceholder.classList.add('web-head-placeholder-for-screenshot-comparison'); webHeadPlaceholder.classList.add('web-head-placeholder-for-screenshot-comparison');
const uniPage = document.querySelector('uni-page'); const uniPage = document.querySelector('uni-page');
const uniPageHead = document.querySelector('uni-page-head'); const uniPageHead = document.querySelector('uni-page-head');
uniPage.insertBefore(webHeadPlaceholder, uniPageHead); uniPage.insertBefore(webHeadPlaceholder, uniPageHead);
`); `);
}
}, },
loaded() { loaded() {
this.isLoaded = true this.isLoaded = true
if (this.needRemoveWebHead) {
this.removeWebHead();
} else {
this.appendWebHeadPlaceholder(); this.appendWebHeadPlaceholder();
}
}, },
error(event : WebViewErrorEvent) { error(event : WebViewErrorEvent) {
console.log('webview load error', JSON.stringify(event.detail)); console.log('webview load error', JSON.stringify(event.detail));
......
type State = { type State = {
lifeCycleNum: number lifeCycleNum : number,
// 状态栏 + 导航栏高度
headerHeight : number,
// 设备像素比
devicePixelRatio : number
} }
export const state = reactive({ lifeCycleNum: 0 } as State) export const state = reactive({ lifeCycleNum: 0, headerHeight: 0, devicePixelRatio: 1 } as State)
export const setLifeCycleNum = (num: number) => { export const setLifeCycleNum = (num : number) => {
state.lifeCycleNum = num state.lifeCycleNum = num
} }
export const setHeaderHeight = (height : number) => {
state.headerHeight = height
}
export const setDevicePixelRatio = (devicePixelRatio : number) => {
state.devicePixelRatio = devicePixelRatio
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册