Skip to content

Commit 2473d84

Browse files
committed
test: Put test-functions.js helpers on window object
webdriver's and BiDi's functions for preloading a script only supports declaring and calling a function. This can't define globals. So do what sizzle does and put the helper functions on the `window` global object. Drop the long-obsolete jQuery comment.
1 parent eff7d97 commit 2473d84

File tree

1 file changed

+94
-97
lines changed

1 file changed

+94
-97
lines changed

test/common/test-functions.js

Lines changed: 94 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
/*
44
* These are routines used by our testing code.
5-
*
6-
* jQuery is not necessarily present. Don't rely on it
7-
* for routine operations.
85
*/
96

10-
function ph_select(sel) {
7+
window.ph_select = function(sel) {
118
if (!window.Sizzle) {
129
return Array.from(document.querySelectorAll(sel));
1310
}
@@ -20,95 +17,95 @@ function ph_select(sel) {
2017
} else {
2118
return Array.from(document.querySelectorAll(sel));
2219
}
23-
}
20+
};
2421

25-
function ph_only(els, sel) {
22+
window.ph_only = function(els, sel) {
2623
if (els.length === 0)
2724
throw new Error(sel + " not found");
2825
if (els.length > 1)
2926
throw new Error(sel + " is ambiguous");
3027
return els[0];
31-
}
28+
};
3229

33-
function ph_find (sel) {
34-
const els = ph_select(sel);
35-
return ph_only(els, sel);
36-
}
30+
window.ph_find = function(sel) {
31+
const els = window.ph_select(sel);
32+
return window.ph_only(els, sel);
33+
};
3734

38-
function ph_count(sel) {
39-
const els = ph_select(sel);
35+
window.ph_count = function(sel) {
36+
const els = window.ph_select(sel);
4037
return els.length;
41-
}
38+
};
4239

43-
function ph_count_check(sel, expected_num) {
44-
return (ph_count(sel) == expected_num);
45-
}
40+
window.ph_count_check = function(sel, expected_num) {
41+
return (window.ph_count(sel) == expected_num);
42+
};
4643

47-
function ph_val (sel) {
48-
const el = ph_find(sel);
44+
window.ph_val = function(sel) {
45+
const el = window.ph_find(sel);
4946
if (el.value === undefined)
5047
throw new Error(sel + " does not have a value");
5148
return el.value;
52-
}
49+
};
5350

54-
function ph_set_val (sel, val) {
55-
const el = ph_find(sel);
51+
window.ph_set_val = function(sel, val) {
52+
const el = window.ph_find(sel);
5653
if (el.value === undefined)
5754
throw new Error(sel + " does not have a value");
5855
el.value = val;
5956
const ev = new Event("change", { bubbles: true, cancelable: false });
6057
el.dispatchEvent(ev);
61-
}
58+
};
6259

63-
function ph_has_val (sel, val) {
64-
return ph_val(sel) == val;
65-
}
60+
window.ph_has_val = function(sel, val) {
61+
return window.ph_val(sel) == val;
62+
};
6663

67-
function ph_collected_text_is (sel, val) {
68-
const els = ph_select(sel);
64+
window.ph_collected_text_is = function(sel, val) {
65+
const els = window.ph_select(sel);
6966
const rest = els.map(el => {
7067
if (el.textContent === undefined)
7168
throw new Error(sel + " can not have text");
7269
return el.textContent.replaceAll("\xa0", " ");
7370
}).join("");
7471
return rest === val;
75-
}
72+
};
7673

77-
function ph_text (sel) {
78-
const el = ph_find(sel);
74+
window.ph_text = function(sel) {
75+
const el = window.ph_find(sel);
7976
if (el.textContent === undefined)
8077
throw new Error(sel + " can not have text");
8178
// 0xa0 is a non-breakable space, which is a rendering detail of Chromium
8279
// and awkward to handle in tests; turn it into normal spaces
8380
return el.textContent.replaceAll("\xa0", " ");
84-
}
81+
};
8582

86-
function ph_attr (sel, attr) {
87-
return ph_find(sel).getAttribute(attr);
88-
}
83+
window.ph_attr = function(sel, attr) {
84+
return window.ph_find(sel).getAttribute(attr);
85+
};
8986

90-
function ph_set_attr (sel, attr, val) {
91-
const el = ph_find(sel);
87+
window.ph_set_attr = function(sel, attr, val) {
88+
const el = window.ph_find(sel);
9289
if (val === null || val === undefined)
9390
el.removeAttribute(attr);
9491
else
9592
el.setAttribute(attr, val);
9693

9794
const ev = new Event("change", { bubbles: true, cancelable: false });
9895
el.dispatchEvent(ev);
99-
}
96+
};
10097

101-
function ph_has_attr (sel, attr, val) {
102-
return ph_attr(sel, attr) == val;
103-
}
98+
window.ph_has_attr = function(sel, attr, val) {
99+
return window.ph_attr(sel, attr) == val;
100+
};
104101

105-
function ph_attr_contains (sel, attr, val) {
106-
const a = ph_attr(sel, attr);
102+
window.ph_attr_contains = function(sel, attr, val) {
103+
const a = window.ph_attr(sel, attr);
107104
return a && a.indexOf(val) > -1;
108-
}
105+
};
109106

110-
function ph_mouse(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) {
111-
const el = ph_find(sel);
107+
window.ph_mouse = function(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) {
108+
const el = window.ph_find(sel);
112109

113110
/* The element has to be visible, and not collapsed */
114111
if (el.offsetWidth <= 0 && el.offsetHeight <= 0 && el.tagName != 'svg')
@@ -160,48 +157,48 @@ function ph_mouse(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, metaKey) {
160157
/* It really had to work */
161158
if (!processed)
162159
throw new Error(sel + " is disabled or somehow doesn't process events");
163-
}
160+
};
164161

165-
function ph_get_checked (sel) {
166-
const el = ph_find(sel);
162+
window.ph_get_checked = function(sel) {
163+
const el = window.ph_find(sel);
167164
if (el.checked === undefined)
168165
throw new Error(sel + " is not checkable");
169166

170167
return el.checked;
171-
}
168+
};
172169

173-
function ph_set_checked (sel, val) {
174-
const el = ph_find(sel);
170+
window.ph_set_checked = function(sel, val) {
171+
const el = window.ph_find(sel);
175172
if (el.checked === undefined)
176173
throw new Error(sel + " is not checkable");
177174

178175
if (el.checked != val)
179-
ph_mouse(sel, "click", 0, 0, 0);
180-
}
176+
window.ph_mouse(sel, "click", 0, 0, 0);
177+
};
181178

182-
function ph_is_visible (sel) {
183-
const el = ph_find(sel);
179+
window.ph_is_visible = function(sel) {
180+
const el = window.ph_find(sel);
184181
return el.tagName == "svg" || ((el.offsetWidth > 0 || el.offsetHeight > 0) && !(el.style.visibility == "hidden" || el.style.display == "none"));
185-
}
182+
};
186183

187-
function ph_is_present(sel) {
188-
const els = ph_select(sel);
184+
window.ph_is_present = function(sel) {
185+
const els = window.ph_select(sel);
189186
return els.length > 0;
190-
}
187+
};
191188

192-
function ph_in_text (sel, text) {
193-
return ph_text(sel).indexOf(text) != -1;
194-
}
189+
window.ph_in_text = function(sel, text) {
190+
return window.ph_text(sel).indexOf(text) != -1;
191+
};
195192

196-
function ph_text_is (sel, text) {
197-
return ph_text(sel) == text;
198-
}
193+
window.ph_text_is = function(sel, text) {
194+
return window.ph_text(sel) == text;
195+
};
199196

200-
function ph_text_matches (sel, pattern) {
201-
return ph_text(sel).match(pattern);
202-
}
197+
window.ph_text_matches = function(sel, pattern) {
198+
return window.ph_text(sel).match(pattern);
199+
};
203200

204-
function ph_go(href) {
201+
window.ph_go = function(href) {
205202
if (href.indexOf("#") === 0) {
206203
window.location.hash = href;
207204
} else {
@@ -213,25 +210,25 @@ function ph_go(href) {
213210
};
214211
window.parent.postMessage("\n" + JSON.stringify(control), "*");
215212
}
216-
}
213+
};
217214

218-
function ph_focus(sel) {
219-
ph_find(sel).focus();
220-
}
215+
window.ph_focus = function(sel) {
216+
window.ph_find(sel).focus();
217+
};
221218

222-
function ph_scrollIntoViewIfNeeded(sel) {
223-
ph_find(sel).scrollIntoViewIfNeeded();
224-
}
219+
window.ph_scrollIntoViewIfNeeded = function(sel) {
220+
window.ph_find(sel).scrollIntoViewIfNeeded();
221+
};
225222

226-
function ph_blur(sel) {
227-
ph_find(sel).blur();
228-
}
223+
window.ph_blur = function(sel) {
224+
window.ph_find(sel).blur();
225+
};
229226

230-
function ph_blur_active() {
227+
window.ph_blur_active = function() {
231228
const elt = window.document.activeElement;
232229
if (elt)
233230
elt.blur();
234-
}
231+
};
235232

236233
class PhWaitCondTimeout extends Error {
237234
constructor(description) {
@@ -244,7 +241,7 @@ class PhWaitCondTimeout extends Error {
244241
}
245242
}
246243

247-
function ph_wait_cond(cond, timeout, error_description) {
244+
window.ph_wait_cond = function(cond, timeout, error_description) {
248245
return new Promise((resolve, reject) => {
249246
// poll every 100 ms for now; FIXME: poll less often and re-check on mutations using
250247
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
@@ -269,7 +266,7 @@ function ph_wait_cond(cond, timeout, error_description) {
269266
}
270267
step();
271268
});
272-
}
269+
};
273270

274271
function currentFrameAbsolutePosition() {
275272
let currentWindow = window;
@@ -307,27 +304,27 @@ function flatten(array_of_arrays) {
307304
return [];
308305
}
309306

310-
function ph_selector_clips(sels) {
307+
window.ph_selector_clips = function(sels) {
311308
const f = currentFrameAbsolutePosition();
312-
const elts = flatten(sels.map(ph_select));
309+
const elts = flatten(sels.map(window.ph_select));
313310
return elts.map(e => {
314311
const r = e.getBoundingClientRect();
315312
return { x: r.x + f.x, y: r.y + f.y, width: r.width, height: r.height, scale: 1 };
316313
});
317-
}
314+
};
318315

319-
function ph_element_clip(sel) {
320-
ph_find(sel); // just to make sure it is not ambiguous
321-
return ph_selector_clips([sel])[0];
322-
}
316+
window.ph_element_clip = function(sel) {
317+
window.ph_find(sel); // just to make sure it is not ambiguous
318+
return window.ph_selector_clips([sel])[0];
319+
};
323320

324-
function ph_count_animations(sel) {
325-
return ph_find(sel).getAnimations({ subtree: true }).length;
326-
}
321+
window.ph_count_animations = function(sel) {
322+
return window.ph_find(sel).getAnimations({ subtree: true }).length;
323+
};
327324

328-
function ph_set_texts(new_texts) {
325+
window.ph_set_texts = function(new_texts) {
329326
for (const sel in new_texts) {
330-
const elts = ph_select(sel);
327+
const elts = window.ph_select(sel);
331328
if (elts.length == 0)
332329
throw new Error(sel + " not found");
333330
for (let elt of elts) {
@@ -357,4 +354,4 @@ function ph_set_texts(new_texts) {
357354
}
358355
}
359356
}
360-
}
357+
};

0 commit comments

Comments
 (0)