-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcypress-daywalker.js
58 lines (50 loc) · 1.34 KB
/
cypress-daywalker.js
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
((global) => {
if (!global.Cypress) {
return;
}
class DaywalkerStore {
constructor() {
this.data = {
tags: {},
classes: {},
ids: {},
};
}
addInstance(tagName, instance) {
this.data.tags[tagName.toLowerCase()] = this.data.tags[tagName] || [];
this._push(this.data.tags[tagName], instance);
if (instance.id) {
this.data.ids[instance.id] = this.data.ids[instance.id] || [];
this._push(this.data.ids[instance.id], instance);
}
const classes = Array.from(instance.classList);
classes.forEach(function(c) {
this.data.classes[c] = this.data.classes[c] || [];
this._push(this.data.classes[c], instance);
}, this);
}
_push(arr, instance) {
if (arr.find((node) => node == instance)) {
return;
}
arr.push(instance);
}
}
const daywalker = {
store: new DaywalkerStore(),
};
const oldDefine = customElements.define;
customElements.define = function define(...args) {
if (args && args.length > 0) {
const name = args[0];
args[1] = class b extends args[1] {
constructor(...args) {
super(...args);
daywalker.store.addInstance(name, this);
}
};
}
return oldDefine.apply(this, args);
};
global.Daywalker = daywalker;
})(window);