This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
123 lines (112 loc) · 3.06 KB
/
test.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
/**
* Test controller.
* https://adblockplus.org/filters
*/
"use strict";
/**
* Check whether a test passed.
* @function
*/
const checkTest = (debug) => {
const visible = document.querySelectorAll("._nano_test_visible");
if (visible.length === 0) {
if (debug) {
debugger;
}
return false;
}
for (let e of visible) {
let elem = e;
do {
if (elem === document.body) {
break;
}
// To be visible, itself and all parents must be visible
const display = getComputedStyle(elem).display;
if (display !== "block" && display !== "inline" && display !== "inline-block") {
if (debug) {
debugger;
}
return false;
}
} while (elem = elem.parentElement);
}
const hide = document.querySelectorAll("._nano_test_hide");
if (hide.length === 0) {
if (debug) {
debugger;
}
return false;
}
for (let e of hide) {
let isHidden = false;
let elem = e;
do {
if (elem === document.body) {
break;
}
// To be hidden, itself or one of parent must be hidden
const display = getComputedStyle(elem).display;
if (display === "none") {
isHidden = true;
break;
}
} while (elem = elem.parentElement);
if (!isHidden) {
if (debug) {
debugger;
}
return false;
}
}
return true;
};
/**
* Available tests.
* @const {Object.<Function>}
*/
const tests = {
extended_basic: async (override = "extended-basic") => {
let page = await browser.browser.newPage();
await page.goto(localhostBase + "tests/" + override);
await delay(1000);
try {
await page.waitForFunction(checkTest, {
timeout: 1500,
});
await page.close();
return true;
} catch (err) {
if (/timeout/i.test(err.message)) {
await page.evaluate(checkTest, true);
return false;
} else {
throw err;
}
}
},
extended_extended: async () => {
return await tests.extended_basic("extended-extended");
},
extended_other: async () => {
return await tests.extended_basic("extended-other");
},
// Some network filter tests use DOM flag elements to enable code reuse
network_basic: async () => {
return await tests.extended_basic("network-basic");
},
};
/**
* Establish connection with dashboard.
* @async @function
*/
module.exports = async () => {
await dashboard.exposeFunction("nano_test_engine", async (test_name) => {
if (tests.hasOwnProperty(test_name)) {
return await tests[test_name]();
} else {
console.error("[Tests] Not Found ::", test_name);
return false;
}
});
};