Skip to content

Commit 5e9eb72

Browse files
authored
fix(web-integration): reserve the visible nodes of invisible elements (#840)
1 parent 3775c2d commit 5e9eb72

File tree

10 files changed

+486924
-13
lines changed

10 files changed

+486924
-13
lines changed

apps/report/test-data/ai-shop.json

Lines changed: 6945 additions & 1 deletion
Large diffs are not rendered by default.

apps/report/test-data/error.json

Lines changed: 45 additions & 1 deletion
Large diffs are not rendered by default.

apps/report/test-data/online-order.json

Lines changed: 384732 additions & 1 deletion
Large diffs are not rendered by default.

apps/report/test-data/query-only.json

Lines changed: 78 additions & 1 deletion
Large diffs are not rendered by default.

apps/report/test-data/swag-lab.json

Lines changed: 3645 additions & 1 deletion
Large diffs are not rendered by default.

apps/report/test-data/taobao.json

Lines changed: 74284 additions & 1 deletion
Large diffs are not rendered by default.

biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"**/visualizer/scripts/fixture/*",
2727
"**/unpacked-extension/*",
2828
"**/page-data/**",
29-
"**/dump.json"
29+
"**/dump.json",
30+
"**/dump-with-invisible.json"
3031
]
3132
},
3233
"javascript": {

packages/web-integration/src/common/utils.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,33 @@ export function trimContextByViewport(execution: ExecutionDump) {
224224
function filterVisibleTree(
225225
node: ElementTreeNode<BaseElement>,
226226
): ElementTreeNode<BaseElement> | null {
227-
if (!node || !node.node || node.node.isVisible !== true) return null;
227+
if (!node) return null;
228+
229+
// recursively process all children
228230
const filteredChildren = Array.isArray(node.children)
229231
? (node.children
230232
.map(filterVisibleTree)
231233
.filter((child) => child !== null) as ElementTreeNode<BaseElement>[])
232-
: undefined;
233-
return {
234-
...node,
235-
...(filteredChildren ? { children: filteredChildren } : {}),
236-
};
234+
: [];
235+
236+
// if the current node is visible, keep it and the filtered children
237+
if (node.node && node.node.isVisible === true) {
238+
return {
239+
...node,
240+
children: filteredChildren,
241+
};
242+
}
243+
244+
// if the current node is invisible, but has visible children, create an empty node to include these children
245+
if (filteredChildren.length > 0) {
246+
return {
247+
node: null,
248+
children: filteredChildren,
249+
};
250+
}
251+
252+
// if the current node is invisible and has no visible children, return null
253+
return null;
237254
}
238255

239256
return {

packages/web-integration/tests/unit-test/fixtures/dump-with-invisible.json

Lines changed: 17152 additions & 0 deletions
Large diffs are not rendered by default.

packages/web-integration/tests/unit-test/util.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
13
import { getKeyCommands } from '@/common/ui-utils';
24
import {
35
getCurrentExecutionFile,
46
replaceIllegalPathCharsAndSpace,
7+
trimContextByViewport,
58
} from '@/common/utils';
9+
import type { ExecutionDump } from '@midscene/core/.';
610
import { describe, expect, it } from 'vitest';
711

812
describe('TaskCache', () => {
@@ -151,3 +155,17 @@ describe('replaceIllegalPathCharsAndSpace', () => {
151155
expect(result).toBe('cache-id-with-special-chars-and-spaces--');
152156
});
153157
});
158+
159+
describe('trimContextByViewport', () => {
160+
it('should reserve the visible nodes of invisible elements', () => {
161+
const dumpPath = path.join(
162+
__dirname,
163+
'fixtures',
164+
'dump-with-invisible.json',
165+
);
166+
const dump = JSON.parse(fs.readFileSync(dumpPath, 'utf8'));
167+
const result = trimContextByViewport(dump.executions[0]);
168+
expect(result.tasks[0].pageContext?.tree?.node).toBeNull();
169+
expect(result.tasks[0].pageContext?.tree?.children.length).toBe(28);
170+
});
171+
});

0 commit comments

Comments
 (0)