Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/wrappers/pages.js

Issue 29885565: Noissue - Automatically test subscription links (Closed)
Patch Set: Created Sept. 19, 2018, 2:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 TEST_PAGES_URL = "https://testpages.adblockplus.org/"; 20 const TEST_PAGES_URL = "https://testpages.adblockplus.org/";
21 21
22 const assert = require("assert"); 22 const assert = require("assert");
23 const Jimp = require("jimp"); 23 const Jimp = require("jimp");
24 const {By} = require("selenium-webdriver"); 24 const {By} = require("selenium-webdriver");
25 25
26 // Once we require Node.js >= 10 this should be replaced with
27 // the built-in finally() method of the Promise object.
28 function promiseFinally(p, callback)
29 {
30 return p.then(callback, callback);
31 }
32
33 function closeWindow(driver, goTo, returnTo, callback)
34 {
35 return promiseFinally(
36 driver.switchTo().window(goTo).then(() =>
37 promiseFinally(
38 new Promise(resolve => resolve(callback && callback())),
39 () => driver.close()
40 )
41 ),
42 () => driver.switchTo().window(returnTo)
43 );
44 }
45
46 function testSubscribeLink(driver)
47 {
48 return driver.findElement(By.id("subscribe-button")).click().then(() =>
49 driver.wait(() =>
50 driver.getAllWindowHandles().then(handles =>
51 handles.length > 2 ? handles : null
52 ), 1000
53 )
54 ).then(handles =>
55 closeWindow(driver, handles[2], handles[1], () =>
56 driver.switchTo().frame(0).then(() =>
57 driver.findElement(By.id("dialog-content-predefined"))
58 ).then(dialog =>
59 Promise.all([
60 dialog.isDisplayed(),
61 dialog.findElement(By.css("h3")).getText()
62 ]).then(([displayed, title]) =>
63 {
64 assert.ok(displayed, "subscribe link: dialog shown");
65 assert.equal(title, "ABP Testcase Subscription",
66 "subscribe link: title shown in dialog");
67
68 return dialog.findElement(By.css("button")).click();
69 })
70 ).then(() =>
71 driver.executeAsyncScript(`
72 let callback = arguments[arguments.length - 1];
73 browser.runtime.sendMessage({type: "subscriptions.get",
74 ignoreDisabled: true,
75 downloadable: true}).then(subs =>
76 subs.some(s =>
77 s.url.endsWith("${TEST_PAGES_URL.replace(/^[^:]+/, "")}" +
78 "abp-testcase-subscription.txt")
79 )
80 ).then(
81 res => callback([res, null]),
82 err => callback([null, err])
83 );
84 `)
85 ).then(([added, err]) =>
86 {
87 if (err)
88 throw err;
89 assert.ok(added, "subscribe link: subscription added");
90 })
91 )
92 );
93 }
94
26 function imageFromBase64(s) 95 function imageFromBase64(s)
27 { 96 {
28 return Jimp.read(Buffer.from(s, "base64")); 97 return Jimp.read(Buffer.from(s, "base64"));
29 } 98 }
30 99
31 function takeScreenshot(element) 100 function takeScreenshot(element)
32 { 101 {
33 return element.takeScreenshot().then( 102 return element.takeScreenshot().then(
34 imageFromBase64, 103 imageFromBase64,
35 104
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 143 }
75 144
76 it("test pages", function() 145 it("test pages", function()
77 { 146 {
78 return this.driver.navigate().to(TEST_PAGES_URL).then(() => 147 return this.driver.navigate().to(TEST_PAGES_URL).then(() =>
79 this.driver.findElements(By.css(".site-pagelist a")) 148 this.driver.findElements(By.css(".site-pagelist a"))
80 ).then(elements => 149 ).then(elements =>
81 Promise.all(elements.map(elem => elem.getAttribute("href"))) 150 Promise.all(elements.map(elem => elem.getAttribute("href")))
82 ).then(urls => 151 ).then(urls =>
83 { 152 {
84 let p1 = Promise.resolve(); 153 let p1 = testSubscribeLink(this.driver);
85 for (let url of urls) 154 for (let url of urls)
86 p1 = p1.then(() => 155 p1 = p1.then(() =>
87 this.driver.navigate().to(url) 156 this.driver.navigate().to(url)
88 ).then(() => 157 ).then(() =>
89 Promise.all([ 158 Promise.all([
90 getSections(this.driver), 159 getSections(this.driver),
91 this.driver.findElement(By.css("h2")).getAttribute("textContent"), 160 this.driver.findElement(By.css("h2")).getAttribute("textContent"),
92 this.driver.executeScript("document.body.classList.add('expected');") 161 this.driver.executeScript("document.body.classList.add('expected');")
93 ]) 162 ])
94 ).then(([sections, pageTitle]) => 163 ).then(([sections, pageTitle]) =>
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 By.css("a[href],button") 218 By.css("a[href],button")
150 ).click().then(() => 219 ).click().then(() =>
151 this.driver.sleep(100) 220 this.driver.sleep(100)
152 ).then(() => 221 ).then(() =>
153 this.driver.getAllWindowHandles() 222 this.driver.getAllWindowHandles()
154 ).then(handles => 223 ).then(handles =>
155 { 224 {
156 if (title.startsWith("$popup Exception -")) 225 if (title.startsWith("$popup Exception -"))
157 { 226 {
158 assert.equal(handles.length, 3, title); 227 assert.equal(handles.length, 3, title);
159 228 return closeWindow(this.driver, handles[2], handles[1]);
160 return this.driver.switchTo().window(handles[2]).then(() =>
161 this.driver.close()
162 ).then(() =>
163 this.driver.switchTo().window(handles[1])
164 );
165 } 229 }
166 230
167 assert.equal(handles.length, 2, title); 231 assert.equal(handles.length, 2, title);
168 }); 232 });
169 } 233 }
170 234
171 return takeScreenshot(element).then(screenshot => 235 return takeScreenshot(element).then(screenshot =>
172 assert.ok( 236 assert.ok(
173 screenshot.width == expectedScreenshot.width && 237 screenshot.width == expectedScreenshot.width &&
174 screenshot.height == expectedScreenshot.height && 238 screenshot.height == expectedScreenshot.height &&
175 screenshot.data.compare(expectedScreenshot.data) == 0, 239 screenshot.data.compare(expectedScreenshot.data) == 0,
176 title 240 title
177 ) 241 )
178 ); 242 );
179 }); 243 });
180 } 244 }
181 return p2; 245 return p2;
182 }); 246 });
183 return p1; 247 return p1;
184 }); 248 });
185 }); 249 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld