OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 const TEST_PAGES_URL = "https://testpages.adblockplus.org/"; |
| 21 |
| 22 const assert = require("assert"); |
| 23 const Jimp = require("jimp"); |
| 24 const {By} = require("selenium-webdriver"); |
| 25 |
| 26 function imageFromBase64(s) |
| 27 { |
| 28 return Jimp.read(Buffer.from(s, "base64")); |
| 29 } |
| 30 |
| 31 function takeScreenshot(element) |
| 32 { |
| 33 return element.takeScreenshot().then( |
| 34 imageFromBase64, |
| 35 |
| 36 // Chrome doesn't support taking screenshots of individual elements. So as |
| 37 // a workaround, we scroll to the position of the element, take a screenshot |
| 38 // of the viewport and crop it to the size and position of our element. |
| 39 // This is not guaranteed to work on other browsers (mostly because |
| 40 // the behavior of Driver.takeScreenshot() may vary across browsers). |
| 41 () => element.getLocation().then(loc => |
| 42 element.getDriver().executeScript(` |
| 43 window.scrollTo(${loc.x}, ${loc.y}); |
| 44 return [window.scrollX, window.scrollY]; |
| 45 `).then(result => |
| 46 { |
| 47 let x = loc.x - result[0]; |
| 48 let y = loc.y - result[1]; |
| 49 |
| 50 return Promise.all([ |
| 51 element.getDriver().takeScreenshot().then(imageFromBase64), |
| 52 element.getSize() |
| 53 ]).then(([img, size]) => |
| 54 img.crop(x, y, size.width, size.height) |
| 55 ); |
| 56 }) |
| 57 ) |
| 58 ).then(img => img.bitmap); |
| 59 } |
| 60 |
| 61 function getSections(driver) |
| 62 { |
| 63 return driver.findElements(By.css("section")).then(elements => |
| 64 Promise.all(elements.map(e => |
| 65 Promise.all([ |
| 66 e.findElement(By.css("h2")).catch(() => null), |
| 67 e.findElement(By.className("testcase-container")).catch(() => null), |
| 68 e.findElements(By.css("pre")) |
| 69 ]) |
| 70 )) |
| 71 ).then(sections => sections.filter( |
| 72 ([title, demo, filters]) => title && demo && filters.length > 0 |
| 73 )); |
| 74 } |
| 75 |
| 76 it("test pages", function() |
| 77 { |
| 78 return this.driver.navigate().to(TEST_PAGES_URL).then(() => |
| 79 this.driver.findElements(By.css(".site-pagelist a")) |
| 80 ).then(elements => |
| 81 Promise.all(elements.map(elem => elem.getAttribute("href"))) |
| 82 ).then(urls => |
| 83 { |
| 84 let p1 = Promise.resolve(); |
| 85 for (let url of urls) |
| 86 p1 = p1.then(() => |
| 87 this.driver.navigate().to(url) |
| 88 ).then(() => |
| 89 Promise.all([ |
| 90 getSections(this.driver), |
| 91 this.driver.findElement(By.css("h2")).getAttribute("textContent"), |
| 92 this.driver.executeScript("document.body.classList.add('expected');") |
| 93 ]) |
| 94 ).then(([sections, pageTitle]) => |
| 95 Promise.all(sections.map(([title, demo, filters]) => |
| 96 Promise.all([ |
| 97 title.getAttribute("textContent").then(testTitle => |
| 98 `${pageTitle.trim()} - ${testTitle.trim()}` |
| 99 ), |
| 100 takeScreenshot(demo), |
| 101 Promise.all(filters.map(elem => elem.getAttribute("textContent"))) |
| 102 ]) |
| 103 )) |
| 104 ).then(testCases => |
| 105 { |
| 106 let p2 = Promise.resolve(); |
| 107 for (let i = 0; i < testCases.length; i++) |
| 108 { |
| 109 let [title, expectedScreenshot, filters] = testCases[i]; |
| 110 let platform = this.test.parent.title; |
| 111 |
| 112 if (// https://issues.adblockplus.org/ticket/6917 |
| 113 title == "$subdocument - Test case" && platform == "gecko" || |
| 114 // Chromium doesn't support Flash |
| 115 /^\$object(-subrequest)? /.test(title) && platform == "chrome") |
| 116 continue; |
| 117 |
| 118 p2 = p2.then(() => |
| 119 this.driver.navigate().to(this.origin + "/options.html") |
| 120 ).then(() => |
| 121 this.driver.executeAsyncScript(` |
| 122 let filters = arguments[0]; |
| 123 let callback = arguments[arguments.length - 1]; |
| 124 browser.runtime.sendMessage({type: "subscriptions.get", |
| 125 downloadable: true, |
| 126 special: true}).then(subs => |
| 127 { |
| 128 for (let subscription of subs) |
| 129 browser.runtime.sendMessage({type: "subscriptions.remove", |
| 130 url: subscription.url}); |
| 131 return browser.runtime.sendMessage({type: "filters.importRaw", |
| 132 text: filters}); |
| 133 }).then(() => callback(), callback); |
| 134 `, filters.join("\n")) |
| 135 ).then(error => |
| 136 { |
| 137 if (error) |
| 138 throw error; |
| 139 return this.driver.navigate().to(url); |
| 140 }).then(() => |
| 141 getSections(this.driver) |
| 142 ).then(sections => |
| 143 takeScreenshot(sections[i][1]) |
| 144 ).then(screenshot => |
| 145 assert.ok( |
| 146 screenshot.width == expectedScreenshot.width && |
| 147 screenshot.height == expectedScreenshot.height && |
| 148 screenshot.data.compare(expectedScreenshot.data) == 0, |
| 149 title |
| 150 ) |
| 151 ); |
| 152 } |
| 153 return p2; |
| 154 }); |
| 155 return p1; |
| 156 }); |
| 157 }); |
OLD | NEW |