1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-env node */
"use strict";
/* global module */
async function test(context, commands) {
await context.selenium.driver.setContext("chrome");
let elementData = await context.selenium.driver.executeAsyncScript(
function() {
let callback = arguments[arguments.length - 1];
(async function() {
let lightDOM = document.querySelectorAll("*");
let elementsWithoutIDs = {};
let idElements = [];
let lightDOMDetails = { idElements, elementsWithoutIDs };
lightDOM.forEach(n => {
if (n.id) {
idElements.push(n.id);
} else {
if (!elementsWithoutIDs.hasOwnProperty(n.localName)) {
elementsWithoutIDs[n.localName] = 0;
}
elementsWithoutIDs[n.localName]++;
}
});
let lightDOMCount = lightDOM.length;
// Recursively explore shadow DOM:
function getShadowElements(root) {
let allElems = Array.from(root.querySelectorAll("*"));
let shadowRoots = allElems.map(n => n.openOrClosedShadowRoot);
for (let innerRoot of shadowRoots) {
if (innerRoot) {
allElems.push(getShadowElements(innerRoot));
}
}
return allElems;
}
let totalDOMCount = Array.from(lightDOM, node => {
if (node.openOrClosedShadowRoot) {
return [node].concat(
getShadowElements(node.openOrClosedShadowRoot)
);
}
return node;
}).flat().length;
let panelMenuCount = document.querySelectorAll(
"panel,menupopup,popup,popupnotification"
).length;
return {
panelMenuCount,
lightDOMCount,
totalDOMCount,
lightDOMDetails,
};
})().then(callback);
}
);
let { lightDOMDetails } = elementData;
delete elementData.lightDOMDetails;
lightDOMDetails.idElements.sort();
for (let id of lightDOMDetails.idElements) {
console.log(id);
}
console.log("Elements without ids:");
for (let [localName, count] of Object.entries(
lightDOMDetails.elementsWithoutIDs
)) {
console.log(count.toString().padStart(4) + " " + localName);
}
console.log(elementData);
await context.selenium.driver.setContext("content");
await commands.measure.start("data:text/html,BrowserDOM");
commands.measure.addObject(elementData);
}
module.exports = {
test,
owner: "Browser Front-end team",
name: "Dom-size",
description: "Measures the size of the DOM",
supportedBrowsers: ["Desktop"],
supportedPlatforms: ["Windows", "Linux", "macOS"],
};
|