-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.js
123 lines (108 loc) · 3.2 KB
/
commands.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const Daywalker = require('./dawalker');
function ensureAttached(w, node) {
if (node == null || w.document.body.contains(node)) {
return;
}
const attached = node.cloneNode(true);
document.body.appendChild(attached);
return attached;
}
Cypress.Commands.add('dwGet', {prevSubject: false}, (selector, options = {}) => {
Cypress.log({
displayName: 'DAYWALKER GET',
message: ` => ${selector}`,
});
return cy.window({log: false}).then((w) => {
const walker = new Daywalker(w);
const maxRetries = options.maxRetries || 3;
const retryDelayBase = options.retryDelayBase || 500;
return new Cypress.Promise((resolve) => {
let tries = 0;
let result;
function tryQuery() {
result = options.all
? walker.querySelectorAll(selector)
: walker.querySelector(selector, (options.nth) || 1);
if (result == null && tries <= maxRetries) {
setTimeout(() => {
result = tryQuery();
}, retryDelayBase * tries);
tries += 1;
return;
}
resolve(result);
}
tryQuery();
})
.then((result) => {
if (result == null) {
return cy.get(selector, options);
}
return result;
});
});
});
Cypress.Commands.add('dwAttach', {prevSubject: true}, (subject) => {
Cypress.log({
displayName: 'DAYWALKER ATTACH (CLONE)',
message: ` ${subject[0].tagName}`,
});
return cy.window({log: false}).then((w) => new Cypress.Promise((resolve, reject) => {
const attachedNode = ensureAttached(w, subject[0]);
if (attachedNode) {
attachedNode.__proto__.dwDetach = attachedNode.remove;
}
resolve(attachedNode || subject[0]);
}));
});
Cypress.Commands.add('dwDetach', {prevSubject: true}, (subject) => {
Cypress.log({
displayName: 'DAYWALKER DETACH',
message: ` ${subject[0].tagName}`,
});
return cy.window({log: false}).then((w) => new Cypress.Promise((resolve, reject) => {
const node = subject[0];
if (node.detach) {
debugger;
node.dwDetach();
}
resolve(subject[0]);
}));
});
Cypress.Commands.add('dwSetProp', {prevSubject: true}, (subject, value, prop = 'value') => {
const element = subject[0];
Cypress.log({
displayName: 'DAYWALKER SET PROP',
message: `${element.tagName} => ${value}`,
});
if (element.set != null) {
element.set(prop, value);
return subject;
}
element[prop] = value;
return subject;
});
Cypress.Commands.add('dwCall', {prevSubject: true}, (subject, fnName, args = []) => {
const element = subject[0];
Cypress.log({
displayName: 'DAYWALKER CALL',
message: `${element.tagName} => ${fnName}`,
});
if (element) {
element[fnName](...args);
};
return subject;
});
Cypress.Commands.add('dwDispatch', {prevSubject: true}, (subject, event, options) => {
const element = subject[0];
const eventStr = typeof event === 'string' ? event : event.toString();
const e = typeof event === 'string' ? new Event(event) : event;
Cypress.log({
displayName: 'DAYWALKER DISPATCH',
message: `${element.tagName} => ${eventStr}`,
});
if (element) {
element.dispatchEvent(e, options);
};
return subject;
});