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://codereview.adblockplus.org/29871606/ |
| 113 title == "Element Hiding Emulation / Extended Selectors - " + |
| 114 "Case insensative extended selectors" || |
| 115 // https://codereview.adblockplus.org/29871622/ |
| 116 title == "Element Hiding Emulation / Extended Selectors - " + |
| 117 "Regular expression in :-abp-contains()" || |
| 118 // https://issues.adblockplus.org/ticket/6917 |
| 119 title == "$subdocument - Test case" && platform == "gecko" || |
| 120 // https://codereview.adblockplus.org/29871625/ |
| 121 title == "$xmlhttprequest Exception - Test case" || |
| 122 // Chromium doesn't support Flash |
| 123 /^\$object(-subrequest)?\b/.test(title) && platform == "chrome") |
| 124 continue; |
| 125 |
| 126 p2 = p2.then(() => |
| 127 this.driver.navigate().to(this.origin + "/options.html") |
| 128 ).then(() => |
| 129 this.driver.executeAsyncScript(` |
| 130 let filters = arguments[0]; |
| 131 let callback = arguments[arguments.length - 1]; |
| 132 browser.runtime.sendMessage({type: "subscriptions.get", |
| 133 downloadable: true, |
| 134 special: true}).then(subs => |
| 135 { |
| 136 for (let subscription of subs) |
| 137 browser.runtime.sendMessage({type: "subscriptions.remove", |
| 138 url: subscription.url}); |
| 139 return browser.runtime.sendMessage({type: "filters.importRaw", |
| 140 text: filters}); |
| 141 }).then(() => callback(), callback); |
| 142 `, filters.join("\n")) |
| 143 ).then(error => |
| 144 { |
| 145 if (error) |
| 146 throw error; |
| 147 return this.driver.navigate().to(url); |
| 148 }).then(() => |
| 149 getSections(this.driver) |
| 150 ).then(sections => |
| 151 takeScreenshot(sections[i][1]) |
| 152 ).then(screenshot => |
| 153 assert.ok( |
| 154 screenshot.width == expectedScreenshot.width && |
| 155 screenshot.height == expectedScreenshot.height && |
| 156 screenshot.data.compare(expectedScreenshot.data) == 0, |
| 157 title |
| 158 ) |
| 159 ); |
| 160 } |
| 161 return p2; |
| 162 }); |
| 163 return p1; |
| 164 }); |
| 165 }); |
OLD | NEW |