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 | |
42 function installWebExt(driver, extension) | |
43 { | |
44 let cmd = new Command("moz-install-web-ext") | |
45 .setParameter("path", path.resolve(extension)) | |
46 .setParameter("temporary", true); | |
47 | 39 |
48 driver.getExecutor() | 40 before(() => |
49 .defineCommand(cmd.getName(), "POST", | 41 ensureFirefox(FIREFOX_VERSION).then(firefoxPath => |
50 "/session/:sessionId/moz/addon/install"); | 42 { |
| 43 let binary = new firefox.Binary(firefoxPath); |
| 44 binary.addArguments("-headless"); |
51 | 45 |
52 return driver.schedule(cmd, `installWebExt(${extension})`); | 46 driver = new webdriver.Builder() |
53 } | 47 .forBrowser("firefox") |
| 48 .setFirefoxOptions(new firefox.Options().setBinary(binary)) |
| 49 .build(); |
54 | 50 |
55 ensureFirefox(FIREFOX_VERSION).then(firefoxPath => | 51 let devenv = "./devenv.gecko"; |
56 { | 52 let cmd = new Command("moz-install-web-ext") |
57 let binary = new firefox.Binary(firefoxPath); | 53 .setParameter("path", path.resolve(devenv)) |
| 54 .setParameter("temporary", true); |
58 | 55 |
59 binary.addArguments("-headless"); | 56 driver.getExecutor().defineCommand( |
| 57 cmd.getName(), "POST", |
| 58 "/session/:sessionId/moz/addon/install" |
| 59 ); |
| 60 driver.schedule(cmd, `installWebExt(${devenv})`); |
60 | 61 |
61 let options = new firefox.Options() | 62 return driver.wait(() => |
62 .setBinary(binary); | 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 ); |
63 | 71 |
64 let driver = new webdriver.Builder() | 72 it("qunit", () => |
65 .forBrowser("firefox") | 73 driver.navigate().to(origin + "/qunit/index.html").then(() => |
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 | 74 // Wait for qunit-results to be present |
87 driver.wait(until.elementLocated(By.id("qunit-testresult"))) | 75 driver.wait(until.elementLocated(By.id("qunit-testresult"))) |
88 ).then(() => | 76 ).then(() => |
89 // Wait for tests to finish | 77 // Wait for tests to finish |
90 driver.wait(() => | 78 driver.wait(() => |
91 driver.findElement(By.id("qunit-testresult")) | 79 driver.findElement(By.id("qunit-testresult")) |
92 .getAttribute("innerHTML").then(data => | 80 .getAttribute("innerHTML").then(data => |
93 data.includes("Tests completed"))) | 81 data.includes("Tests completed"))) |
94 ).then(() => Promise.all([ | 82 ).then(() => Promise.all([[true, ".pass"], [false, ".fail"]].map( |
95 reportElements(test, driver, true), | 83 ([success, sel]) => driver.findElements( |
96 reportElements(test, driver, false) | 84 By.css(`#qunit-tests ${sel} .test-name`) |
97 ])).then( | 85 ).then(elements => Promise.all(elements.map(elem => |
98 () => driver.quit().then(() => test.done()), | 86 elem.getAttribute("textContent").then(data => assert.ok(success, data)) |
99 err => driver.quit().then(() => { throw err; }) | 87 ))) |
100 ); | 88 ))) |
101 }); | 89 ); |
102 }; | 90 |
| 91 after(() => driver.quit()); |
| 92 }); |
OLD | NEW |