Left: | ||
Right: |
OLD | NEW |
---|---|
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 /* eslint-env mocha */ | |
19 | |
18 "use strict"; | 20 "use strict"; |
19 | 21 |
20 const FIREFOX_VERSION = "57.0"; | 22 const FIREFOX_VERSION = "57.0"; |
21 | 23 |
22 const path = require("path"); | 24 const path = require("path"); |
25 const assert = require("assert"); | |
23 const webdriver = require("selenium-webdriver"); | 26 const webdriver = require("selenium-webdriver"); |
24 const {By, until} = webdriver; | 27 const {By, until} = webdriver; |
25 const firefox = require("selenium-webdriver/firefox"); | 28 const firefox = require("selenium-webdriver/firefox"); |
26 const {Command} = require("selenium-webdriver/lib/command"); | 29 const {Command} = require("selenium-webdriver/lib/command"); |
27 const {ensureFirefox} = require("../adblockpluscore/test/runners/" + | 30 const {ensureFirefox} = require("../adblockpluscore/test/runners/" + |
28 "firefox_download"); | 31 "firefox_download"); |
29 | 32 |
30 function reportElements(test, driver, success) | 33 describe("Firefox", function() |
31 { | 34 { |
32 return driver.findElements( | 35 this.timeout(0); |
33 By.css(`#qunit-tests ${success ? ".pass" : ".fail"} .test-name`) | |
34 ).then(elements => Promise.all(elements.map(elem => | |
35 elem.getAttribute("innerHTML").then(data => test.ok(success, data)) | |
36 ))); | |
37 } | |
38 | 36 |
39 exports.runFirefox = function(test) | 37 let driver; |
40 { | 38 let origin; |
41 // https://stackoverflow.com/a/45045036 | 39 |
42 function installWebExt(driver, extension) | 40 before(() => |
41 ensureFirefox(FIREFOX_VERSION).then(firefoxPath => | |
42 { | |
43 let binary = new firefox.Binary(firefoxPath); | |
44 binary.addArguments("-headless"); | |
45 | |
46 driver = new webdriver.Builder() | |
47 .forBrowser("firefox") | |
48 .setFirefoxOptions(new firefox.Options().setBinary(binary)) | |
49 .build(); | |
50 | |
51 let devenv = "./devenv.gecko"; | |
52 let cmd = new Command("moz-install-web-ext") | |
53 .setParameter("path", path.resolve(devenv)) | |
54 .setParameter("temporary", true); | |
55 | |
56 driver.getExecutor().defineCommand( | |
57 cmd.getName(), "POST", | |
58 "/session/:sessionId/moz/addon/install" | |
59 ); | |
60 driver.schedule(cmd, `installWebExt(${devenv})`); | |
61 | |
62 return driver.wait(() => | |
63 driver.getAllWindowHandles().then(handles => handles[1]) | |
64 ).then(handle => | |
65 driver.switchTo().window(handle) | |
66 ).then(() => | |
67 driver.executeScript("return location.origin;") | |
68 ).then(result => { origin = result; }); | |
69 }) | |
70 ); | |
71 | |
72 after(() => driver.quit()); | |
tlucas
2018/08/26 15:10:51
Nit: IMHO this should go after it(), to reflect th
Sebastian Noack
2018/08/26 19:02:00
Done. Though one could argue the other way around
| |
73 | |
74 function reportElements(success) | |
43 { | 75 { |
44 let cmd = new Command("moz-install-web-ext") | 76 return driver.findElements( |
45 .setParameter("path", path.resolve(extension)) | 77 By.css(`#qunit-tests ${success ? ".pass" : ".fail"} .test-name`) |
46 .setParameter("temporary", true); | 78 ).then(elements => Promise.all(elements.map(elem => |
47 | 79 elem.getAttribute("innerHTML").then(data => assert.ok(success, data)) |
48 driver.getExecutor() | 80 ))); |
49 .defineCommand(cmd.getName(), "POST", | |
50 "/session/:sessionId/moz/addon/install"); | |
51 | |
52 return driver.schedule(cmd, `installWebExt(${extension})`); | |
53 } | 81 } |
54 | 82 |
55 ensureFirefox(FIREFOX_VERSION).then(firefoxPath => | 83 it("qunit", () => |
56 { | 84 driver.navigate().to(origin + "/qunit/index.html").then(() => |
tlucas
2018/08/26 15:10:51
Just FMI, why do you deem this more appropriate th
Sebastian Noack
2018/08/26 15:42:18
I need the extension URL prefix for the tests I'm
tlucas
2018/08/26 21:13:50
Thanks for clarifying.
| |
57 let binary = new firefox.Binary(firefoxPath); | |
58 | |
59 binary.addArguments("-headless"); | |
60 | |
61 let options = new firefox.Options() | |
62 .setBinary(binary); | |
63 | |
64 let driver = new webdriver.Builder() | |
65 .forBrowser("firefox") | |
66 .setFirefoxOptions(options) | |
67 .build(); | |
68 | |
69 installWebExt(driver, "./devenv.gecko"); | |
70 | |
71 driver.wait(() => | |
72 // Wait for the firstrun-page to be loaded | |
73 driver.getAllWindowHandles().then(handles => | |
74 { | |
75 if (handles.length > 1) | |
76 { | |
77 driver.switchTo().window(handles[1]); | |
78 return true; | |
79 } | |
80 return false; | |
81 }) | |
82 ).then(() => | |
83 // Navigate to the qunit index | |
84 driver.executeScript("location.href = \"qunit/index.html\";") | |
85 ).then(() => | |
86 // Wait for qunit-results to be present | 85 // Wait for qunit-results to be present |
87 driver.wait(until.elementLocated(By.id("qunit-testresult"))) | 86 driver.wait(until.elementLocated(By.id("qunit-testresult"))) |
88 ).then(() => | 87 ).then(() => |
89 // Wait for tests to finish | 88 // Wait for tests to finish |
90 driver.wait(() => | 89 driver.wait(() => |
91 driver.findElement(By.id("qunit-testresult")) | 90 driver.findElement(By.id("qunit-testresult")) |
92 .getAttribute("innerHTML").then(data => | 91 .getAttribute("innerHTML").then(data => |
93 data.includes("Tests completed"))) | 92 data.includes("Tests completed"))) |
94 ).then(() => Promise.all([ | 93 ).then(() => Promise.all([ |
95 reportElements(test, driver, true), | 94 reportElements(true), |
96 reportElements(test, driver, false) | 95 reportElements(false) |
97 ])).then( | 96 ])) |
98 () => driver.quit().then(() => test.done()), | 97 ); |
99 err => driver.quit().then(() => { throw err; }) | 98 }); |
100 ); | |
101 }); | |
102 }; | |
OLD | NEW |