Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / tools / wptrunner / wptrunner / testdriver-extra.js
1 "use strict";
2
3 (function(){
4     let pending_resolve = null;
5     let pending_reject = null;
6     window.addEventListener("message", function(event) {
7         const data = event.data;
8
9         if (typeof data !== "object" && data !== null) {
10             return;
11         }
12
13         if (data.type !== "testdriver-complete") {
14             return;
15         }
16
17         if (data.status === "success") {
18             pending_resolve();
19         } else {
20             pending_reject();
21         }
22     });
23
24     const get_selector = function(element) {
25         let selector;
26
27         if (element.id && document.getElementById(element.id) === element) {
28             const id = element.id;
29
30             selector = "#";
31             // escape everything, because it's easy to implement
32             for (let i = 0, len = id.length; i < len; i++) {
33                 selector += '\\' + id.charCodeAt(i).toString(16) + ' ';
34             }
35         } else {
36             // push and then reverse to avoid O(n) unshift in the loop
37             let segments = [];
38             for (let node = element;
39                  node.parentElement;
40                  node = node.parentElement) {
41                 let segment = "*|" + node.localName;
42                 let nth = Array.prototype.indexOf.call(node.parentElement.children, node) + 1;
43                 segments.push(segment + ":nth-child(" + nth + ")");
44             }
45             segments.push(":root");
46             segments.reverse();
47
48             selector = segments.join(" > ");
49         }
50
51         return selector;
52     };
53
54     window.test_driver_internal.click = function(element) {
55         const selector = get_selector(element);
56         const pending_promise = new Promise(function(resolve, reject) {
57             pending_resolve = resolve;
58             pending_reject = reject;
59         });
60         window.opener.postMessage({"type": "action", "action": "click", "selector": selector}, "*");
61         return pending_promise;
62     };
63 })();