Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

test: add pixel diff e2e (#2924)

Wenzhao Huf1b8dc138f05
diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml
index 2ad9a90a5c..6aadc00c27 100644
--- a/.github/workflows/playwright.yml
+++ b/.github/workflows/playwright.yml
@@ -7,8 +7,8 @@ on:
branches: [main, dev]
jobs:
- test:
- timeout-minutes: 60
+ e2e_test:
+ timeout-minutes: 10
runs-on: ubuntu-latest
env:
@@ -36,7 +36,7 @@ jobs:
run: pnpm exec playwright test
- uses: actions/upload-artifact@v3
- if: always()
+ if: failure() # only upload artifact when failed
with:
name: playwright-report
path: playwright-report/
diff --git a/.github/workflows/update-snapshots.yml b/.github/workflows/update-snapshots.yml
new file mode 100644
index 0000000000..7f434d59bb
--- /dev/null
+++ b/.github/workflows/update-snapshots.yml
@@ -0,0 +1,68 @@
+# https://mmazzarolo.com/blog/2022-09-09-visual-regression-testing-with-playwright-and-github-actions/
+
+# This workflow's goal is forcing an update of the reference snapshots used
+# by Playwright tests. It runs whenever you post a new pull request comment
+# that strictly matches the "/update-snapshots".
+# From a high-level perspective, it works like this:
+# 1. Because of a GitHub Action limitation, this workflow is triggered on every
+# comment posted on a issue or pull request. We manually interrupt it unless
+# the comment content strictly matches "/update-snapshots" and we're in a
+# pull request.
+# 2. Use the GitHub API to grab the information about the branch name and SHA of
+# the latest commit of the current pull request.
+# 3. Update the Playwright reference snapshots based on the UI of this branch.
+# 4. Commit the newly generated Playwright reference snapshots into this branch.
+name: 🎭 Update Snapshots
+
+on:
+ # It looks like you can't target PRs-only comments:
+ # https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
+ # So we must run this workflow every time a new comment is added to issues
+ # and pull requests
+ issue_comment:
+ types: [created]
+
+jobs:
+ update_snapshots:
+ # Run this job only on comments of pull requests that strictly match
+ # the "/update-snapshots" string
+ if: ${{ github.event.issue.pull_request && github.event.comment.body == '/update-snapshots'}}
+ timeout-minutes: 10
+ runs-on: ubuntu-latest
+ steps:
+ # Checkout and do a deep fetch to load all commit IDs
+ - uses: actions/checkout@v4
+ with:
+ fetch-depth: 0 # Load all commits
+ token: ${{ secrets.GITHUB_TOKEN }}
+
+ # Get the SHA and branch name of the comment's pull request
+ # We must use the GitHub API to retrieve these information because they're
+ # not accessibile within workflows triggered by "issue_comment"
+ - name: Get SHA and branch name
+ id: get-branch-and-sha
+ run: |
+ sha_and_branch=$(\
+ curl \
+ -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
+ https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} \
+ | jq -r '.head.sha," ",.head.ref');
+ echo "::set-output name=sha::$(echo $sha_and_branch | cut -d " " -f 1)";
+ echo "::set-output name=branch::$(echo $sha_and_branch | cut -d " " -f 2)"
+
+ - name: Fetch Branch
+ run: git fetch
+ - name: Checkout Updating Branch
+ run: git checkout ${{ steps.get-branch-and-sha.outputs.branch }}
+
+ - name: Setup Node.js
+ uses: ./.github/actions/setup-node
+
+ - name: Install Playwright Browsers
+ run: npx playwright install --with-deps chromium
+
+ - name: Update Snapshots
+ run: npx playwright test --update-snapshots --reporter=list
+ - uses: stefanzweifel/git-auto-commit-action@v4
+ with:
+ commit_message: 'ci: update snapshots'
diff --git a/e2e/disposing/README.md b/e2e/disposing/README.md
new file mode 100644
index 0000000000..fabdfaee9f
--- /dev/null
+++ b/e2e/disposing/README.md
@@ -0,0 +1 @@
+These tests ensure that no error is thrown when the user is disposing a unit or a Univer instance.
diff --git a/e2e/e2e.d.ts b/e2e/e2e.d.ts
new file mode 100644
index 0000000000..3f0821bc8c
--- /dev/null
+++ b/e2e/e2e.d.ts
@@ -0,0 +1,15 @@
+// The type definition is copied from:
+// examples/src/plugins/debugger/controllers/e2e/e2e-memory.controller.ts
+export interface IE2EControllerAPI {
+ loadAndRelease(id: number): Promise<void>;
+ loadDefaultSheet(): Promise<void>;
+ loadDefaultDoc(): Promise<void>;
+ disposeUniver(): Promise<void>;
+}
+
+declare global {
+ // eslint-disable-next-line ts/naming-convention
+ interface Window {
+ E2EControllerAPI: IE2EControllerAPI;
+ }
+}
diff --git a/e2e/memory/memory.spec.ts b/e2e/memory/memory.spec.ts
index 32786c02af..601198912b 100644
--- a/e2e/memory/memory.spec.ts
+++ b/e2e/memory/memory.spec.ts
@@ -19,21 +19,6 @@
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
-// The type definition is copied from:
-// examples/src/plugins/debugger/controllers/e2e/e2e-memory.controller.ts
-export interface IE2EControllerAPI {
- loadAndRelease(id: number): Promise<void>;
- loadDefaultSheet(): Promise<void>;
- disposeUniver(): Promise<void>;
-}
-
-declare global {
- // eslint-disable-next-line ts/naming-convention
- interface Window {
- E2EControllerAPI: IE2EControllerAPI;
- }
-}
-
const MAX_MEMORY_OVERFLOW = 2_500_000;
test('memory', async ({ page }) => {
diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json
new file mode 100644
index 0000000000..9118311939
--- /dev/null
+++ b/e2e/tsconfig.json
@@ -0,0 +1,4 @@
+{
+ "extends": "../common/shared/tsconfigs/node.json",
+ "include": ["**/*.ts", "e2e.d.ts"]
+}
diff --git a/e2e/visual-comparison/README.md b/e2e/visual-comparison/README.md
new file mode 100644
index 0000000000..40d5133f76
--- /dev/null
+++ b/e2e/visual-comparison/README.md
@@ -0,0 +1,4 @@
+# Notes on visual comparison
+
+You should set `maxDiffPixels` on a minimum value. The cursor may be different in snapshots because it is blinking,
+especially in rich-text editors.
diff --git a/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts
new file mode 100644
index 0000000000..190e073dca
--- /dev/null
+++ b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts
@@ -0,0 +1,27 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { expect, test } from '@playwright/test';
+
+test('diff default doc content', async ({ page }) => {
+ await page.goto('http://localhost:3000/docs/');
+ await page.waitForTimeout(2000);
+
+ await page.evaluate(() => window.E2EControllerAPI.loadDefaultDoc());
+ await page.waitForTimeout(5000);
+
+ await expect(page).toHaveScreenshot({ maxDiffPixels: 30 });
+});
diff --git a/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/diff-default-doc-content-1-chromium-linux.png b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/diff-default-doc-content-1-chromium-linux.png
new file mode 100644
index 0000000000..4cd62b455d
Binary files /dev/null and b/e2e/visual-comparison/docs/docs-visual-comparison.spec.ts-snapshots/diff-default-doc-content-1-chromium-linux.png differ
diff --git a/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts b/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts
new file mode 100644
index 0000000000..f3bb3674ac
--- /dev/null
+++ b/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts
@@ -0,0 +1,28 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { expect, test } from '@playwright/test';
+
+test('diff default sheet content', async ({ page }) => {
+ await page.goto('http://localhost:3000/sheets/');
+ await page.waitForTimeout(2000);
+
+ await page.evaluate(() => window.E2EControllerAPI.loadDefaultSheet());
+ await page.waitForTimeout(5000);
+
+ await expect(page).toHaveScreenshot({ maxDiffPixels: 0 });
+});
+
diff --git a/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts-snapshots/diff-default-sheet-content-1-chromium-linux.png b/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts-snapshots/diff-default-sheet-content-1-chromium-linux.png
new file mode 100644
index 0000000000..51b6abb346
Binary files /dev/null and b/e2e/visual-comparison/sheet/sheets-visual-comparison.spec.ts-snapshots/diff-default-sheet-content-1-chromium-linux.png differ
diff --git a/packages/debugger/src/controllers/e2e/data/default-doc.ts b/packages/debugger/src/controllers/e2e/data/default-doc.ts
new file mode 100644
index 0000000000..5ab4ad8d5a
--- /dev/null
+++ b/packages/debugger/src/controllers/e2e/data/default-doc.ts
@@ -0,0 +1,204 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IDocumentData } from '@univerjs/core';
+import { BaselineOffset, BooleanNumber, ColumnSeparatorType, DocumentFlavor, DrawingTypeEnum, ObjectRelativeFromH, ObjectRelativeFromV, PositionedObjectLayoutType, PresetListType, SectionType, WrapTextType } from '@univerjs/core';
+
+// eslint-disable-next-line max-lines-per-function
+export function getDefaultDocData(): IDocumentData {
+ return {
+ id: 'd',
+ drawings: {
+ shapeTest1: {
+ unitId: 'd',
+ subUnitId: 'd',
+ drawingType: DrawingTypeEnum.DRAWING_SHAPE,
+ drawingId: 'shapeTest1',
+ title: 'test shape',
+ description: 'test shape',
+ docTransform: {
+ size: {
+ width: 100,
+ height: 400,
+ },
+ positionH: {
+ relativeFrom: ObjectRelativeFromH.COLUMN,
+ posOffset: 100,
+ },
+ positionV: {
+ relativeFrom: ObjectRelativeFromV.PARAGRAPH,
+ posOffset: 160,
+ },
+ angle: 0,
+ // imageProperties: {
+ // contentUrl: 'https://cnbabylon.com/assets/img/agents.png',
+ // },
+ },
+ layoutType: PositionedObjectLayoutType.WRAP_SQUARE,
+ behindDoc: BooleanNumber.FALSE,
+ wrapText: WrapTextType.BOTH_SIDES,
+ },
+ },
+ body: {
+ dataStream: '在“第1题”工作表中完成以下操作\b上标日期列单元格数据验证,限制只能输入日期(介于1949年1月1日至2099年1月1日)\r细化埋点上报链路和指标方案梳理目前通过每日定时任务发送报告\r自2018年首届进博会举办以来,进博会已经成为全球新品的首发地、前沿技术的首选地、创新服务的首推地。中国这十年”对外开放成就展湖北展区主要以图文、图表、数据、视频、企业展品、实物模型、光电科技等体现湖北十年开放成就、重大开放平台及产业。湖北省共计17家企业、机构的展品将在这一展区展示\r国家主席习近平以视频方式出席在上海举行的第五届中国国际进口博览会开幕式并发表题为《共创开放繁荣的美好未来》的致辞中国将推动各国各方共享深化国际合作机遇,全面深入参与世界贸易组织改革谈判\r中国男排两名现役国手彭世坤和张秉龙分别效力的三得利太阳鸟和东京大熊本轮遭遇。双方经过激战,主场作战的卫冕冠军三得利技高一筹3-2逆转击败对手,力夺第3场胜利\r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 15,
+ ts: {
+ fs: 12,
+ bg: {
+ rgb: 'rgb(200,0,90)',
+ },
+ cl: {
+ rgb: 'rgb(255,130,0)',
+ },
+ },
+ },
+ {
+ st: 17,
+ ed: 18,
+ ts: {
+ fs: 14,
+ bg: {
+ rgb: 'rgb(2,128,2)',
+ },
+ cl: {
+ rgb: 'rgb(0,1,55)',
+ },
+ va: BaselineOffset.SUPERSCRIPT,
+ },
+ },
+ {
+ st: 19,
+ ed: 60,
+ ts: {
+ fs: 14,
+ bg: {
+ rgb: 'rgb(90,128,255)',
+ },
+ cl: {
+ rgb: 'rgb(0,1,255)',
+ },
+ },
+ },
+
+ {
+ st: 62,
+ ed: 90,
+ },
+
+ {
+ st: 92,
+ ed: 233,
+ },
+
+ {
+ st: 235,
+ ed: 326,
+ },
+
+ {
+ st: 328,
+ ed: 405,
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 60,
+ bullet: {
+ listType: PresetListType.ORDER_LIST,
+ listId: 'a',
+ nestingLevel: 0,
+ textStyle: {
+ fs: 20,
+ },
+ },
+ },
+ {
+ startIndex: 91,
+ bullet: {
+ listType: PresetListType.ORDER_LIST,
+ listId: 'a',
+ nestingLevel: 1,
+ textStyle: {
+ fs: 20,
+ },
+ },
+ },
+ {
+ startIndex: 234,
+ bullet: {
+ listType: PresetListType.ORDER_LIST,
+ listId: 'a',
+ nestingLevel: 1,
+ textStyle: {
+ fs: 20,
+ },
+ },
+ },
+ {
+ startIndex: 327,
+ bullet: {
+ listType: PresetListType.ORDER_LIST,
+ listId: 'a',
+ nestingLevel: 1,
+ textStyle: {
+ fs: 20,
+ },
+ },
+ },
+ {
+ startIndex: 406,
+ bullet: {
+ listType: PresetListType.ORDER_LIST,
+ listId: 'a',
+ nestingLevel: 2,
+ textStyle: {
+ fs: 20,
+ },
+ },
+ },
+ ],
+ sectionBreaks: [
+ {
+ startIndex: 407,
+ columnProperties: [
+ {
+ width: 200,
+ paddingEnd: 20,
+ },
+ ],
+ columnSeparatorType: ColumnSeparatorType.NONE,
+ sectionType: SectionType.SECTION_TYPE_UNSPECIFIED,
+ // textDirection: textDirectionDocument,
+ // contentDirection: textDirection!,
+ },
+ ],
+ },
+ documentStyle: {
+ pageSize: {
+ width: 594.3,
+ height: 840.51,
+ },
+ documentFlavor: DocumentFlavor.TRADITIONAL,
+ marginTop: 72,
+ marginBottom: 72,
+ marginRight: 90,
+ marginLeft: 90,
+ },
+ };
+}
diff --git a/packages/debugger/src/controllers/e2e/data/default-sheet.ts b/packages/debugger/src/controllers/e2e/data/default-sheet.ts
new file mode 100644
index 0000000000..3b62c07f71
--- /dev/null
+++ b/packages/debugger/src/controllers/e2e/data/default-sheet.ts
@@ -0,0 +1,65 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IWorkbookData } from '@univerjs/core';
+import { LocaleType } from '@univerjs/core';
+
+export function getDefaultWorkbookData(): IWorkbookData {
+ const DEFAULT_WORKBOOK_DATA_DEMO = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ name: 'sheet1',
+ cellData: {
+ 0: {
+ 3: {
+ f: '=SUM(A1)',
+ si: '3e4r5t',
+ },
+ },
+ 1: {
+ 3: {
+ f: '=SUM(A2)',
+ si: 'OSPtzm',
+ },
+ },
+ 2: {
+ 3: {
+ si: 'OSPtzm',
+ },
+ },
+ 3: {
+ 3: {
+ si: 'OSPtzm',
+ },
+ },
+ },
+ rowCount: 100,
+ columnCount: 100,
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ resources: [
+ ],
+ };
+
+ return DEFAULT_WORKBOOK_DATA_DEMO;
+}
diff --git a/packages/debugger/src/controllers/e2e/e2e-memory.controller.ts b/packages/debugger/src/controllers/e2e/e2e-memory.controller.ts
index 3a5b781660..c7ba0e1ec9 100644
--- a/packages/debugger/src/controllers/e2e/e2e-memory.controller.ts
+++ b/packages/debugger/src/controllers/e2e/e2e-memory.controller.ts
@@ -14,51 +14,10 @@
* limitations under the License.
*/
-import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, LocaleType, OnLifecycle, UniverInstanceType } from '@univerjs/core';
+import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { DisposeUniverCommand } from '../../commands/commands/unit.command';
-
-const DEFAULT_WORKBOOK_DATA_DEMO = {
- id: 'test',
- appVersion: '3.0.0-alpha',
- sheets: {
- sheet1: {
- id: 'sheet1',
- name: 'sheet1',
- cellData: {
- 0: {
- 3: {
- f: '=SUM(A1)',
- si: '3e4r5t',
- },
- },
- 1: {
- 3: {
- f: '=SUM(A2)',
- si: 'OSPtzm',
- },
- },
- 2: {
- 3: {
- si: 'OSPtzm',
- },
- },
- 3: {
- 3: {
- si: 'OSPtzm',
- },
- },
- },
- rowCount: 100,
- columnCount: 100,
- },
- },
- locale: LocaleType.ZH_CN,
- name: '',
- sheetOrder: [],
- styles: {},
- resources: [
- ],
-};
+import { getDefaultWorkbookData } from './data/default-sheet';
+import { getDefaultDocData } from './data/default-doc';
const AWAIT_LOADING_TIMEOUT = 5000;
const AWAIT_DISPOSING_TIMEOUT = 5000;
@@ -66,6 +25,7 @@ const AWAIT_DISPOSING_TIMEOUT = 5000;
export interface IE2EControllerAPI {
loadAndRelease(id: number): Promise<void>;
loadDefaultSheet(): Promise<void>;
+ loadDefaultDoc(): Promise<void>;
disposeUniver(): Promise<void>;
}
@@ -99,20 +59,30 @@ export class E2EMemoryController extends Disposable {
window.E2EControllerAPI = {
loadAndRelease: (id) => this._releaseAndLoad(id),
loadDefaultSheet: () => this._loadDefaultSheet(),
+ loadDefaultDoc: () => this._loadDefaultDoc(),
disposeUniver: () => this._disposeUniver(),
};
}
private async _releaseAndLoad(releaseId: number): Promise<void> {
const unitId = `e2e${releaseId}`;
- this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, { ...DEFAULT_WORKBOOK_DATA_DEMO, id: unitId });
+ const snapshot = getDefaultWorkbookData();
+ snapshot.id = unitId;
+
+ this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, snapshot);
await timer(AWAIT_LOADING_TIMEOUT);
+
this._univerInstanceService.disposeUnit(unitId);
await timer(AWAIT_DISPOSING_TIMEOUT);
}
private async _loadDefaultSheet(): Promise<void> {
- this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
+ this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, getDefaultWorkbookData());
+ await timer(AWAIT_LOADING_TIMEOUT);
+ }
+
+ private async _loadDefaultDoc(): Promise<void> {
+ this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_DOC, getDefaultDocData());
await timer(AWAIT_LOADING_TIMEOUT);
}
diff --git a/playwright.config.ts b/playwright.config.ts
index 1a91ff7b1f..76a617a9d8 100644
--- a/playwright.config.ts
+++ b/playwright.config.ts
@@ -5,6 +5,7 @@ import { defineConfig, devices } from '@playwright/test';
* If you are running tests in CI, you can use the `CI` environment variable to adjust the configuration.
*/
const isCI = !!process.env.CI;
+const HEADLESS = !!process.env.HEADLESS;
/**
* Read environment variables from file.
@@ -40,7 +41,7 @@ export default defineConfig({
projects: [
{
name: 'chromium',
- use: { ...devices['Desktop Chrome'], headless: isCI },
+ use: { ...devices['Desktop Chrome'], headless: isCI || HEADLESS },
},
// {
@@ -82,11 +83,4 @@ export default defineConfig({
stdout: 'pipe',
stderr: 'pipe',
},
-
- /* Run your local dev server before starting the tests */
- // webServer: {
- // command: 'npm run start',
- // url: 'http://127.0.0.1:3000',
- // reuseExistingServer: !process.env.CI,
- // },
});