Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 const childProcess = require("child_process"); | 20 const {Builder} = require("selenium-webdriver"); |
21 const fs = require("fs"); | 21 const chrome = require("selenium-webdriver/chrome"); |
22 const os = require("os"); | 22 require("chromedriver"); |
23 const path = require("path"); | |
24 | 23 |
25 const remoteInterface = require("chrome-remote-interface"); | 24 const {executeScript} = require("./webdriver"); |
26 | |
27 const {ensureChromium} = require("./chromium_download"); | 25 const {ensureChromium} = require("./chromium_download"); |
28 | 26 |
29 // Chromium 60.0.3082.x | 27 // The Chromium version is a build number, quite obscure. |
30 const CHROMIUM_REVISION = 467222; | 28 // Chromium 63.0.3239.x is 508578 |
29 // Chromium 65.0.3325.0 is 530368 | |
30 // We currently want Chromiun 63, as we still support it and that's the | |
31 // loweset version that supports WebDriver. | |
32 const CHROMIUM_REVISION = 508578; | |
31 | 33 |
32 function rmdir(dirPath) | 34 function runScript(chromiumPath, script, scriptName, scriptArgs) |
33 { | 35 { |
34 for (let file of fs.readdirSync(dirPath)) | 36 const options = new chrome.Options() |
35 { | 37 .headless() |
36 let filePath = path.join(dirPath, file); | 38 .setChromeBinaryPath(chromiumPath); |
37 try | |
38 { | |
39 if (fs.statSync(filePath).isDirectory()) | |
40 rmdir(filePath); | |
41 else | |
42 fs.unlinkSync(filePath); | |
43 } | |
44 catch (error) | |
45 { | |
46 console.error(error); | |
47 } | |
48 } | |
49 | 39 |
50 try | 40 const driver = new Builder() |
51 { | 41 .forBrowser("chrome") |
52 fs.rmdirSync(dirPath); | 42 .setChromeOptions(options) |
53 } | 43 .build(); |
54 catch (error) | |
55 { | |
56 console.error(error); | |
57 } | |
58 } | |
59 | 44 |
60 function startChromium(chromiumPath) | 45 return executeScript(driver, "Chromium (WebDriver)", |
61 { | 46 script, scriptName, scriptArgs); |
62 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU); | |
63 | |
64 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data")); | |
65 let child = null; | |
66 return { | |
67 kill: () => child && child.kill(), | |
68 done: new Promise((resolve, reject) => | |
69 { | |
70 child = childProcess.execFile(chromiumPath, [ | |
71 "--headless", "--single-process", "--disable-gpu", "--no-sandbox", | |
72 "--allow-file-access-from-files", "--remote-debugging-port=9222", | |
73 "--user-data-dir=" + dataDir | |
74 ], error => | |
75 { | |
76 rmdir(dataDir); | |
77 if (error) | |
78 reject(error); | |
79 else | |
80 resolve(); | |
81 }); | |
82 }) | |
83 }; | |
84 } | |
85 | |
86 function throwException(details, url) | |
87 { | |
88 let text = details.exception ? details.exception.description : details.text; | |
89 if (!details.stackTrace) | |
90 { | |
91 // ExceptionDetails uses zero-based line and column numbers. | |
92 text += `\n at ${details.url || url}:` + | |
93 (details.lineNumber + 1) + ":" + | |
94 (details.columnNumber + 1); | |
95 } | |
96 throw text; | |
97 } | |
98 | |
99 function reportMessage(text, level) | |
100 { | |
101 let method = { | |
102 log: "log", | |
103 warning: "warn", | |
104 error: "error", | |
105 debug: "log", | |
106 info: "info" | |
107 }[level] || "log"; | |
108 console[method](text); | |
109 } | |
110 | |
111 function connectRemoteInterface(attempt) | |
112 { | |
113 return remoteInterface().catch(error => | |
114 { | |
115 attempt = attempt || 1; | |
116 if (attempt > 50) | |
117 { | |
118 // Stop trying to connect after 10 seconds | |
119 throw error; | |
120 } | |
121 | |
122 return new Promise((resolve, reject) => | |
123 { | |
124 setTimeout(() => | |
125 { | |
126 connectRemoteInterface(attempt + 1).then(resolve).catch(reject); | |
127 }, 200); | |
128 }); | |
129 }); | |
130 } | |
131 | |
132 function runScript(script, scriptName, scriptArgs) | |
133 { | |
134 return connectRemoteInterface().then(async client => | |
135 { | |
136 try | |
137 { | |
138 let {Runtime, Log, Console} = client; | |
139 | |
140 console.log("\nTests in Chromium (Remote Interface)\n"); | |
kzar
2018/05/04 10:52:43
Perhaps this message should specify if the tests r
hub
2018/05/18 00:04:22
Done.
| |
141 | |
142 await Log.enable(); | |
143 Log.entryAdded(({entry}) => | |
144 { | |
145 reportMessage(entry.text, entry.level); | |
146 }); | |
147 | |
148 await Console.enable(); | |
149 Console.messageAdded(({message}) => | |
150 { | |
151 reportMessage(message.text, message.level); | |
152 }); | |
153 | |
154 await Runtime.enable(); | |
155 let compileResult = await Runtime.compileScript({ | |
156 expression: script, | |
157 sourceURL: scriptName, | |
158 persistScript: true | |
159 }); | |
160 if (compileResult.exceptionDetails) | |
161 throwException(compileResult.exceptionDetails, scriptName); | |
162 | |
163 let runResult = await Runtime.runScript({ | |
164 scriptId: compileResult.scriptId | |
165 }); | |
166 if (runResult.exceptionDetails) | |
167 throwException(runResult.exceptionDetails, scriptName); | |
168 | |
169 let callResult = await Runtime.callFunctionOn({ | |
170 objectId: runResult.result.objectId, | |
171 functionDeclaration: "function(...args) { return this(...args); }", | |
172 arguments: scriptArgs.map(arg => ({value: arg})) | |
173 }); | |
174 if (callResult.exceptionDetails) | |
175 throwException(callResult.exceptionDetails, scriptName); | |
176 | |
177 let promiseResult = await Runtime.awaitPromise({ | |
178 promiseObjectId: callResult.result.objectId | |
179 }); | |
180 if (promiseResult.exceptionDetails) | |
181 throwException(promiseResult.exceptionDetails, scriptName); | |
182 } | |
183 finally | |
184 { | |
185 client.close(); | |
186 } | |
187 }); | |
188 } | 47 } |
189 | 48 |
190 module.exports = function(script, scriptName, ...scriptArgs) | 49 module.exports = function(script, scriptName, ...scriptArgs) |
191 { | 50 { |
192 return ensureChromium(CHROMIUM_REVISION).then(chromiumPath => | 51 return ensureChromium(CHROMIUM_REVISION).then(chromiumPath => |
193 { | 52 { |
194 let child = startChromium(chromiumPath); | 53 return runScript(chromiumPath, script, scriptName, scriptArgs) |
195 return Promise.race([ | 54 .then(result => result) |
196 child.done, | 55 .catch(error => |
197 runScript(script, scriptName, scriptArgs) | 56 { |
198 ]).then(result => | 57 throw error; |
199 { | 58 }); |
200 child.kill(); | |
201 return result; | |
202 }).catch(error => | |
203 { | |
204 child.kill(); | |
205 throw error; | |
206 }); | |
207 }); | 59 }); |
208 }; | 60 }; |
LEFT | RIGHT |